Box2D

 view release on metacpan or  search on metacpan

examples/mouse.pl  view on Meta::CPAN

use strict;
use warnings;
use Box2D;
use SDL;
use SDL::Video;
use SDL::Events qw(:type);
use SDLx::App;

# pixels
my $width  = 400;
my $height = 400;

# pixels per meter
my $ppm = 30;

# meters per pixel
my $mpp = 1.0 / $ppm;

# frames per second
my $fps      = 60.0;
my $timestep = 1.0 / $fps;

# velocity iterations
my $vIters = 30;

# position iterations
my $pIters = 30;

my $gravity = make_vec2( 0.0, 10.0 );
my $world = Box2D::b2World->new( $gravity, 1 );

my $ground = make_ground();

my $walls = make_walls(
    [ [ s2w(0.0), s2w(0.0) ], [ s2w(0.0), s2w($height) ] ],
    [ [ s2w(0.0), s2w(0.0) ], [ s2w($width), s2w(0.0) ] ],
    [ [ s2w($width), s2w(0.0) ], [ s2w($width), s2w($height) ] ],
    [ [ s2w(0.0), s2w($height) ], [ s2w($width), s2w($height) ] ],
);

my $ball = make_ball(
    x       => s2w( $width / 2.0 ),
    y       => s2w( $height / 2.0 ),
    radius  => s2w( rand(5.0) + 10.0 ),
    color   => [ 255, 0, 0 ],
    density => 1.0,
);

my @balls;
for ( 1 .. 100 ) {
    my $b = make_ball(
        x       => rand s2w($width),
        y       => rand s2w($height),
        radius  => s2w( rand(5.0) + 10.0 ),
        color   => [ 0, int rand(200) + 55, int rand(200) + 55 ],
        density => 1.0,
    );
    push @balls, $b;
}

my $joint = make_mouse_joint(
    target   => $ball->{body}->GetWorldCenter(),
    bodyA    => $ground->{body},
    bodyB    => $ball->{body},
    maxForce => 1000.0 * $ball->{body}->GetMass(),
);

my $app = SDLx::App->new(
    width  => $width,
    height => $height,
    dt     => $timestep,
    min_t  => $timestep / 2,
    flags  => SDL_DOUBLEBUF | SDL_HWSURFACE,
    eoq    => 1,
);

$app->add_event_handler(
    sub {
        my ($event) = @_;
        return unless $event->type == SDL_MOUSEMOTION;
        my ( undef, $x, $y ) = @{ SDL::Events::get_mouse_state() };
        $joint->SetTarget( make_vec2( s2w($x), s2w($y) ) );
    }
);

$app->add_show_handler(



( run in 1.100 second using v1.01-cache-2.11-cpan-71847e10f99 )