Rotation - passing or using value from one object to another

Hello - 

As part of my porting of Space Quartz from GameSalad to Corona, I’ve already created various scenes, and now I am focused on the game scene.  At this point I have created the buttons and am coding their corresponding code.  I have written the code for the left and right buttons; the ship rotates based on button press.  I have also written the code for the fire button, but need to work on the “bullet” itself.

I would like the bullet to start off in the angle the ship is facing.  I can see (I am printing it in the console!) the ship’s rotation value (ship.rotation), and would like to use that as the bullet’s initial value and have it move forward based on that value.  As a matter of fact, that part of code can also help with the ship’s thrust forward regardless of angle (ship.rotation).

In GameSalad you could specify forward movement relative to the object (or the scene, but we’re focused on the object); in this case that’s what would apply.  Any ideas as to what commands/arguments I should be looking into?

Thanks, regards.

Assuming you want the bullet to be rotated AND positioned at the tip of the ship when it is fired, you need to do three things:

  1. rotate the bullet to the same rotation value as the ship (this is very easy)

  2. position the bullet at the rotation value around the ship (this is fairly easy)

  3. move the bullet away from the ship in x,y values relative to it’s starting position (this is also very easy)

To handle #2 you simply start with a pair of values, eg: 

{x=0,y=-10}

In this case ‘up’ is a negative y value, as with all motion on the graphics layer. So, the above table would be the starting location for something 10 pixel above 0,0.

You then rotate that table around the 0,0 position by the same rotation value as the ship’s property has, ie: ship.rotation. You can use my mathlib.lua for this, but it’s not hard…

https://gist.github.com/HoraceBury/9431861

require("mathlib") local startingPoint = math.rotateTo( {x=0,y=-10}, ship.rotation, {x=0,y=0} )

Then place the bullet at location ‘startingPoint’ relative to the ship (you can pass ‘ship’ as the third parameter to the ‘rotateTo’ function.

In addition to the great answer by @horacebury, I wanted to add that while Corona doesn’t have  a built-in ‘forward’ concept, the SSK action libs do:

As well, SSK2 comes with a decent 2D math lib: https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/math2D/

The mathlib by @horacebury rocks too.

Tip: Corona uses the screen coordinate system not caretesin coordinates.  Don’t let that trip you up.  In my games and apps I use these unit vectors to represent the four major directions/angles:

  • <0,-1>  up / 0-degrees
  • <1, 0>  right / 90-degrees
  • <0, 1>  down / 180-degrees
  • <-1,-0>  left / 270-degrees

Thanks to both for your replies.  I downloaded mathlib first and am absorbing all the info.  I’ll report back.

Got it to work.  I’ll fine tune it to taste later, but conceptually, here it is.

local function fireLaser() audio.play( fireSound ) local newLaser = display.newImageRect( mainGroup, objectSheet, 5, 14, 40 ) newLaser.myName = "laser" local h = ship.height local posX = h \* math.sin( math.rad( ship.rotation )) local posY = h \* math.cos( math.rad( ship.rotation )) newLaser.x = ship.x + posX newLaser.y = ship.y - posY newLaser.rotation = ship.rotation local function getLinearVelocity(rotation, velocity) local angle = math.rad(rotation) return { xVelocity = math.sin(angle) \* velocity, yVelocity = math.cos(angle) \* -velocity } end physics.addBody( newLaser, "dynamic" ) local newLaserLinearVelocity = getLinearVelocity(newLaser.rotation, 500) newLaser:setLinearVelocity(newLaserLinearVelocity.xVelocity, newLaserLinearVelocity.yVelocity) newLaser:toBack() end&nbsp;

This works for now.  There’s still some stuff to add, like screen wrapping and destroying the object after traveling through space for 1 second or so without hitting anything.

Thanks for your help.  Regards.

Assuming you want the bullet to be rotated AND positioned at the tip of the ship when it is fired, you need to do three things:

  1. rotate the bullet to the same rotation value as the ship (this is very easy)

  2. position the bullet at the rotation value around the ship (this is fairly easy)

  3. move the bullet away from the ship in x,y values relative to it’s starting position (this is also very easy)

To handle #2 you simply start with a pair of values, eg: 

{x=0,y=-10}

In this case ‘up’ is a negative y value, as with all motion on the graphics layer. So, the above table would be the starting location for something 10 pixel above 0,0.

You then rotate that table around the 0,0 position by the same rotation value as the ship’s property has, ie: ship.rotation. You can use my mathlib.lua for this, but it’s not hard…

https://gist.github.com/HoraceBury/9431861

require("mathlib") local startingPoint = math.rotateTo( {x=0,y=-10}, ship.rotation, {x=0,y=0} )

Then place the bullet at location ‘startingPoint’ relative to the ship (you can pass ‘ship’ as the third parameter to the ‘rotateTo’ function.

In addition to the great answer by @horacebury, I wanted to add that while Corona doesn’t have  a built-in ‘forward’ concept, the SSK action libs do:

As well, SSK2 comes with a decent 2D math lib: https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/math2D/

The mathlib by @horacebury rocks too.

Tip: Corona uses the screen coordinate system not caretesin coordinates.  Don’t let that trip you up.  In my games and apps I use these unit vectors to represent the four major directions/angles:

  • <0,-1>  up / 0-degrees
  • <1, 0>  right / 90-degrees
  • <0, 1>  down / 180-degrees
  • <-1,-0>  left / 270-degrees

Thanks to both for your replies.  I downloaded mathlib first and am absorbing all the info.  I’ll report back.

Got it to work.  I’ll fine tune it to taste later, but conceptually, here it is.

local function fireLaser() audio.play( fireSound ) local newLaser = display.newImageRect( mainGroup, objectSheet, 5, 14, 40 ) newLaser.myName = "laser" local h = ship.height local posX = h \* math.sin( math.rad( ship.rotation )) local posY = h \* math.cos( math.rad( ship.rotation )) newLaser.x = ship.x + posX newLaser.y = ship.y - posY newLaser.rotation = ship.rotation local function getLinearVelocity(rotation, velocity) local angle = math.rad(rotation) return { xVelocity = math.sin(angle) \* velocity, yVelocity = math.cos(angle) \* -velocity } end physics.addBody( newLaser, "dynamic" ) local newLaserLinearVelocity = getLinearVelocity(newLaser.rotation, 500) newLaser:setLinearVelocity(newLaserLinearVelocity.xVelocity, newLaserLinearVelocity.yVelocity) newLaser:toBack() end&nbsp;

This works for now.  There’s still some stuff to add, like screen wrapping and destroying the object after traveling through space for 1 second or so without hitting anything.

Thanks for your help.  Regards.