Page 1 of 1

sprite look to cursor

Posted: Tue Nov 10, 2009 10:50 am
by iammfa
Hi,
There is a sprite in the center of the screen, I'm trying make this sprite rotate to look forever to mouse cursor, I coded that and the sprite look to mouse cursor but once, after that stop rotating, this not the goal, the goal is sprite rotate forever to look to mouse cursor when it move.
this is my code:
main.lua
(341 Bytes) Downloaded 328 times
iammfa

Re: sprite look to cursor

Posted: Tue Nov 10, 2009 11:51 am
by kikito
You could use the update function to update mousex before each frame is drawn:

Code: Select all

function update(dt)
  mousex = love.mouse.getX( )
end
Another (possibly less correct) way is to set mousex inside your draw function:

Code: Select all

function draw()
  mousex = love.mouse.getX( )
  love.graphics.setBackgroundColor( 50, 50, 50 )
  love.graphics.draw(player, 400, 300, mousex)
end

Re: sprite look to cursor

Posted: Tue Nov 10, 2009 12:15 pm
by iammfa
it works well with mouse X, but when i do the same with mouse Y, LOVE gave me error ..!

Re: sprite look to cursor

Posted: Tue Nov 10, 2009 2:13 pm
by kikito
hi again,

I didn't understand your question correctly at the beginning - I thought you wanted to move the image, not to orientate it.

You have to calculate the angle - this involves some trigonometry. Mine is a bit rusty, but let me try to help you.

The very least you need is arctanget - wich in lua is provided by math.atan. This is a complicated function to use, since you would have to calculate a division, and also check for special cases (divisions by 0).

Fortunately there's also math.atan2, which handles these special cases (more info in http://lua-users.org/wiki/MathLibraryTutorial)

Now, enough teacher-speak. To your problem.

Let's say the center of your image is in x1, y1 and the mouse is on coordinates x2, y2. Then the angle between those two points is calculated like this in lua:

Code: Select all

  angle = math.atan2(y2-y1,x2-x1) * 57.2957795 --this last multiplation converts to degrees - remove if you are using LÖVE 0.6
Use the calculated angle instead of mousex as a parameter for draw, and your image should look to the mouse, forever. Also, I recommend you to use variables x1 and y1 instead of 400, 300 for drawing. If later you want, for example, to move the image with the keys, it will be easier.

Finally, and advice: upload your complete code every time you modify it and ask a question - it will be easier to understand and answer.

Regards!

Re: sprite look to cursor

Posted: Tue Nov 10, 2009 3:09 pm
by iammfa
may be i cause disturbance, forgive me, i still beginner..
i did what you learn me, but still error can you help me more..!
this my update file:
main.lua
(507 Bytes) Downloaded 539 times

Re: sprite look to cursor

Posted: Tue Nov 10, 2009 3:36 pm
by kikito
No need to ask for forgiveness, no worries.

Give this a try. I can't use it on my computer since I don't have 0.5 any more, so I haven't tested.

I've included comments with "kikito: " in order to explain the changes I made.

Code: Select all

-- include files
-- love.filesystem.include( math ) -- kikito: I think you don't need this in Love
-- load gfx player
function load()
  player = love.graphics.newImage("gfx/player.jpg")
  --player:setCenter( x1, y1 ) -- kikito: not needed - images are centered by default on love 0.5
                               -- for love 0.6 you would have to use player:setCenter(player.width/2, player.height/2)
end

function update(dt) -- kikito: this makes all the following "every frame"
  -- variables
  x1 = 400 -- kikito: replaces player.X = x1
  y1 = 300 -- kikito: replaces player.Y = y1
  x2 = love.mouse.getX( )
  y2 = love.mouse.getY( )
  angle = math.atan2(y2-y1,x2-x1) * 57.2957795 -- kikito: moved to here from the draw() function
end

-- bg color, player position
function draw() -- kikito: this draws a frame using the variables set up in "update"
  love.graphics.setBackgroundColor( 50, 50, 50 )
  love.graphics.draw(player, x1, y1, angle)
end

Re: sprite look to cursor

Posted: Tue Nov 10, 2009 4:54 pm
by iammfa
Many thanks, i tested it and worked well
thanks for your efforts

regards
iammfa

Re: sprite look to cursor

Posted: Tue Nov 10, 2009 4:58 pm
by kikito
My pleasure.

I challenge you now: Move your image with the cursor keys, while it keeps looking at the mouse cursor. :)

oh, the uglyness!

Posted: Wed Nov 11, 2009 12:36 am
by subrime
This code is terrible:

Code: Select all

 angle = math.atan2(y2-y1,x2-x1) * 57.2957795 
About the only thing that messes up code as much as a goto is using hardcoded numerical constants in situ. They should be named something useful and defined once in a common location.

However, in this case you really just need to use the function math.deg() like this:

Code: Select all

 angle = math.deg(math.atan2(y2-y1,x2-x1)) 

Re: sprite look to cursor

Posted: Wed Nov 11, 2009 8:02 am
by kikito
Thanks for pointing this out. It certainly looks cleaner.