jump with a small rotation ??

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
loveu
Prole
Posts: 2
Joined: Wed Oct 29, 2014 3:40 pm

jump with a small rotation ??

Post by loveu »

hello Lovers,
am trying to make a little fish jump to the air and make a little rotation when its come back to the sea am using this code for this matter

Code: Select all

function love.load()
     angle  = 0
    food1 = {
     x = -50,
     y = 500,
     ySpeed = 0,
     xspeed = 1, -- the Speed we'll add to the Y movement
     gravSecond = 1.5, -- gravity amount
     fish1 = love.graphics.newImage("images/fish1.png"),
     rotate = math.pi,
     rotspeed =1/2,
     }

     setmetatable(food1,{__index =food})
     return food1
   end

 function love.update(dt)


   food1.rotate = food1.rotate+food1.rotspeed*dt

   frame = dt * 30 -- I do this just because I think better this way
   food1.ySpeed = food1.ySpeed + food1.gravSecond * frame -- We add gravity to the ySpeed
   food1.y = food1.y + food1.ySpeed * frame -- We add ySpeed to the player's y position
   if food1.y > 800 then -- Are we on the ground?
   food1.y = food1.y - food1.ySpeed * frame -- If we are, We undo that moving down
   food1.ySpeed = 0 -- The ySpeed is reset
  end
   food1.x = food1.x+food1.xspeed
   if food1.ySpeed == 0 then -- Should we be jumping, and can we?
   --This works because the ySpeed is only zero when we aren't already jumping.
      food1.ySpeed = -45 -- Make us add a negative, to move up

   end
  if food1.x > 800 then
      food1.x = 0
  end
end

function love.draw()



    love.graphics.draw(food1.fish1, food1.x, food1.y,food1.rotate)
      -- Draw it!
 end
but the problem is it keeps turning around and around

Image
how to make it rotate just when its comeback to the sea ???
please help
thanks
:neko:
Attachments
test.love
(7.75 KiB) Downloaded 108 times
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: jump with a small rotation ??

Post by micha »

You can calculate the angle from the x/y-velocity:

Code: Select all

food1.rotate = math.atan2(food1.yspeed, food1.xspeed)
This will always align the angle, such that the fish looks into the direction of the velocity.
loveu
Prole
Posts: 2
Joined: Wed Oct 29, 2014 3:40 pm

Re: jump with a small rotation ??

Post by loveu »

micha wrote:You can calculate the angle from the x/y-velocity:

Code: Select all

food1.rotate = math.atan2(food1.yspeed, food1.xspeed)
This will always align the angle, such that the fish looks into the direction of the velocity.
it works perfectly thank u very much micha
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests