Code: Select all
speed = 10
radius = 500
angle = 0
...
function update(dt)
angle = angle+dt*speed
local t = angle
vx = radius*math.sin(t) -- I thought it was cos, but one source said sine :P
vy = radius*math.cos(t)
end
Code: Select all
speed = 10
radius = 500
angle = 0
...
function update(dt)
angle = angle+dt*speed
local t = angle
vx = radius*math.sin(t) -- I thought it was cos, but one source said sine :P
vy = radius*math.cos(t)
end
Code: Select all
omega = (2*math.pi)/4 -- one rotation per 4 seconds
v = 400 -- velocity 400 pixels per second
x, y, angle = 0, 0, 0
function update(dt)
-- do movement:
x = x + v*dt*math.cos(angle)
y = y + v*dt*math.sin(angle)
-- do rotation:
angle = angle + omega*dt
end
Code: Select all
-- v omega r
-- 400 1 400
-- 300 1 300
-- 200 1 200
-- 200 0.5 800
-- 100 0.5 200
-- 50 0.5 100
-- 50 0.25 200
-- 50 0.125 800
-- 25 0.25 100
If you want the speed in pixels per second and the radius is in pixels, divide the speed by the radius:Gunroar:Cannon() wrote: ↑Wed Jun 15, 2022 9:23 am I'm trying to do circular velocity. My implementation after some research:Code: Select all
speed = 10 radius = 500 angle = 0 ... function update(dt) angle = angle+dt*speed
Code: Select all
angle = angle+dt*speed/radius
Woah, How does that even workpgimeno wrote: ↑Wed Jun 15, 2022 7:28 pm If you want the speed in pixels per second and the radius is in pixels, divide the speed by the radius:Code: Select all
angle = angle+dt*speed/radius
Maths
Maybe...just maybe...Maybe that gives you a first hint of why maths and
physics people like radians better than degrees
Gunroar:Cannon() wrote: ↑Thu Jun 16, 2022 10:44 am Oh, sorry for the double post, but I was fiddling around with it and it works well but then now I'm wondering how to make it so that it orbits around a point/other object at all times?
Code: Select all
time = 0 -- time
cX, cY = 400, 300 -- center position
radius = 250 -- orbit position
omega = 0.1 -- angular velocity
phi = math.pi/2 -- starting angular position
length = 20
function love.update (dt)
time = time + dt
end
function love.draw ()
local posX = cX + radius*math.cos(time*omega + phi)
local posY = cY + radius*math.sin(time*omega + phi)
local direction = time*omega + phi + math.pi/2 -- perpendicular to radius
local dx = length*math.cos(direction)
local dy = length*math.sin(direction)
love.graphics.line (posX, posY, posX+dx, posY+dy)
end
Code: Select all
Width, Height = love.graphics.getDimensions( )
cX, cY = Width/2, Height/2
Planets = {}
function addPlanet (radius, phi, omega)
local t = love.timer.getTime()
local planet =
{
omega=omega,
radius=radius,
phi=phi,
x = cX + radius*math.cos (t*omega + phi),
y = cY + radius*math.sin (t*omega + phi),
}
table.insert (Planets, planet)
end
addPlanet (15, 2*math.pi*math.random(), 100/15)
addPlanet (30, 2*math.pi*math.random(), 100/30)
addPlanet (50, 2*math.pi*math.random(), 100/50)
addPlanet (100, 2*math.pi*math.random(), 100/100)
addPlanet (150, 2*math.pi*math.random(), 100/150)
addPlanet (200, 2*math.pi*math.random(), 100/200)
addPlanet (300, 2*math.pi*math.random(), 100/300)
function love.update(dt)
local t = love.timer.getTime()
for i, planet in ipairs (Planets) do
planet.x = cX + planet.radius*math.cos (t*planet.omega + planet.phi)
planet.y = cY + planet.radius*math.sin (t*planet.omega + planet.phi)
end
end
function love.draw()
love.graphics.setColor(1,1,0)
love.graphics.circle ('fill', cX, cY, 5)
love.graphics.setColor(1,1,1)
for i, planet in ipairs (Planets) do
love.graphics.circle ('line', cX, cY, planet.radius)
love.graphics.circle ('fill', planet.x, planet.y, 5)
end
end
1. Engage brain.Gunroar:Cannon() wrote: ↑Thu Jun 16, 2022 10:44 am Oh, sorry for the double post, but I was fiddling around with it and it works well but then now I'm wondering how to make it so that it orbits around a point/other object at all times?
Oh, sorry. You see...errmm...the thing is...
Users browsing this forum: Bing [Bot] and 92 guests