Team Spartan Cookies & Milk Forums
  • Issue Tracker
  • Donations
  • Search
  • Member List
  • Help

Register

Team Spartan Cookies & Milk Forums Garry's Mod Starfall v
« Previous 1 … 6 7 8 9 10 … 18 Next »
Rotating an entity with a center of rotation
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Linear Mode
Rotating an entity with a center of rotation
06-08-2017, 10:22 PM, (This post was last modified: 06-08-2017, 10:23 PM by Salira.)
#4
Salira Offline
Valued donator and member
****
Posts: 196
Threads: 78
Joined: May 2015
RE: Rotating an entity with a center of rotation
(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).
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) end
Explanation of what the code does:
First 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.

Holy crap it's that simple? Thanks!

(03-08-2017, 09:24 PM)Veegi Wrote: 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. Big Grin

Thanks for this too, but Kosmos's method is just so much easier.
Find
Reply
« Next Oldest | Next Newest »


Messages In This Thread
Rotating an entity with a center of rotation - by Salira - 03-08-2017, 06:59 PM
RE: Rotating an entity with a center of rotation - by Veegi - 03-08-2017, 09:24 PM
RE: Rotating an entity with a center of rotation - by Kosmos - 04-08-2017, 04:01 AM
RE: Rotating an entity with a center of rotation - by Salira - 06-08-2017, 10:22 PM

  • View a Printable Version
Forum Jump:


Users browsing this thread: 1 Guest(s)
Contact UsTeam Spartan Cookies & MilkReturn to TopLite (Archive) ModeMark all forums readRSS Syndication

Powered By MyBB, © 2002-2026 MyBB Group.