Relative Positioning

Started by
4 comments, last by Firestryke31 15 years, 9 months ago
Alright, I have two objects, let's call them A and B. They each contain a position, rotation, and scale (PRS for simplicity) to describe themselves in the world. Let's say the objects are set up as follows: Note: rotations are in degrees I want to be able to figure out two things: First, Given both objects' world PRS, I want to find object B's relative PRS Second, Given object A's world PRS and object B's relative PRS, I want object B's world PRS. The more simple you explain the math, the more likely I'll understand it. Assume I know only the basics about trig. For those that want to know why, I want to take an object that's in world coordinates, and make it a child of another object while maintaining it's actual PRS.
Advertisement
For position, here is an example of relative positions using the locations you have

b.x = a.x + 4
b.y = a.y
b.z = a.z + 4

You use A's position as the base, then add/subtract/multiply to get B's position.
But what about for arbitrary values for any of the PRS components? Like if object A's rotation were 26 degrees instead of 0? Or if it's scale.x were 1.3? Keep in mind, I also need relative rotation and scaling.

Edit: An example
Let's say I'm playing Super Smash Brothers Brawl. I find the fan item and pick it up. Then let's say I get the growth mushroom. The fan scales with me, rotates with me, and moves with me because it is positioned and whatnot relative to me. Then let's say I drop it. The fan no longer moves, rotates, or scales with me, but it stays where I drop it because it is now positioned relative to the world. I want to figure out the transition from object space to world space and back using my PRS structure.
What I would do is simply have a variable for the parent object. So like your example, the parent would be the player. If there is a parent, the position and scale would be relative to the player. I would also keep track of the old position. So the object's position would be relative to the player's hand. So we have the actual position and the old position. The old position gets updated just like the actual position relative to the player. But when there is no parent, we set set the actual position with the old position, which doesn't get updated anymore. let me try to explain it with code...

Entity parent;Coord oldPositionCoord position// This is called when there is no longer a parent entitylostParent(){   position = oldPosition;}// This is called every frameupdatelocation(){  if(parent)  {     position.x = parent.x + 5     ...     oldPosition = position;  }  else  {     calculate gravity etc. here  }}
....
/facepalm

I just realized the renderer does the object space to world space conversion every frame, and I can just ask that for the derived PRS data. There's half of the problem solved.

But that still leaves the problem of getting the world space to object space conversion. Your idea would have worked with little modification were it not for the fact that I use the data directly in the renderer's scene graph.
Alright, I know this is a double-post, but I think enough time has passed, and that this is significantly different enough from the other post to deserve it.

I finally got around to sitting down with a sheet of graph paper to come up with a simple way of doing the conversion.

Step 1: Subtract A's position from B's position.
Step 2: Rotate B's position by the opposite of A's rotations.
Step 3: Add opposite of A's rotations to B's rotations.
Step 4: Multiply B's position by the inverse of A's scales.
Step 5: Multiply B's scales by the inverse of A's scales.

Does this do what I want it to do? It was the first thing I came up with that looked right, so I'm hoping it's correct.

This topic is closed to new replies.

Advertisement