appleForce direction relative to body rotation?

I’m trying to figure out how to use applyForce relative to the rotation of a body.

For example, if the body is right side up, the force is vertical; if the body is rotated 90 degrees the force is horizontal.

Obviously I could fake it of those were the only two cases, but the force needs to be relative to any body rotation.

I’d be grateful for any help or direction to look in, can’t figure this one out :slight_smile: [import]uid: 66672 topic_id: 11016 reply_id: 311016[/import]

you can use below

[lua]local forceMagnitude = 1000

local angle = object.rotation

forceX = math.cos(angle)*forceMagnitude
forceY = math.sin(angle)*forceMagnitude

– now use forceX and forceY to apply force[/lua] [import]uid: 48521 topic_id: 11016 reply_id: 40085[/import]

Hi, thanks!

I ended up going with
[lua]body.rotation = body.rotation +.5;
body:applyForce(2, 2 * math.cos( math.rad( 180 + body.rotation ) ), body.x, body.y );[/lua]

Which is working fairly well. Will probably have to tweak after on device testing though. [import]uid: 66672 topic_id: 11016 reply_id: 40088[/import]

This is exactly what I have been looking for, unfortunately it does not appear to work for me.
Using the following code, the results are not as expected -

local forceMagnitude = 100
local angle = ball.rotation
xForce = math.cos(angle)*forceMagnitude
yForce = math.sin(angle)*forceMagnitude

Using an example angle of 45 the results are -
xForce = 52.532198881773
yForce = 85.090352453412

When increasing the angle to just 46 the results are -
xForce = -43.217794488478
yForce = 90.178834764881

I may be missing something really obvious, but surely it shouldn’t be jumping to a negative figure after just one degree.

Any help would be greatly appreciated. [import]uid: 49987 topic_id: 11016 reply_id: 40185[/import]

ooh… i know why… because object.rotation returns angle in degrees… and math.cos/math.sin take angle parameter in radian.

you can change the code to…

[lua]local forceMagnitude = 1000

local angle = math.rad(object.rotation)

forceX = math.cos(angle)*forceMagnitude
forceY = math.sin(angle)*forceMagnitude[/lua] [import]uid: 48521 topic_id: 11016 reply_id: 40212[/import]

Ah, of course.
I even read that in the API reference, but it didnt sink in for some reason!

Thanks very much. [import]uid: 49987 topic_id: 11016 reply_id: 40241[/import]

Hello,

I am having the very same problem! I tried this code out a couple times but no success :frowning: … I have been looking for this FOR A VERY LONGTIME. And now i found it but its not working! I have an object names “snow” and ball is going to hit it. Snow’s rotation is always different, and i need to add a linear impulse instead of a force. Do i need any collision functions? Basically when ball hits snow let’s say snow is 45 degrees, then ball would have a linear impulse in the direction of 45 degrees wherever snow is pointing.

Thanks a lot!
I WOULD REALLY LOVE ANY HELP. [import]uid: 23689 topic_id: 11016 reply_id: 49656[/import]

Hello,

I am having the very same problem! I tried this code out a couple times but no success :frowning: … I have been looking for this FOR A VERY LONGTIME. And now i found it but its not working! I have an object names “snow” and ball is going to hit it. Snow’s rotation is always different, and i need to add a linear impulse instead of a force. Do i need any collision functions? Basically when ball hits snow let’s say snow is 45 degrees, then ball would have a linear impulse in the direction of 45 degrees wherever snow is pointing.

Thanks a lot!
I WOULD REALLY LOVE ANY HELP. [import]uid: 23689 topic_id: 11016 reply_id: 49657[/import]