Box2D

 view release on metacpan or  search on metacpan

examples/clock.pl  view on Meta::CPAN

# This example uses b2RevoluteJoint and it's motor feature to simulate a
# clock. Instead of showing the correct time, which would be boring, the
# clock hand move wildly. A collision group is also used to demonstrate
# how to prevent collisions using a group index. Without this feature,
# the clock hands would collide with one another.
use strict;
use warnings;
use Box2D;
use SDL;
use SDL::Video;
use SDL::GFX::Primitives;
use SDLx::App;

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

# 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 = 10;

# position iterations
my $pIters = 10;

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

my $pivot = {
    x0     => s2w( $width / 2 ),
    y0     => s2w( $height / 2 ),
    radius => s2w(4),
    color  => 0x3F5400FF,
};
$pivot = { %$pivot, %{ make_static_circle( @$pivot{qw( x0 y0 radius )} ) } };

my $anchor = Box2D::b2Vec2->new( @$pivot{qw( x0 y0 )} );

my $hourHand = {
    x0    => s2w( $width / 2 - 2 ),
    y0    => s2w( $height / 2 ),
    w     => s2w(5),
    h     => s2w(70),
    color => 0x3F5400FF,
};
$hourHand
    = { %$hourHand, %{ make_dynamic_rect( @$hourHand{qw( x0 y0 w h )} ) } };

my $minuteHand = {
    x0    => s2w( $width / 2 - 1 ),
    y0    => s2w( $height / 2 - 110 ),
    w     => s2w(3),
    h     => s2w(110),
    color => 0x3F5400FF,
};
$minuteHand = { %$minuteHand,
    %{ make_dynamic_rect( @$minuteHand{qw( x0 y0 w h )} ) } };

my $secondHand = {
    x0    => s2w( $width / 2 ),
    y0    => s2w( $height / 2 - 10 ),
    w     => s2w(1),
    h     => s2w(120),
    color => 0x541500FF,
};
$secondHand = { %$secondHand,
    %{ make_dynamic_rect( @$secondHand{qw( x0 y0 w h )} ) } };

make_revolute_joint( $pivot->{body}, $hourHand->{body}, $anchor, 0.5, 100 );
make_revolute_joint( $pivot->{body}, $minuteHand->{body}, $anchor, -1.1,
    100 );
make_revolute_joint( $pivot->{body}, $secondHand->{body}, $anchor, 2.0, 100 );

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

my $realFps = $fps;



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