![]() |
|
Rotating an entity with a center of rotation - Printable Version +- Team Spartan Cookies & Milk Forums (https://teamscm.co.uk) +-- Forum: Garry's Mod (https://teamscm.co.uk/forum-4.html) +--- Forum: Starfall (https://teamscm.co.uk/forum-7.html) +--- Thread: Rotating an entity with a center of rotation (/thread-2400.html) |
Rotating an entity with a center of rotation - Salira - 03-08-2017 I'm wondering how I would go about setting an entity's angles with a certain vector as its center of rotation. I want to have a ship that has rotating ramp doors, and setting the angles normally wouldn't work. RE: Rotating an entity with a center of rotation - Veegi - 03-08-2017 There are two ways to approach this. In some cases, holos for example, you could make an invisible "rotator" holo, parent the object you want to rotate, and rotate the rotator holo; not sure how well this works with props and stuff though, so that leads us to the other method: rotation matrices. You can get the location of the object you want to rotate in local coordinates relative to the rotation origin, rotate the local coordinates, and turn the rotated local coordinates back to world coordinates. Afterwards you'll also have to rotate the model, as the aforementioned process only changes it's position. To use a rotation matrix you could implement matrix operations to your code, but you can just do it in multiple steps with boring regular operators. I'll link the Wikipedia page for rotation matrices for you to get started.
RE: Rotating an entity with a center of rotation - Kosmos - 04-08-2017 I think the best way to do this is with localToWorld and worldToLocal (which are global functions, not only entity methods). Here's an example function which does this: Code: function rotateAroundPoint(entity, worldPos, rotAngle)
local rPos, rAng = worldToLocal(entity:pos(), entity:ang(), worldPos, Angle())
rPos, rAng = localToWorld(rPos, rAng, worldPos, rotAngle)
entity:setPos(rPos)
entity:setAng(rAng)
endFirst it calculates which relative position and angle the entity currently has. Then it implicitly (because this merely follows from not using the same local coordinate system when translating from and to world) rotates the position and angle. Finally those values are converted to world again and applied to the entity. Note that this code does basically what Veegi said but without needing a parent entity. This also means you have to do it frequently, so if you want some rotating ring, use a Hologram and it's setAngVel method - that's a very useful thing those holos have. RE: Rotating an entity with a center of rotation - Salira - 06-08-2017 (04-08-2017, 04:01 AM)Kosmos Wrote: I think the best way to do this is with localToWorld and worldToLocal (which are global functions, not only entity methods). Holy crap it's that simple? Thanks! (03-08-2017, 09:24 PM)Veegi Wrote: There are two ways to approach this. Thanks for this too, but Kosmos's method is just so much easier. |