math help

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
PGUp
Party member
Posts: 105
Joined: Fri Apr 21, 2017 9:17 am

math help

Post 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 ?
-
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: math help

Post 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
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: math help

Post 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
TheHistoricApple
Prole
Posts: 26
Joined: Sun Jan 15, 2017 6:46 am

Re: math help

Post 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
Last edited by TheHistoricApple on Thu Nov 09, 2017 3:37 am, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: math help

Post 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.
Me and my stuff :3True 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

Post 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!
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 35 guests