1
\$\begingroup\$

Say I have worlds and levels. Each world contains multiple levels. Levels have their own coordinate system, centered around their pivot point (assume its centre, for example). These level coordinates are expressed in floats.

Each level have has int64 coordinate.

Now I have game objects which should seamlessly travel between levels in the world. How can I transition between coordinates inside these levels?

\$\endgroup\$
  • \$\begingroup\$ Do your levels overlap? \$\endgroup\$ – Kromster says support Monica Dec 25 '16 at 6:44
  • \$\begingroup\$ No, but it is possibility. It's first time I try to do something like this, so I'm looking for any pointers, especially math, since I don't really know what to even look for. \$\endgroup\$ – Łukasz Baran Dec 25 '16 at 6:58
  • \$\begingroup\$ Then I can only advise to start with something that works and build from there \$\endgroup\$ – Kromster says support Monica Dec 25 '16 at 7:42
  • \$\begingroup\$ dropbox.com/s/bpt50hej4chiwna/grid.png?dl=0 Thick lines are boundaries of levels, thinner lines are coordinates within levels. Levels have coordinates like 1,1 ; 1,2 ; 2,1 ; etc. \$\endgroup\$ – Łukasz Baran Jan 11 '17 at 23:25
3
\$\begingroup\$

What you have is the basics of 3D math, without any rotation. In 3D, we generally refer to an object's position as its translation, and set of things that describe its position (its translation, and its rotation) in space as the transform. In this case, since we are not talking about rotated objects, then we can assume that an object's transform is the same as its translation.

Just to illustrate, let's say we have 2 levels, one called Level A, and one called Level B. Each of those Levels has a position in the world, that is the level's world transform. You have a bullet, we'll call it Bullet 1, and that Bullet resides in Level A at some position. Bullet 1's position relative to level A is called the local transform.

The neat thing about transforms is that you can apply a transform to another transform, and you will get a new transform out that is a combination of the two. Let's make a simple example, let's say Level A's world transform is (10, 10). This means it is 10 units away from the world on the X axis, and 10 units away on the Y axis. Level B will have a world transform of (5, 1). Now let's say that Bullet 1 is inside Level A, and inside Level A, Bullet 1's position is (2, 3). We can now say that Bullet 1's local transform is (2, 3). What do we do if we want to find Bullet 1's position in the world? This is known as finding Bullet 1's world transform. Well, Bullet 1 is in Level A, and the center of Level A is at (10, 10) in the world, and Bullet 1's position relative to the center of Level A is (2, 3), so we can just add them together. In math terms, this is "Applying the world transform of Level A to the local transform of Bullet 1", and what you get out of this operation is "The world transform of Bullet 1", or in English, the position of Bullet 1 in the world.

Phew, ok, so one more concept before we go on. When we apply the world transform of Level A to the local transform of Bullet 1, we add them together. What if we had the world transform of Bullet 1 and we want to find the local transform of Bullet 1 relative to Level A (the position we started with!)? Why, it should be obvious, we subtract the numbers we just added, in other words, we subtract the world transform of Level A from the world transform of Bullet 1. But we only know how to apply transforms, and that is adding! Well, we can fix that, instead of subtracting a number, we can just add the negative number, and this does the same thing. The negative version of the world transform of Level A is (-10, -10), in math terms, this is called the inverse transform.

So now we have all the tools we need to solve this problem. I've explained them in a way that may seem complicated, but it's actually some very simple math, but these concepts are important in 3D and game development, and will help things you read make more sense, so please bear with me. Let's go through all the things we have first. World Transform of Level A: (10, 10) World Transform of Level B: (5, 1) Local Transform of Bullet 1 relative to Level A: (2, 3)

What we want to find is Local Transform of Bullet 1 relative to Level B.

First, we need to get the world transform of Bullet 1, or, where the bullet is in the world. To get that, we will apply the world transform of Level A to Bullet 1: (10, 10) + (2, 3) = (12, 13). So now we have World Transform of Bullet 1: (12, 13)

To find where it is relative to Level B, we need to find the inverse world transform of Level B, also known as its negative, (-5, -1). If we have the World Transform of Bullet 1, and the Inverse World Transform of Level B, then adding those together will give us the Local Transform of Bullet 1 relative to Level B. Now we have: Local Transform of Bullet 1 relative to Level B: (12, 13) + (-5, -1) = (8, 12)

Ok, we finally have what we started out to get. We take the position of Bullet 1 relative to Level A, and we add in Level A's position relative to the world. This gives us where Bullet 1 is in the world. Then we want to find where it is relative to Level B, so we subtract the world position of Level B from the world position of Bullet 1 and we're left with the position of Bullet 1 relative to Level B.

Hopefully this helps!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.