Best want to slide an object forward with physics?

Hello,

I have applied power and angle to make an object be thrown like you would in Angry Birds. Now what I want to do is put my finger on an object and push it forward at what ever angle. How can I do this and then know the angle and power to continue it with? It’s not going in the air but on a table for something like air hockey. I want to push a hockey puck at any angle forward for no more than 75 pixels and either at that point or before if I let go I want it to continue on. I’ll want to apply some drag on it as well.

Thanks,

Warren

You can put a touch handler on the object. In the began phase, record the initial touch x and y positions (as x1, y1 perhaps). In the ended phase you record the final x and y positions (as x2, y2). Then you can say

deltaX = x2 - x1 deltaY = y2 - y1

You can use pythagoras with these values to get the distance of the swipe, and trigonometry to get the angle:

local angle = math.atan2(deltaY, deltaX)

You can use these to set the linear velocity of the object to whatever you like. To put drag on the object you can set its linearDamping to a value greater than 0.

Hi Warren,

This may be an ideal case for a physics joint. Have you considered attaching an invisible (but hit-testable) object of smaller radius to the “puck” using a “distance” joint, offsetting it slightly away from the center of the puck? Then, detect touch on that smaller object, and as you move it around, it should ideally push the puck ahead of it.

Brent

You can put a touch handler on the object. In the began phase, record the initial touch x and y positions (as x1, y1 perhaps). In the ended phase you record the final x and y positions (as x2, y2). Then you can say

deltaX = x2 - x1 deltaY = y2 - y1

You can use pythagoras with these values to get the distance of the swipe, and trigonometry to get the angle:

local angle = math.atan2(deltaY, deltaX)

You can use these to set the linear velocity of the object to whatever you like. To put drag on the object you can set its linearDamping to a value greater than 0.

Hi Warren,

This may be an ideal case for a physics joint. Have you considered attaching an invisible (but hit-testable) object of smaller radius to the “puck” using a “distance” joint, offsetting it slightly away from the center of the puck? Then, detect touch on that smaller object, and as you move it around, it should ideally push the puck ahead of it.

Brent