## Moving to mouse.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
rokit boy
Party member
Posts: 198
Joined: Wed Jan 18, 2012 7:40 pm

### Moving to mouse.

mouse.x and mouse.y represent the mouse position
player.x and player.y represents the player position

My code:

Code: Select all

posx = mouse.x - player.x;
posy = mouse.y - player.y;
a = atan2(posy,posx);

a is the direction in radians >_<

I got a couple of questions.

1, This is in radians, how can I change it to degrees?
2, how can I make my character move in a direction in degrees?
3, Is this correct? I suck at maths
4, If it is correct, is there a better way?
u wot m8

trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

### Re: Moving to mouse.

Don't know why you'd do all that work converting to degrees and back, but the functions are there if you really need to.

Code: Select all

love.load = function()
vector = {
length = function(v) return math.sqrt((v.x * v.x) + (v.y * v.y)) end,
normal = function(v) l = vector.length(v); return {x = v.x / l, y = v.y / l} end,
vtor = function(v) return math.atan2(v.y, v.x) end,
vtod = function(v) return math.atan2(v.y, v.x) * (180 / math.pi) end,
rtov = function(r) return{x = math.cos(r), y = math.sin(r)} end,
dtov = function(d) return vector.rtovec(d * (math.pi / 180)) end,
}
player = {x = 64, y = 64, speed = 128}
playertomouse = function() return {x = love.mouse.getX() - player.x, y = love.mouse.getY() - player.y} end
end

love.update = function(dt)
direction = vector.normal(playertomouse())
player.x = player.x + (direction.x * player.speed) * dt
player.y = player.y + (direction.y * player.speed) * dt
end

love.draw = function()
love.graphics.rectangle('fill', player.x, player.y, 16, 16)
end
Disclaimer : I suck at math too, so there's probably at least one dumb mistake in there.

T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

### Re: Moving to mouse.

rokit boy wrote:mouse.x and mouse.y represent the mouse position
player.x and player.y represents the player position

My code:

Code: Select all

posx = mouse.x - player.x;
posy = mouse.y - player.y;
a = atan2(posy,posx);

a is the direction in radians >_<

I got a couple of questions.

1, This is in radians, how can I change it to degrees?
2, how can I make my character move in a direction in degrees?
3, Is this correct? I suck at maths
4, If it is correct, is there a better way?
This looks fine. Converting from radians to degrees is done like this

Code: Select all

rads = math.atan2(...) --something in radians

However, you probably don't want to do that. If you want to move the character in a direction, use math.sin and math.cos, and both of them use radians. Do it like this

Code: Select all

speed = 100

posx = mouse.x - player.x;
posy = mouse.y - player.y;
a = atan2(posy,posx);
dx = speed*dt*math.cos(a)
dy = speed*dt*math.sin(a)

player.x = player.x + dx
player.y = player.y + dy


vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

### Re: Moving to mouse.

T-Bone wrote:Converting from radians to degrees is done like this (code)
Ahem: math.deg()
T-Bone wrote:Do it like this

Code: Select all

posx = mouse.x - player.x;
posy = mouse.y - player.y;
a = atan2(posy,posx);
dx = speed*dt*math.cos(a)
dy = speed*dt*math.sin(a)
(...)
It's better - because faster and IMO easier to read (and vector math is superior to trigonometry in general) - to use vector math for this. Trubblegum already showed you how, but here's a more verbose version:

Code: Select all

posx = mouse.x - player.x
posy = mouse.y - player.y
local len = math.sqrt(posx*posx + posy*posy)
dx = posx/len * speed * dt
dy = posx/len * speed * dt
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine

rokit boy
Party member
Posts: 198
Joined: Wed Jan 18, 2012 7:40 pm

### Re: Moving to mouse.

Ok thanks, how do I make the player move in that direction?
u wot m8

trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

### Re: Moving to mouse.

rokit boy wrote:Ok thanks, how do I make the player move in that direction?
You're kidding, right?

rokit boy
Party member
Posts: 198
Joined: Wed Jan 18, 2012 7:40 pm

### Re: Moving to mouse.

trubblegum wrote:
rokit boy wrote:Ok thanks, how do I make the player move in that direction?
You're kidding, right?
Oh osrry, didn't read all of t-bones post.
u wot m8

rokit boy
Party member
Posts: 198
Joined: Wed Jan 18, 2012 7:40 pm

### Re: Moving to mouse.

Ok I quickly whipped this up:

Code: Select all

function love.load()
det = 0

player = {x = 10, y = 10, width = 32, height = 32}
mouse = {x = love.mouse.getX(), y = love.mouse.getY()}

speed = 100

posx = mouse.x - player.x;
posy = mouse.y - player.y;
a = math.atan2(posy,posx);
dx = speed*det*math.cos(a)
dy = speed*det*math.sin(a)

player.x = player.x + dx
player.y = player.y + dy
end

function love.draw()
love.graphics.rectangle("fill",player.x,player.y,player.width,player.height)
end

function love.update(dt)
det = dt
end

function love.mousepressed(x, y, button)
if button == "l" then
posx = mouse.x - player.x;
posy = mouse.y - player.y;
a = math.atan2(posy,posx);
dx = speed*det*math.cos(a)
dy = speed*det*math.sin(a)

player.x = player.x + dx
player.y = player.y + dy
end
end

Oh god I'm tired.
I really need m8y code to wrk..
u wot m8

trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

### Re: Moving to mouse.

rokit boy wrote:Oh god I'm tired.
Then go to bed.
You are wasting your time (and ours) on problems (not to mention creating problems that don't exist) which you could easily solve if you were rested.

### Who is online

Users browsing this forum: Google [Bot], raidho36, Sky_Render, steVeRoll and 20 guests