I'm new to using Box 2D and never really understood physics much, and I can't seem to figure out what I'm doing wrong here.
Basically, I want to apply a small explosive impulse to a square at a point that is not it's center. It's almost like I want to flick the square at various points and have it react accordingly.
I have a world and a square created and I'm applying a force to it trying various spots for the second b2Vec2 spot of ApplyImpulse. It seems like no matter what point I pick, the square will always move straight up. Oddly depending on the spot I pick, the square moves straight up and then on it's collision with the floor will bounce in a certain direction.
I'm assuming that I'm thinking wrong but aren't the two b2Vec2's for ApplyImpulse used for the direction of the force and the point of that force?
Also, I've tried changing the first b2Vec2 spot and figured out how to move the square in an angle, but I need the square to spin around it's center as you'd expect it to.
In any case, here's the code:
(the box starts off in the air and then falls to the ground and THEN I try to apply my force.)
World Creation:
Floor Creation and Square Creation:
Game Tick:
My thinking is that I'm applying a force to the square that is straight up and at a point 10 meters away from the square's center. Obviously, this is wrong...
Any help would be appreciated!
Basically, I want to apply a small explosive impulse to a square at a point that is not it's center. It's almost like I want to flick the square at various points and have it react accordingly.
I have a world and a square created and I'm applying a force to it trying various spots for the second b2Vec2 spot of ApplyImpulse. It seems like no matter what point I pick, the square will always move straight up. Oddly depending on the spot I pick, the square moves straight up and then on it's collision with the floor will bounce in a certain direction.
I'm assuming that I'm thinking wrong but aren't the two b2Vec2's for ApplyImpulse used for the direction of the force and the point of that force?
Also, I've tried changing the first b2Vec2 spot and figured out how to move the square in an angle, but I need the square to spin around it's center as you'd expect it to.
In any case, here's the code:
(the box starts off in the air and then falls to the ground and THEN I try to apply my force.)
World Creation:
Code:
// WORLD CREATION
var gravity:b2Vec2 = new b2Vec2(0.0, 100);
// Construct world
world = new b2World(gravity, true);
var gravity:b2Vec2 = new b2Vec2(0.0, 100);
// Construct world
world = new b2World(gravity, true);
Floor Creation and Square Creation:
Code:
var floorBodyDef:b2BodyDef=new b2BodyDef();
floorBodyDef.position.Set(canvasWidth*.5*pxToM, (410)*pxToM);
var floorBody=world.CreateBody(floorBodyDef);
var floorShape:b2PolygonShape=new b2PolygonShape();
floorShape.SetAsBox(canvasWidth*.5*pxToM, pxToM);
var floorFixtureDef:b2FixtureDef = new b2FixtureDef();
floorFixtureDef.shape=floorShape;
var floorFixture:b2Fixture=floorBody.CreateFixture(floorFixtureDef);
var crateBodyDef:b2BodyDef=new b2BodyDef();
crateBodyDef.type=b2Body.b2_dynamicBody;
crateBodyDef.position.Set(pStartX*pxToM, pStartY*pxToM);
vehicleBody=world.CreateBody(crateBodyDef);
var crateShape:b2PolygonShape=new b2PolygonShape();
crateShape.SetAsBox(mWidth, mWidth);
var crateFixtureDef:b2FixtureDef = new b2FixtureDef();
crateFixtureDef.shape=crateShape;
crateFixtureDef.restitution=.25; // bouncy ness
crateFixtureDef.density=10; // bigger object = heavier..
var crateFixture:b2Fixture = vehicleBody.CreateFixture(crateFixtureDef);
floorBodyDef.position.Set(canvasWidth*.5*pxToM, (410)*pxToM);
var floorBody=world.CreateBody(floorBodyDef);
var floorShape:b2PolygonShape=new b2PolygonShape();
floorShape.SetAsBox(canvasWidth*.5*pxToM, pxToM);
var floorFixtureDef:b2FixtureDef = new b2FixtureDef();
floorFixtureDef.shape=floorShape;
var floorFixture:b2Fixture=floorBody.CreateFixture(floorFixtureDef);
var crateBodyDef:b2BodyDef=new b2BodyDef();
crateBodyDef.type=b2Body.b2_dynamicBody;
crateBodyDef.position.Set(pStartX*pxToM, pStartY*pxToM);
vehicleBody=world.CreateBody(crateBodyDef);
var crateShape:b2PolygonShape=new b2PolygonShape();
crateShape.SetAsBox(mWidth, mWidth);
var crateFixtureDef:b2FixtureDef = new b2FixtureDef();
crateFixtureDef.shape=crateShape;
crateFixtureDef.restitution=.25; // bouncy ness
crateFixtureDef.density=10; // bigger object = heavier..
var crateFixture:b2Fixture = vehicleBody.CreateFixture(crateFixtureDef);
Game Tick:
Code:
world.Step(0.025, 10, 10);
var hX:Number, hY:Number;
hX=vehicleBody.GetPosition().x;
hY=vehicleBody.GetPosition().y;
if (kMouse) {
vehicleBody.ApplyImpulse(new b2Vec2(0, -4000), new b2Vec2(hX+10, hY));
}
var hX:Number, hY:Number;
hX=vehicleBody.GetPosition().x;
hY=vehicleBody.GetPosition().y;
if (kMouse) {
vehicleBody.ApplyImpulse(new b2Vec2(0, -4000), new b2Vec2(hX+10, hY));
}
My thinking is that I'm applying a force to the square that is straight up and at a point 10 meters away from the square's center. Obviously, this is wrong...
Any help would be appreciated!