applyForce to a bullet in the direction the gun is pointing

I am trying to apply a force to a small display.newCircle() in the direction that the player rotates the gun itself.

–For future reference, I had to subtract 90 from “myAngle” to get this to work, after that everything lined up.​

Hey logan,

this question was answered in several topics already. (use the search function next time please)

Check for example this topic, which covers all you need:

https://forums.coronalabs.com/topic/63285-working-out-angle-to-apply-force/

Here’s the code in short:

local function angle2VecDeg(angle) angle = angle\*math.pi/180 return math.cos(angle), math.sin(angle) end local myAngle = 90 --the rotation of the object local myForce = 100 --the force you want to apply local vecX, vecY = angle2VecDeg(myAngle) myObject:applyForce(vecX\*myForce, vecY\*myForce)

Greetings

Tirben :slight_smile:

Could you explain how to implement this into my code and how it works. I see that you change the degrees to radians and return the cosine and sine of that but it is still doing the same as when I set the y force to the rotation of the gun in degrees. Thanks!!

Hey logan,

this question was answered in several topics already. (use the search function next time please)

Check for example this topic, which covers all you need:

https://forums.coronalabs.com/topic/63285-working-out-angle-to-apply-force/

Here’s the code in short:

local function angle2VecDeg(angle) angle = angle\*math.pi/180 return math.cos(angle), math.sin(angle) end local myAngle = 90 --the rotation of the object local myForce = 100 --the force you want to apply local vecX, vecY = angle2VecDeg(myAngle) myObject:applyForce(vecX\*myForce, vecY\*myForce)

Greetings

Tirben :slight_smile:

Could you explain how to implement this into my code and how it works. I see that you change the degrees to radians and return the cosine and sine of that but it is still doing the same as when I set the y force to the rotation of the gun in degrees. Thanks!!