sprite look to cursor

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
iammfa
Prole
Posts: 5
Joined: Sat Apr 11, 2009 2:47 pm

sprite look to cursor

Post 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 346 times
iammfa
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: sprite look to cursor

Post 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
When I write def I mean function.
iammfa
Prole
Posts: 5
Joined: Sat Apr 11, 2009 2:47 pm

Re: sprite look to cursor

Post by iammfa »

it works well with mouse X, but when i do the same with mouse Y, LOVE gave me error ..!
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: sprite look to cursor

Post 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!
Last edited by kikito on Tue Nov 10, 2009 3:23 pm, edited 1 time in total.
When I write def I mean function.
iammfa
Prole
Posts: 5
Joined: Sat Apr 11, 2009 2:47 pm

Re: sprite look to cursor

Post 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 576 times
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: sprite look to cursor

Post 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
When I write def I mean function.
iammfa
Prole
Posts: 5
Joined: Sat Apr 11, 2009 2:47 pm

Re: sprite look to cursor

Post by iammfa »

Many thanks, i tested it and worked well
thanks for your efforts

regards
iammfa
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: sprite look to cursor

Post by kikito »

My pleasure.

I challenge you now: Move your image with the cursor keys, while it keeps looking at the mouse cursor. :)
When I write def I mean function.
User avatar
subrime
Citizen
Posts: 76
Joined: Thu Nov 13, 2008 6:18 pm
Location: Australia

oh, the uglyness!

Post 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)) 
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: sprite look to cursor

Post by kikito »

Thanks for pointing this out. It certainly looks cleaner.
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Majestic-12 [Bot], slime and 7 guests