Moving sprite toward mouse
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 11
- Joined: Fri Jun 04, 2010 1:11 am
Moving sprite toward mouse
I'm not quite sure how to do this. I've tried all I can and nothing seems to work. It must be some kind of math that I am not familiar with?
- TechnoCat
- Inner party member
- Posts: 1611
- Joined: Thu Jul 30, 2009 12:31 am
- Location: Milwaukee, WI
- Contact:
Re: Moving sprite toward mouse
If you split it into X and Y then it becomes very easy algebra. Just test and move accordingly.danlthemanl wrote:I'm not quite sure how to do this. I've tried all I can and nothing seems to work. It must be some kind of math that I am not familiar with?
However, if you want a constant speed, you will need to use some linear algebra. Finding the x and y component from mouse to sprite and then getting the normalized vector of that.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Moving sprite toward mouse
In pseudocode:
Code: Select all
image.x = image.x + difference between mouse_x and image.x times a factor < 1
image.y = image.y + difference between mouse_y and image.y times a factor < 1
Help us help you: attach a .love.
-
- Prole
- Posts: 11
- Joined: Fri Jun 04, 2010 1:11 am
Re: Moving sprite toward mouse
I'm still not getting it. Whatever I do the sprite always moves in an angle, i can't get it to move toward the mouse!
this is what i have:
and it just moves at an angle.
this is what i have:
Code: Select all
-- In load
PlayerX = 400
PlayerY = 300
PlayerVel = -0.2
if love.mouse.isDown("l") then
mouseX = love.mouse.getPosition(x)
mouseY = love.mouse.getPosition(y)
diffX = PlayerX / mouseX - 1
diffY = PlayerY / mouseY - 1
PlayerX = PlayerX + diffX * PlayerVel
PlayerY = PlayerY + diffY * PlayerVel
end
- tentus
- Inner party member
- Posts: 1060
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
- Contact:
Re: Moving sprite toward mouse
Change
to
love.mouse.getPosition() returns to values in a specific order, you can't tell it to give you a single value.
Code: Select all
mouseX = love.mouse.getPosition(x)
mouseY = love.mouse.getPosition(y)
Code: Select all
mouseX,mouseY = love.mouse.getPosition()
Kurosuke needs beta testers
Who is online
Users browsing this forum: Bing [Bot] and 7 guests