Orbit object around another object using mouse position

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
xDVexan
Prole
Posts: 6
Joined: Sun Jan 27, 2019 1:33 am

Orbit object around another object using mouse position

Post by xDVexan »

Hello everyone, I just got back into lua/love2d development and I had a question.

I'm trying to make an object orbit around another object(player) using the mouse position. I attached a good example of this. You need to be on windows to run it since it's an exe. Create a level and look at the object orbiting around the player. I don't have a clue how this is done and it would be amazing if someone knows how to. Thanks :)
Attachments
citizen_of_nowhere-7.0.0-pre-alpha.zip
(39.09 MiB) Downloaded 209 times
Lucio, Head Developer
Haecia
Official Haecia Website
User avatar
4vZEROv
Party member
Posts: 126
Joined: Wed Jan 02, 2019 8:44 pm

Re: Orbit object around another object using mouse position

Post by 4vZEROv »

Trigonometry friend.

Code: Select all

function orbit(x,y, tx, ty, distance)
    local _x, _y = tx - x, ty - y
    local angle = math.atan2(_y, _x)
    return math.cos(angle)*distance + x, math.sin(angle)*distance + y
end

local a     = {x = love.graphics.getWidth()/2, y = love.graphics.getHeight()/2}
local mouse = {x = 0, y = 0}
local point = {x = 0, y = 0}

function love.update(dt)
    mouse.x, mouse.y = love.mouse.getPosition()
    point.x, point.y = orbit(a.x, a.y, mouse.x, mouse.y, 100)
end

function love.draw() 
    love.graphics.setColor(1,0,0)
    love.graphics.circle("line", a.x, a.y, 10)
    love.graphics.circle("line", mouse.x, mouse.y, 10)
    love.graphics.setColor(1,1,1)
    love.graphics.circle("line", point.x, point.y, 10)
end
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Orbit object around another object using mouse position

Post by pgimeno »

I don't have MSWindows, and anyway I wouldn't execute a random binary. A gif or a video would have been easier.

But I just wanted to offer an improvement over what 4vZEROv has posted. Calculating an angle just to find its sine and cosine is just a wasteful method of normalizing a vector, for which you don't need trigonometry. Here's a shorter one:

Code: Select all

function orbit(x, y, tx, ty, distance)
    local _x, _y = tx  -x, ty - y
    local length = math.sqrt(_x^2 + _y^2)
    return _x / length * distance + x, _y / length * distance + y
end
(edited to fix typo y -> _y)
Last edited by pgimeno on Sun Jun 09, 2019 12:19 pm, edited 1 time in total.
User avatar
4vZEROv
Party member
Posts: 126
Joined: Wed Jan 02, 2019 8:44 pm

Re: Orbit object around another object using mouse position

Post by 4vZEROv »

pgimeno in the game he gave, the object orbiting stay at the same distance all the time (see webm), I don't think you can get this result without normalizing the angle ?
Attachments
output.webm
(278.02 KiB) Not downloaded yet
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Orbit object around another object using mouse position

Post by pgimeno »

Sorry, I made a typo. I wrote _x^2 + y^2 and the second one should be _y, not y.

The formula I gave also needs a check for a zero length, in case the mouse is right on the centre, but it is working otherwise; there's no need to calculate an angle.

Keeping the same distance is done by normalizing the vector, which has the same result as the sine/cosine of the arctangent.
xDVexan
Prole
Posts: 6
Joined: Sun Jan 27, 2019 1:33 am

Re: Orbit object around another object using mouse position

Post by xDVexan »

Thanks guys!
Lucio, Head Developer
Haecia
Official Haecia Website
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 41 guests