Tag Archives: restitution

How to add sprite to a body in Box2D in Adobe AIR, FLEX and FLASH? Or Add image/Sprite to a body in ActionScript.

By | April 12, 2012

The below code is for FlashBuilder 4, If you are using FlexBuilder3 use addChild() instead of the commented block in the following code. [java] public function addCircle(){x:Number, y: Number, radius:Number, ballcip:Ball):void{ bd:b2BodyDef = new b2BodyDef(); var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, imageFailed); var request:URLRequest = new URLRequest("ball.png"); loader.load(request); /*optionally change the position of the image if not fitting… Read More »

How to shoot a bullet in the direction of touch in Box2D iphone?

By | January 30, 2011

write the below code in “ccTouchesBegan” function and you are done. Here the ballBody represents the bullet body. You can increase the power variable to increase bullet speed. [java] UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInView:[touch view]]; location = [[CCDirector sharedDirector] convertToGL:location]; CGPoint shootVector = ccpSub(location, ball.position); CGFloat shootAngle = ccpToAngle(shootVector); CGPoint normalizedShootVector = ccpNormalize(shootVector);… Read More »

How to rotate a body manually in Box2D?

By | January 24, 2011

The below code helps you to rotate a body in Box2D manually. [java] -(void) start { rot_sprite = [CCSprite spriteWithFile:[NSString stringWithFormat:@"rot_sprite.png" ]]; [self addChild: rot_sprite]; b2BodyDef bodyDef; b2Vec2 initVel; b2PolygonShape shape; b2CircleShape circleShape; b2FixtureDef fd; b2Body * rotating_body; b2RevoluteJointDef revJointDef; b2DistanceJointDef jointDef; b2Vec2 pos; bodyDef.position.Set(11.043825f, 4.984064f); bodyDef.angle = 0.000000f; bodyDef.userData = rot_sprite; rotating_body = world->CreateBody(&bodyDef;); initVel.Set(0.000000f, 0.000000f); rotating_body->SetLinearVelocity(initVel);… Read More »