Is there a way I can rotate a sprite to face where I touched?
So my sprite rocket can fight enemies in 360 degrees.
Is there a way I can rotate a sprite to face where I touched?
So my sprite rocket can fight enemies in 360 degrees.
Try this
function setup()
tpos=vec2(WIDTH/2,HEIGHT/2)
shippos=vec2(WIDTH/2,HEIGHT/2)
end
function draw()
background(0)
pushMatrix()
translate(shippos.x,shippos.y)
rotate(angleBetween(shippos,tpos))
spriteMode(CENTER)
sprite("Space Art:Red Ship",0,0)
popMatrix()
-- do the rest of your drawing here
end
function touched(touch)
tpos=vec2(touch.x,touch.y)
end
function angleBetween(ptA,ptB)
local angle=math.deg(math.atan2(ptA.y-ptB.y,ptA.x-ptB.x))
angle = angle + 90
return angle
end
@Coder Instead of hand-coding an angleBetween function (using trigonometry), you can just use vec2.angleBetween.
rotate(shippos:angleBetween(tpos))
I thought that got the angle between two directional vectors but you could have vec2(0,1):angleBetween(tpos) but that would only work if the ship was a the origin (you could use translate)