sprite look to cursor
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
sprite look to cursor
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: iammfa
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: iammfa
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: sprite look to cursor
You could use the update function to update mousex before each frame is drawn:
Another (possibly less correct) way is to set mousex inside your draw function:
Code: Select all
function update(dt)
mousex = love.mouse.getX( )
end
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.
Re: sprite look to cursor
it works well with mouse X, but when i do the same with mouse Y, LOVE gave me error ..!
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: sprite look to cursor
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:
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!
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
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.
Re: sprite look to cursor
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:
i did what you learn me, but still error can you help me more..!
this my update file:
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: sprite look to cursor
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.
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.
Re: sprite look to cursor
Many thanks, i tested it and worked well
thanks for your efforts
regards
iammfa
thanks for your efforts
regards
iammfa
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: sprite look to cursor
My pleasure.
I challenge you now: Move your image with the cursor keys, while it keeps looking at the mouse cursor.
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.
oh, the uglyness!
This code is terrible:
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.atan2(y2-y1,x2-x1) * 57.2957795
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))
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: sprite look to cursor
Thanks for pointing this out. It certainly looks cleaner.
When I write def I mean function.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot], Majestic-12 [Bot], slime and 7 guests