I have an object that is rotating continuously, and it fires bullets. I want the bullets to travel forward according to their direction.
physics.setGravity( 0, 0 )
fireBullets = function ( )
local bullet = display.newRect( sceneGroup,0,0, 40, 40 )
bullet:setFillColor( 0, 1, 0 )
local h = player.height
local posX = h * math.sin( math.rad( player.rotation ))
local posY = h * math.cos( math.rad(player.rotation ))
bullet.x = player.x + posX
bullet.y = player.y - posY
bullet.rotation = player.rotation
So far so good, the bullets appear with the exact rotation of the player.
local angle = math.rad( bullet.rotation )
local xDir = math.cos( angle )
local yDir = math.sin( angle )
physics.addBody( bullet, "dynamic" )
bullet:setLinearVelocity( xDir * 100, yDir * 100)
end
They don't move forward according to their direction, they seem to be moving towards their right. What's wrong with my calculation?