can't figure out proper rotation for projectiles :-(

I’m stuck. i’m trying to get my projectile class to point in the direction it’s heading. it works for 1 axis, but not for the other. any help? in this example (look at setup), the projectiles that travel along the y axis (p1, and p2) are pointing the opposite direction. if I remove the rotate(-90), then they face the right direction, but p3 and p4 (x-axis travelers) end up pointing the opposite direction.

``````Projectile = class()

function Projectile:init(x, y, gfx, heading, speed, lifespan)
-- you can accept and set parameters here
self.x = x
self.y = y
self.gfx = gfx  --a string
self.speed = speed -- int rate
self.lifespan = lifespan
self.deathCounter = 0
self.explosionSize = {}
self.explosionSize.w, self.explosionSize.h = spriteSize("Tyrian Remastered:Space Ice 2")

self.explosionGraphicSequence = {
"Tyrian Remastered:Space Ice 2",
"Tyrian Remastered:Space Ice 4",
"Tyrian Remastered:Space Ice 6",
"Tyrian Remastered:Space Ice 3",
"Tyrian Remastered:Space Ice 8",
}

end

function Projectile:draw()
-- Codea does not automatically call this method
if( self.lifetime < self.lifespan ) then
pushMatrix()
translate( self.x, self.y )

--print( math.deg( self.heading:angleBetween( vec2(1,0) ) ) )
rotate( -90 )
rotate( math.deg( self.heading:angleBetween( vec2(1,0) ) ) )
spriteMode( CORNER )
sprite( self.gfx )
popMatrix()
--now update self.x/y for next frame
local v = vec2( self.x, self.y )
v = v + (self.heading * self.speed)
self.x = v.x
self.y = v.y
else
--trigger death counter (10 frames)
--use explosion graphic
if( self.deathCounter < 9 ) then
self.deathCounter = self.deathCounter + 1
local idx = math.floor( self.deathCounter/2 + 1 )
sprite( self.explosionGraphicSequence[idx],
self.x, self.y,
self.explosionSize.w, self.explosionSize.h )
end
end
end
function setup()
p1 = Projectile( WIDTH/2,
HEIGHT/2 ,
"Tyrian Remastered:Bullet Energy A",
vec2(0,1),
1,
80)
p2 = Projectile( WIDTH/2,
HEIGHT/2 ,
"Tyrian Remastered:Bullet Energy A",
vec2(0,-1),
1,
80)
p3 = Projectile( WIDTH/2,
HEIGHT/2 ,
"Tyrian Remastered:Bullet Energy A",
vec2(-1,0),
1,
80)
p4 = Projectile( WIDTH/2,
HEIGHT/2 ,
"Tyrian Remastered:Bullet Energy A",
vec2(1,0),
1,
80)
end

function draw()
background(255, 255, 255, 255 )
p1:draw()
p2:draw()
p3:draw()
p4:draw()
end
``````

Try this (without rotating -90)

``````rotate(-90- math.deg( self.heading:angleBetween( vec2(1,0) ) ) )
``````