Can't figure out second parameter of applyForce()

Discuss issues specific to the Flash/AS3 port of Box2D
kamcknig
Posts: 6
Joined: Fri Sep 14, 2012 10:37 am

Can't figure out second parameter of applyForce()

Postby kamcknig » Thu Sep 20, 2012 9:16 am

Hi everyone.

I have an app where the user clicks and holds the mouse to build up force, when released, it will apply force to objects in the world based on how long the mouse was held down.

In the docs for applyForce it says the second parameter is "the world position of the point of application." So to me that says it's the global position of the mouse x and y when clicked in this case.

But this doesn't work. It does apply force to my objects, but it adds no torque at all. If I change the point to be 0, 0 instead of where the mouse is clicked, then it appears to work fine. It will add torque along with linear force. Why is this? Can someone explain this parameter in more detail and what it actually means? Because it seems to me that all I have to do is always apply the force at 0, 0... but then what's the point of having it if that's the only way ti works? Is it local to the body the force is applied to? Is it global tot he world like it says? And if it's global then why does using the mouse coordinates not work properly?

Thanks so much!
Kyle

jayther
Posts: 266
Joined: Sun Oct 25, 2009 3:28 am
Contact:

Re: Can't figure out second parameter of applyForce()

Postby jayther » Fri Sep 21, 2012 12:20 am

Make sure you're multiply/dividing by your pixel-to-meter or meter-to-pixel ratio when you pass in the mouse x and y into the b2Vec2 for the applyForce. You may also need to offset the x and y if the (0,0) coordinate in the Box2D world is not the top left corner of the screen.

Also, for dragging bodies around with the mouse, the mouse joint will be better for this kind of thing.

kamcknig
Posts: 6
Joined: Fri Sep 14, 2012 10:37 am

Re: Can't figure out second parameter of applyForce()

Postby kamcknig » Fri Sep 21, 2012 8:01 am

Thanks for answering! And I'm sorry but I'm not sure I'm following still.

This is more of an explosion, all items should fly away from the click point with an amount of force based on both how far away the mouse is and how long the mouse was held down.

I am actually using the Gideros port of Box2D for android, but I come from an ActionScript background and there are no forums specifcally for the mobiel port (well the iPhone one) but the ideas are the same so I've posted here and the ideas should be the same so I figured if someone could speak it in ActionScript I could get it and port the idea.

So here is my code in Pseudo form:

Code: Select all

get body's world x and y
get the mouse's x and y (is this where I need to multiply/divide? if so which one haha) (my world scale is set at 60)
get distance from mousing using standard distance equation

IF distance < maxDistance
{
    get the x and y differences of body and mouse
    get the angle from mouse to body using atan2
    get the x and y components of the angle vector using cos and sin and multiple by force and percentage of max distance to get the total x and y forces
    apply the force using applyForce to the body at the body's world center of mass
}


and here is the actual code in case it might make more sense to see it as code even if it's lua and not AS3. self.originY/X are the mouse event's x and y coordinates

Code: Select all

local cx, cy = body:getPosition();
local distance = math.sqrt((cy - self.originY) * (cy - self.originY) + (cx - self.originX) * (cx - self.originX));

if distance <= maxDistance then
    local yDiff = cy - self.originY;
    local xDiff = cx - self.originX;
    local angle = math.atan2(yDiff, xDiff);
    local xComponent = math.cos(angle) * force * (1 - distance / maxDistance);
    local yComponent = math.sin(angle) * force * (1 - distance / maxDistance);
    local wx, wy = body:getWorldCenter();
    body:applyForce(xComponent, yComponent, wx, wy);
end

jayther
Posts: 266
Joined: Sun Oct 25, 2009 3:28 am
Contact:

Re: Can't figure out second parameter of applyForce()

Postby jayther » Fri Sep 21, 2012 8:30 pm

Applying a force at the body's center will not cause any torque. When you apply a force that's in the center of mass, there won't be any torque. The force has to be out of the center of mass (and the force direction does not intersect the center) to cause any sort of torque.

So, with that said, you'll need to have the world point of force not be in the center of the body like what you're doing with that last couple of lines.

kamcknig
Posts: 6
Joined: Fri Sep 14, 2012 10:37 am

Re: Can't figure out second parameter of applyForce()

Postby kamcknig » Fri Sep 21, 2012 8:51 pm

That's what I had originally thought. So I had used the point where the mouse hit first but it gave really wonky results ill keep trying, thanks!


Return to “Flash”



Who is online

Users browsing this forum: Google [Bot] and 4 guests