Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 74 lines (48 sloc) 1.57 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
use strict;
use warnings;
use Box2D;
use SDL;
use SDL::Video;
use SDLx::App;

my $app = SDLx::App->new( width => 300, height => 300, flags => SDL_DOUBLEBUF | SDL_HWSURFACE, eoq => 1 );

my $vec = Box2D::b2Vec2->new(2.5,-10);
my $world = Box2D::b2World->new($vec, 1);

my $body_def = Box2D::b2BodyDef->new();

$body_def->position->Set(0.0, -2.0);

my $groundBody = $world->CreateBody($body_def);

my $groundBox = Box2D::b2PolygonShape->new();

$groundBox->SetAsBox(50.0, 10.0);

$groundBody->CreateFixture( $groundBox, 0.0 );

my $pos = $groundBody->GetPosition();

$app->update();

my $bodyDef = Box2D::b2BodyDef->new();
$bodyDef->type(Box2D::b2_dynamicBody);
$bodyDef->position->Set(0.0, 100.0);

my $body = $world->CreateBody($bodyDef);


my $dynamicBox = Box2D::b2PolygonShape->new();
$dynamicBox->SetAsBox( 1.0, 1.0 );


my $fixtureDef = Box2D::b2FixtureDef->new();
$fixtureDef->shape( $dynamicBox );
$fixtureDef->density(1.0);
$fixtureDef->friction(0.01);


$body->CreateFixtureDef($fixtureDef);

my $timeStep = 1.0/60.0;
my $velocityIterations = 6;
my $positionIterations = 2;
$app->add_move_handler(
sub {

$world->Step( $timeStep, $velocityIterations, $positionIterations );
$world->ClearForces();


}
);

$app->add_show_handler( sub
{
$app->draw_rect([0,0,300,300],[0,0,0,255]);
$app->draw_rect( [$pos->x(), 250-$pos->y(), 50, 10], [0,255,0,255] );
my $position = $body->GetPosition();
my $angle = $body->GetAngle();
warn $angle;
$app->draw_rect( [$position->x(), 250-$position->y(), 1*16, 1*16], [255,255,0,255] );
$app->update();

} );

$app->run();
Something went wrong with that request. Please try again.