How can I make these two jets of flame move with the ship?

I have a ship, and I create two jets of flame separately and place them behind the ship. The only problem is that when I make the ship move, the flames stay in the same place. Does anyone know the formula for getting these jets of flame to move with the ship? I know there’s math involved.

I have the x and y values of the flames when the ship is at an angle of 0. I just need to make it move appropriately as the ship rotates.

Put them with the ship into a display group and then move the group?

That would work, but if he needs collision detection that will hose it up.

You can,

  1. Create ship art as spritesheets with and without the flame, then show the right frame at the right time.

  2. Give the flame a body and mount it to the ship. 

  3. Use an enter frame listener and calculate the ships position+rotation into a vector then translate the flame along that vector with the same rotation.

    local ship = display.newImageRect( … ) – DIY local flame = display.newImageRect( … ) – DIY flame.anchorY = 0 ship.flame = flame flame.offset = 10 function ship.enterFrame( self ) local vec = ssk.math2d.angle2Vector( self.rotation + 180 ) vec = ssk.math2d.normalize( vec ) vec = ssk.math2d.scale( vec, self.contentHeight/2 + self.flame.offset ) vec = ssk.math2d.add( vec, self ) self.flame.rotation = self.rotation self.flame.x = vec.x self.flame.y = vec.y end; listen( “enterFrame”, ship )

A. May contain typos and errors

B. This assumes your art (for ship and flame) are oriented like this:

spaceship.pod_.1.green_.png   8eac0a36668451.572d74ac09ca6.gif

Suggestion.  Test this alone before putting it into your game.

i.e. Make a small sample that just draws a ship and flame.  Then add this code.  Then place the ship in random places at random orientations to verify it works.

That is very close, but my ship looks like this:

I don’t have time to respond at length, but if you have a ship image and a ‘flame’ image where they are the same size and line up perfectly, this becomes drop dead simple.

local flame = display.newImageRect( ... ) -- DIY MUST BE FIRST SO IT IS BELOW SHIP local ship = display.newImageRect( ... ) -- DIY ship.flame = flame function ship.enterFrame( self ) self.flame.rotation = self.rotation self.flame.x = self.x self.flame.y = self.y end; listen( "enterFrame", ship )

ship.png flame.png

I agree with @roaminggamer… if the image (sprite) method can work for you, it’s the easiest (just make the overall “canvas” the same size as the ship, so both the ship and flames can be centrally aligned).

Only if you want to make these flames into particle emitters will you need to tinker around with moving/rotating them in an offset manner as the ship rotates.

Brent

Oh, you mean make the frames of the flames and the ship the same size? Unfortunately, this option does not apply to me.

You mentioned mounting the body to the ship, how would I go about doing this?

That is the worst way.

The ship and the flame would need physics bodies.  

Also, it could behave badly.  I would not do this.

It seems number 3 is the only option here. But for some reason, I can’t get the enterFrame to run.  :( I’m going to see if I can get the canvas to be the same size then.

I would suggest an alternative of using this API:

https://docs.coronalabs.com/api/type/DisplayObject/localToContent.html

You’ll need to follow and understand the example carefully, because this initially can be tricky to understand what it’s doing. Basically, you can take any “point” on an object… the ship for example… and then, when it rotates, you can gather the new “screen position” of that specific point relative to whatever the ship is rotated to, and then reposition another object (flame) according to that position.

Of course you’ll still need to determine when the ship rotates, either by “enterFrame” or some other means.

Brent

Thanks for all your help, RG helped me with this. :slight_smile:

Put them with the ship into a display group and then move the group?

That would work, but if he needs collision detection that will hose it up.

You can,

  1. Create ship art as spritesheets with and without the flame, then show the right frame at the right time.

  2. Give the flame a body and mount it to the ship. 

  3. Use an enter frame listener and calculate the ships position+rotation into a vector then translate the flame along that vector with the same rotation.

    local ship = display.newImageRect( … ) – DIY local flame = display.newImageRect( … ) – DIY flame.anchorY = 0 ship.flame = flame flame.offset = 10 function ship.enterFrame( self ) local vec = ssk.math2d.angle2Vector( self.rotation + 180 ) vec = ssk.math2d.normalize( vec ) vec = ssk.math2d.scale( vec, self.contentHeight/2 + self.flame.offset ) vec = ssk.math2d.add( vec, self ) self.flame.rotation = self.rotation self.flame.x = vec.x self.flame.y = vec.y end; listen( “enterFrame”, ship )

A. May contain typos and errors

B. This assumes your art (for ship and flame) are oriented like this:

spaceship.pod_.1.green_.png   8eac0a36668451.572d74ac09ca6.gif

Suggestion.  Test this alone before putting it into your game.

i.e. Make a small sample that just draws a ship and flame.  Then add this code.  Then place the ship in random places at random orientations to verify it works.

That is very close, but my ship looks like this:

I don’t have time to respond at length, but if you have a ship image and a ‘flame’ image where they are the same size and line up perfectly, this becomes drop dead simple.

local flame = display.newImageRect( ... ) -- DIY MUST BE FIRST SO IT IS BELOW SHIP local ship = display.newImageRect( ... ) -- DIY ship.flame = flame function ship.enterFrame( self ) self.flame.rotation = self.rotation self.flame.x = self.x self.flame.y = self.y end; listen( "enterFrame", ship )

ship.png flame.png

I agree with @roaminggamer… if the image (sprite) method can work for you, it’s the easiest (just make the overall “canvas” the same size as the ship, so both the ship and flames can be centrally aligned).

Only if you want to make these flames into particle emitters will you need to tinker around with moving/rotating them in an offset manner as the ship rotates.

Brent

Oh, you mean make the frames of the flames and the ship the same size? Unfortunately, this option does not apply to me.