HTML and CSS Reference
In-Depth Information
Creating Balls
Creatingtheballsthatwillfallandbouncearoundinourdemoisverysimilartocreatingwalls
in Box2D. For this exercise, we will create 50 balls with random locations and sizes. The loc-
ations and sizes are (again) in MTS units. The only real difference from creating the walls is
that we set the fixture shape property to b2CircleShape(size) , where size is a random size
in MTS units.
var
var numBalls = 50 ;
var
var balls = new
new Array ();
for
for ( var
var i = 0 ; i < numBalls ; i ++ ) {
var
var ballDef = new
new b2BodyDef ;
ballDef . type = b2Body . b2_dynamicBody ;
var
var ypos = ( Math . random () * 8 ) + 1 ;
var
var xpos = ( Math . random () * 14 ) + 1 ;
var
var size = ( Math . random () * . 4 ) + 0.2 ;
ballDef . position . Set ( xpos , ypos );
var
var ballFixture = new
new b2FixtureDef ;
ballFixture . density = 10.0 ;
ballFixture . friction = 0.5 ;
ballFixture . restitution = 1 ;
ballFixture . shape = new b2CircleShape ( size );
var
var newBall = world . CreateBody ( ballDef )
newBall . CreateFixture ( ballFixture );
balls . push ( newBall );
}
Search WWH ::




Custom Search