## math help

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
PGUp
Party member
Posts: 105
Joined: Fri Apr 21, 2017 9:17 am

### math help

so i wanna rotate something around a point, just like orbiting around it, i found some formulas.. but its just too long, i remembered a formula with only sin and cos, times it by radius and it works.. also maintaining the current position, but i forgot the formula already

Code: Select all

x = math.sin(math.rad(angle))

it will just go to the left side of the screen, it wont go to the middle even if i change the position, i dont know what to add to maintain its current position, help ?
-

ivan
Party member
Posts: 1655
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### Re: math help

The common way of moving a point (x,y) around an offset (offsetx, offsety) based on an angle:

Code: Select all

rads = math.rad(angle)
y = math.sin(rads)*radius + offsety

Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

### Re: math help

Here's a function that takes 2 points and rotates one around the other.

Code: Select all

local function rotatePoint(cx,cy,px,py,angle)
local s = math.sin(angle)
local c = math.cos(angle)
px,py = px-cx,py-cy
return px*c - py*s + cx,px*s + py*c + cy
end

function love.draw()
local cx,cy = 100,100 --center of rotation
local px,py = 150,100 --point that is being rotated
local angle = love.timer.getTime()
local rx,ry = rotatePoint(cx,cy,px,py,angle) --point after rotation

love.graphics.setColor(255,255,255,255)

love.graphics.setColor(0,255,0,255)

love.graphics.setColor(0,0,255,255)
love.graphics.line(cx,cy,px,py)
love.graphics.points(cx,cy,px,py)

love.graphics.setColor(255,0,0,255)
love.graphics.line(cx,cy,rx,ry)
love.graphics.points(rx,ry)
end

TheHistoricApple
Prole
Posts: 26
Joined: Sun Jan 15, 2017 6:46 am

### Re: math help

Think this is what you're looking for.

Code: Select all

--load
angle = 1 -- in degrees I use it as math.random(1,360) to get random starting spot

X, Y = ObjectInMiddleX + math.cos(angle) * DistanceYouWantObjectToBeFromMiddle, ObjectInMiddleY + math.sin(angle) * DistanceYouWantObjectToBeFromMiddle

--update
angle = angle + (SpeedYouWantToOrbit *dt)
--then you have to update the x, and y too
X, Y = ObjectInMiddleX + math.cos(angle) * DistanceYouWantObjectToBeFromMiddle, ObjectInMiddleY + math.sin(angle) * DistanceYouWantObjectToBeFromMiddle

Last edited by TheHistoricApple on Thu Nov 09, 2017 3:37 am, edited 1 time in total.

zorg
Party member
Posts: 3041
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: math help

TheHistoricApple wrote:
Thu Nov 09, 2017 3:10 am
Think this is what you're looking for.
-code-
You're mixing up the sin and cos though; x gets cos, y gets sin, like with the examples given above yours.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

TheHistoricApple
Prole
Posts: 26
Joined: Sun Jan 15, 2017 6:46 am

### Re: math help

zorg wrote:
Thu Nov 09, 2017 3:24 am
TheHistoricApple wrote:
Thu Nov 09, 2017 3:10 am
Think this is what you're looking for.
-code-
You're mixing up the sin and cos though; x gets cos, y gets sin, like with the examples given above yours.
You're right I fixed it, thanks!

### Who is online

Users browsing this forum: No registered users and 34 guests