Applying Impulses to Objects (Newbie here)

Hello again,

I'm currently trying to work out how to correctly apply forces to an object to get it to behave as I desire. Essentially I have a ball on screen that I want the user to be able to hold the mouse down over, then drag out a straight line to set the direction that they want to ball to go in. The length of line the drawn will determine how powerful the force applied to the ball will be (this will obviously be limited to avoid ridiculously powerful shots!). When they release the mouse button, I am currently using the following code to apply an impulse to the centre of the ball to make it move:

//Set velocity, etc
var forceX:Number = (trajectory.scaleX * 5 * (mouseX - ballMC.x))/ratio;
var forceY:Number = (trajectory.scaleX * 5 * (mouseY - ballMC.y))/ratio;
var totForce:b2Vec2 = new b2Vec2(forceX, forceY);
var point:b2Vec2 = new b2Vec2(ballMC.x, ballMC.y);

trace("Applying a force of (" + forceX + ", " + forceY + ")");
krong.ApplyImpulse(totForce, point);

The variable 'BallMC' is simply a MovieClip which references the userData of the ball physics object. The '5' used to calculate the force is just an arbitrary number I'm using to try to get the speed just right.

Unfortunately it seems to move as would be expected at first, but occasionally begins rotating like crazy and sliding about the 'ground' rectangle. It also seems to sometimes (although not always) speed up when it collides with the sides (the game is surrounded by 4 rectangles to form a 'frame') - I've checked and neither the restitution or friction values are more than 1 so I have no idea why this is happening...

In case this helps, here is the code where I initialize the ball, and one of the sides (all sides are identical in terms of friction, etc so I will not bore you with the rest of them):

//Add Ball
var ballDef:b2BodyDef = new b2BodyDef();
ballDef.position.Set((stageWidth/2)/ratio, (stageHeight/2)/ratio);
ballDef.userData = new Ball();
addChild(ballDef.userData);

var ballShape:b2CircleDef = new b2CircleDef();
ballShape.radius = 24/ratio;
ballShape.density = 1;
ballShape.friction = 0.5;
ballShape.restitution = 0.5;

ball = gameWorld.CreateBody(ballDef);
ball.CreateShape(ballShape);
ball.SetMassFromShapes();
ballMC = ballDef.userData;
ballMC.addEventListener(MouseEvent.MOUSE_DOWN, ballMouseDown);

//Bottom side
var bottomSideDef:b2BodyDef = new b2BodyDef();
bottomSideDef.position.Set((stageWidth/2)/ratio, (stageHeight-7.5)/ratio);

//Add a sprite
bottomSideDef.userData = new PlainEdge();
bottomSideDef.userData.width = stageWidth;
bottomSideDef.userData.height = 15;
addChild(bottomSideDef.userData);

var bottomShape:b2PolygonDef = new b2PolygonDef();
bottomShape.SetAsBox((stageWidth/2)/ratio, 7.5/ratio); //HALF width/height values
bottomShape.friction = 0.2;
bottomShape.density = 0; //Static object

var bottomSide:b2Body = gameWorld.CreateBody(bottomSideDef);
bottomSide.CreateShape(bottomShape);
bottomSide.SetMassFromShapes();

If anyone can see anything wrong with this then please let me know, every little helps at the moment!

Thanks in advance 🙂
For the record, I've been fiddling with the program for a while now, and it seems that it's only behaving oddly when it is 'spinning around like crazy' so if anyone can see why it might be doing this (are my friction values odd in any way?) then it should be problem solved.
It looks like you are applying the force to the wrong position on the body. You are using the sprite's x/y coordinates but not dividing them by your scaling constant.
Aha! That sorted it - thank you so much! God I wish Box2D worked in pixels 😆

Original thread: https://box2d.org/forum/viewtopic.php?t=4711