Adding thrust to an object which can rotate

Hello - 

This is related to my earlier thread on passing a value from one object to another.  BTW, passing the value itself is easy; the rest of the stuff was trickier.

Since I figured out how to rotate a bullet according to the ship’s rotation and make it move forward in relation to its rotation (facing forward), I though I’d apply the same logic to the ship to make it thrust forward in the direction it is facing.

It’s moving in the direction it’s pointing, alright, but it’s doing it awfully slow.  Even adding a large number (4000) to shipLinearVelocity near the end of the attached code snippet does not alter the slow velocity.  Any clues as to why it’s doing it so slow?  Is there another approach?

Thanks, regards.

local function thrustFwd() if touchedThrust == true then -- Play thrust sound -- audio.play( fireSound ) local posX = math.sin( math.rad( ship.rotation )) local posY = math.cos( math.rad( ship.rotation )) ship.x = ship.x + posX ship.y = ship.y - posY local function getLinearVelocity(rotation, velocity) local angle = math.rad(rotation) return { xVelocity = math.sin(angle)\* velocity, yVelocity = math.cos(angle) \* -velocity } end physics.addBody( ship, "dynamic" ) local shipLinearVelocity = getLinearVelocity(ship.rotation, 4000) ship:setLinearVelocity(ship.xVelocity, ship.yVelocity) end end

I was playing around with writing an asteroids game a few months back, and ended up using applyforce to ‘thrust’ the players ship.

I  used the following function to get the vector of the ship…

local function angle2VecDeg (angle) angle = angle\*math.pi/180 return math.cos(angle), math.sin(angle) end

and then in my player.enterFrame function had the following to move the ship…

if player.thrust == true then -- If thrust key is pressed local vecX, vecY = angle2VecDeg( player.rotation-90 ) player:applyForce ( vecX, vecY, player.x, player.y ) end

Hope this helps.

your real problem isn’t the trig, it’s that you’re not actually using the value “shipLinearVelocity” returned from your function.

Haha!  You guys were both right!  davebollinger rightfully noticed I used ship instead of shipLinearVelocity in the code above.  Appletreeman “knows” what I need, and applyForce is the proper way to do it.

Thanks to both.

Good morning all,

Just for your entertainment or info purposes.  Logically, when you release the thrust button you’d like the ship to slowly reduce speed.  I just discovered that linearDamping will let you achieve it.

Have a great day!

Haha, I’ve just replied to your PM about that :slight_smile:

I was playing around with writing an asteroids game a few months back, and ended up using applyforce to ‘thrust’ the players ship.

I  used the following function to get the vector of the ship…

local function angle2VecDeg (angle) angle = angle\*math.pi/180 return math.cos(angle), math.sin(angle) end

and then in my player.enterFrame function had the following to move the ship…

if player.thrust == true then -- If thrust key is pressed local vecX, vecY = angle2VecDeg( player.rotation-90 ) player:applyForce ( vecX, vecY, player.x, player.y ) end

Hope this helps.

your real problem isn’t the trig, it’s that you’re not actually using the value “shipLinearVelocity” returned from your function.

Haha!  You guys were both right!  davebollinger rightfully noticed I used ship instead of shipLinearVelocity in the code above.  Appletreeman “knows” what I need, and applyForce is the proper way to do it.

Thanks to both.

Good morning all,

Just for your entertainment or info purposes.  Logically, when you release the thrust button you’d like the ship to slowly reduce speed.  I just discovered that linearDamping will let you achieve it.

Have a great day!

Haha, I’ve just replied to your PM about that :slight_smile: