How does one make an entity "orbit" another one on a specific axis? While I know how to do it physics wise, I have no idea how to implement it into the code, could someone give me some advice?
LocalToWorld and some trigonomitry.
Could you explain why do I need to use LocalToWorld?
So you can set the position/angle relative to the entity.
Thanks for help guys!
I've managed to make a little step forward, but I'm a little bit lost with the LocalToWorld...
This is my bit of code making the prop(v) "orbit" around the star(self).
At this very moment it is spinning around but not essentially around the star, but at some offset.
I have no idea what I'm doing with LocalToWorld, so could someone help me a bit with this?(I'm sorry)
I have spent too much time on this orbiting thing, my head hurts already.
[CODE]
local pos = self:GetPos()
local vpos = v:GetPos()
local difference = pos - vpos
local length = difference:Length()
local f = 5 * (CurTime())/2
local R = (self:LocalToWorld(pos) - vpos):Length()
local x = R * math.cos(f)
local y = R * math.sin(f)
if ( length <= 2*self.Scale ) then
vphys:AddVelocity(Vector(x,y,0))
end
[/CODE]
All positions are simply vectors measured from a specific point, world vectors are measured from the world origin, local vectors are measured from a specific entites position.
Lets say you have an object at world vector (10, 10, 10), ent:LocalToWorld(Vector(5,0,0)) would equal Vector(15, 10, 10), ent:WorldToLocal(15, 10, 10) would be Vector(5,0,0)
[editline]18th August 2012[/editline]
Try
[lua]local pos = self:GetPos()
local vpos = v:GetPos()
local difference = pos - vpos
local length = difference:Length()
local f = 5 * (CurTime())/2
local R = length
local x = R * math.cos(f)
local y = R * math.sin(f)
if ( length <= 2*self.Scale ) then
vphys:AddVelocity(self:LocalToWorld(Vector(x,y,0)))
end[/lua]
Thanks for the explanation of vectors Mate!
Sadly the code you provided makes the orbiting entity spiral high into the air away from the star.
Also, the length check before applying the force is not that much important at the moment, it pretty much messes up the whole thing.
Oh I see why, it's because of the velocity, I think you need to apply the velocity along the tangent to the circle at (x, y)
How does one do this?
Sorry, you need to Log In to post a reply to this thread.