Applying a force based on rotation

Hi,

All i’m trying to do is apply a force to an object based on rotation.

So if the object is upright then the force will apply from the bottom and it will go directly up.

if the object is rotated 45 degrees, then the force will push it in a 45 degree angle and so on.

I tried to object:applyForce(0,-10) on every frame but that just moves it up regardless of the rotation direction.

I think its just a function i’m not using that I should be but dont know which one.

Thanks

Kevin

Wouldn’t you use applyForce(-10,-10) and applyForce(10,-10) to push at 45 degrees?
 

I can push it 45 degrees, but it depends on the rotation of the object i’m trying to push, so I need to have a calculation which says how much force in each direction I should apply based on the rotation of the object.

If i’m trying to move “the camera” right and left based on if the object moves to the left or right, i’m not actually moving the ship left or right at all right? I’m actually moving the background?

Is the best way to apply a force on every frame that I hold the power button down?

Sorry for expanding this question a little bit I just want to make sure im doing things correctly.

You would use pythagorus theorem to calculate the correct values for the force direction. Do you need to apply specific force every touch or are you trying to move it at the same speed while the button is pressed? If second then setting velocity would be a better approach.

Is there any tutorial on how to do this?? I have a max speed, when I apply the thrust it will obviously start off slow because of gravity but it will get faster and faster until it hits max speed, as its coming down to earth again hitting the thrusters will slow down the drop to earth and over a small time will push it back up again.

You should be ok with applyForce. You just need to calcualte the correct force direction based on rotation.

Where A is rotation/360

tan(A) = opposite /adjacent = y / x

Remember you need to negate the y value as it’s reversed in content coordinate space so

y = -1 * (tan(A) * x)

x = y / tan(A)

Check my math but it should be something like that.

Sorry messed up the functions, you need the values from your force value so the functions would be:

y = -1 * sin(A) * force

x = cos(A) * force

Try that :slight_smile:

Check this out: http://developer.coronalabs.com/code/simple-draggable-fan-demo-2

If you need more help or if that demo doesn’t work, let me know and I’ll post an updated version.

Wouldn’t you use applyForce(-10,-10) and applyForce(10,-10) to push at 45 degrees?
 

I can push it 45 degrees, but it depends on the rotation of the object i’m trying to push, so I need to have a calculation which says how much force in each direction I should apply based on the rotation of the object.

If i’m trying to move “the camera” right and left based on if the object moves to the left or right, i’m not actually moving the ship left or right at all right? I’m actually moving the background?

Is the best way to apply a force on every frame that I hold the power button down?

Sorry for expanding this question a little bit I just want to make sure im doing things correctly.

You would use pythagorus theorem to calculate the correct values for the force direction. Do you need to apply specific force every touch or are you trying to move it at the same speed while the button is pressed? If second then setting velocity would be a better approach.

Is there any tutorial on how to do this?? I have a max speed, when I apply the thrust it will obviously start off slow because of gravity but it will get faster and faster until it hits max speed, as its coming down to earth again hitting the thrusters will slow down the drop to earth and over a small time will push it back up again.

You should be ok with applyForce. You just need to calcualte the correct force direction based on rotation.

Where A is rotation/360

tan(A) = opposite /adjacent = y / x

Remember you need to negate the y value as it’s reversed in content coordinate space so

y = -1 * (tan(A) * x)

x = y / tan(A)

Check my math but it should be something like that.

Sorry messed up the functions, you need the values from your force value so the functions would be:

y = -1 * sin(A) * force

x = cos(A) * force

Try that :slight_smile:

Check this out: http://developer.coronalabs.com/code/simple-draggable-fan-demo-2

If you need more help or if that demo doesn’t work, let me know and I’ll post an updated version.