Hello,
so I'm making a catapult game thing using Box2D, Actionscript 3 port and I've noticed a problem.
Every time the ball is launched it behaves differently than the last time despite all the settings being the same.
What I'm doing is creating a physical ball every time the catapult fires and giving it an impulse (every time it's the same impulse).
Then, it the user clicks the screen, the ball is destroyed and a new one is created when the catapult fires next time.
One pattern I noticed is that the faster the ball is destroyed since it was created, the stronger the force is next time
So if I let it roll on the ground for some time before destroying it, it barely flies off next time I launch.
I figured it has to be something with the way I'm destroying them. I was just doing world.DestroyBody(ball), and after I also tried manually destroying the fixture too. No dice.
Here's the code I use to create the ball:
The velocity is always the same.
Any help would be appreciated. Thank you.
so I'm making a catapult game thing using Box2D, Actionscript 3 port and I've noticed a problem.
Every time the ball is launched it behaves differently than the last time despite all the settings being the same.
What I'm doing is creating a physical ball every time the catapult fires and giving it an impulse (every time it's the same impulse).
Then, it the user clicks the screen, the ball is destroyed and a new one is created when the catapult fires next time.
One pattern I noticed is that the faster the ball is destroyed since it was created, the stronger the force is next time
So if I let it roll on the ground for some time before destroying it, it barely flies off next time I launch.
I figured it has to be something with the way I'm destroying them. I was just doing world.DestroyBody(ball), and after I also tried manually destroying the fixture too. No dice.
Here's the code I use to create the ball:
Code:
public static function CreatePhysicsBall(x:Number, y:Number, xVelocity:Number, yVelocity:Number):void
{
trace(xVelocity, yVelocity);
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.type = b2Body.b2_dynamicBody;
var body:b2Body = World.CreateBody(bodyDef);
var shape:b2CircleShape = new b2CircleShape(Ball.width / 2 * PxToM);
var fixtureDef:b2FixtureDef = new b2FixtureDef();
fixtureDef.density = 30;
fixtureDef.restitution = 0;
fixtureDef.friction = 0.9;
fixtureDef.shape = shape;
fix = body.CreateFixture(fixtureDef);
body.SetUserData(Ball);
Ball.PhysicalBody = body;
body.SetPosition(new b2Vec2(x * PxToM, y * PxToM));
body.ApplyImpulse(new b2Vec2(xVelocity * PxToM, yVelocity * PxToM), new b2Vec2(0, 0));
}
{
trace(xVelocity, yVelocity);
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.type = b2Body.b2_dynamicBody;
var body:b2Body = World.CreateBody(bodyDef);
var shape:b2CircleShape = new b2CircleShape(Ball.width / 2 * PxToM);
var fixtureDef:b2FixtureDef = new b2FixtureDef();
fixtureDef.density = 30;
fixtureDef.restitution = 0;
fixtureDef.friction = 0.9;
fixtureDef.shape = shape;
fix = body.CreateFixture(fixtureDef);
body.SetUserData(Ball);
Ball.PhysicalBody = body;
body.SetPosition(new b2Vec2(x * PxToM, y * PxToM));
body.ApplyImpulse(new b2Vec2(xVelocity * PxToM, yVelocity * PxToM), new b2Vec2(0, 0));
}
The velocity is always the same.
Any help would be appreciated. Thank you.