align bullet to player offset

Real simple one here I guess.

I have a player that fires a projectile.

this works fine no worries.

I need to place the projectile when its spawned up by for example 20px so it dosnt spawn in the center of the player.

Now I tried

projectile.x = player.x

projectile.y = player.y -20

This obviously just offsets it to the x and y axis’ so dosnt work for me.

I checked out the setreferencepoint api and tried:

projectile:setReferencePoint(display.setBottomReferencePoint), no go again.

I could solve it by redrawing my sprite sheet but there has to be a simple way?

Any help greatly appreciated.

thank you

Hi bubblebobble,

I don’t get your problem exactly.

Do you want the offset be related to the shooting direction? Then a simple estimation with math.sin and math.cos will do it.

Or did I get it all wrong and that’s not the problem?

sorry I will be clearer.

I have a rotating player 40px x 40px for example, I have a bullet say 60px x 2px for example.

When I fire the bullet the bullets starting point is the physics centre of the object.

So the bullets back will appear 10px behind the player.

I just want to move the bullet up towards the front of the player by 10px so the bullet appears to start in the player not behind it.

I can rotate and fire in the direction no worries.

Thanks

What I did in my space shooter was to do a player:toFront() call after spawning the bullet so it appears to come from the player, not magically appearing in front of him.

I wonder that setting the reference point has no effect.

Did you set the reference point BEFORE you set the objects position?

Also you could try something like this:

[LUA]

local obj = display.newRect(0,0,10,60)

obj.yReference = 20

obj.x, obj.y = player.x, player.y

[/LUA]

Could not imagine why this should not work.

@tor,

blinkin heck, forgot about setting the Reference before placing the object, easy to miss , all sorted thank you for your help

Hi bubblebobble,

I don’t get your problem exactly.

Do you want the offset be related to the shooting direction? Then a simple estimation with math.sin and math.cos will do it.

Or did I get it all wrong and that’s not the problem?

sorry I will be clearer.

I have a rotating player 40px x 40px for example, I have a bullet say 60px x 2px for example.

When I fire the bullet the bullets starting point is the physics centre of the object.

So the bullets back will appear 10px behind the player.

I just want to move the bullet up towards the front of the player by 10px so the bullet appears to start in the player not behind it.

I can rotate and fire in the direction no worries.

Thanks

What I did in my space shooter was to do a player:toFront() call after spawning the bullet so it appears to come from the player, not magically appearing in front of him.

I wonder that setting the reference point has no effect.

Did you set the reference point BEFORE you set the objects position?

Also you could try something like this:

[LUA]

local obj = display.newRect(0,0,10,60)

obj.yReference = 20

obj.x, obj.y = player.x, player.y

[/LUA]

Could not imagine why this should not work.

@tor,

blinkin heck, forgot about setting the Reference before placing the object, easy to miss , all sorted thank you for your help