Luke Stirk
Luke Stirk

Reputation: 55

CoronaSDK rotating 3d sprite

I am making a tower defense game and I have a sprite thats kind of 3D in the angle it has been rendered. I have an issue that I cannot figure out how to solve.

Image

I need to rotate the sprite smoothly to face the enemy it is firing at. At the moment it gets the angle between the turret and the enemy and sets the sprite for that angle, so if the angle was 140 degrees i would play the 140 degrees firing sequence. The problem with this is it will jump straight to that angle so if the last place the turret fired was at 270 degrees and the next enemy is at 120 degrees it will jump straight to that angle.

Any ideas how I can rotate the turret every 5 degrees between the last played sequence and the sequence for the next angle before the tower begins firing? So last played is 270 i need to go to 140 so i would play the frames for 265, 260, 255.......150, 145, 140

Upvotes: 0

Views: 1398

Answers (1)

Egor Skriptunoff
Egor Skriptunoff

Reputation: 23737

current_angle = 270

-- when new target appears
local target_angle = 140
local delta = (target_angle - current_angle + 180) % 360 - 180
-- Now: -180 <= delta < 180
step = delta < 0 and -5 or 5
number_of_steps = delta / step
ready_to_fire = false

-- inside "draw" function
ready_to_fire = number_of_steps == 0
if not ready_to_fire then
   current_angle = (current_angle + step) % 360
   number_of_steps = number_of_steps - 1
   -- draw frame for "current_angle"
end

Upvotes: 1

Related Questions