Page 1 of 1

math help

Posted: Wed Nov 08, 2017 1:05 pm
by PGUp
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 ?

Re: math help

Posted: Wed Nov 08, 2017 2:00 pm
by ivan
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)
x = math.cos(rads)*radius + offsetx
y = math.sin(rads)*radius + offsety

Re: math help

Posted: Wed Nov 08, 2017 2:20 pm
by Azhukar
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
	local radius = math.sqrt((px-cx)^2+(py-cy)^2)
	
	love.graphics.setColor(255,255,255,255)
	love.graphics.circle("line",cx,cy,radius)
	
	love.graphics.setColor(0,255,0,255)
	love.graphics.arc("line","open",cx,cy,radius/2,0,angle%(math.pi*2))
	
	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

Re: math help

Posted: Thu Nov 09, 2017 3:10 am
by TheHistoricApple
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

Re: math help

Posted: Thu Nov 09, 2017 3:24 am
by zorg
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.

Re: math help

Posted: Thu Nov 09, 2017 3:38 am
by TheHistoricApple
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!