Moving a scene/entity in a 3D world.

Started by
3 comments, last by L. Spiro 11 years, 8 months ago
I'm working on a space game where the player controls a spaceship, and I'm having some difficulty getting the math right.

What I'd like is a spaceship starting at the origin of the world with up in the positive Y, and forward into the screen, and the ability to move the ship forward, backward, left/right (strafe), turn to the right and left (rotation on the Y axis), and nose up and down (rotation on the X axis).

All this is working, at least to start. However, I'm having difficulty, suppose I start my game, and turn the ship to the left by 45 degrees so I'm still on the Z plane have neither angle up nor down, and simply am pointing off between negative Z (away from the screen), and negative X. Suppose the ship is given some forward acceleration, and then stopped say 50 units away from the origin.
Note: Up to this point, I have this working, the following is where it gets tricky.
Now suppose the ship is put nose down 45 degrees, and another 45 degrees to the left, so I'm facing directly in the negative X with my nose now pointing in the half-way between negative Y and negative X. Now, suppose I give the ship some velocity in the forward direction... Where should it end up?

So put another way, given a ship at an arbitrary 3D point, (x, y, z), a heading/direction, a velocity and a time(delta), how do I find the point where the ship should end up? Also, how should the heading/direction be stored?

Along the same lines, currently, when a user presses keys for rotating the ship on one of the axes, I increment a value, and rotate the ship on the corresponding axis. This works, and looks right, but I'm having trouble keeping track of where "heading/direction" is. How should this be recorded? Should I have three vectors for each axis or something?

I would greatly appreciate some detailed explanation or discussion of this. I've browsed and found some useful vector/matrix math stuff that's been helpful, but not quite enough to get me where I'm going. Any insight you could provide is welcome. Thanks in advance.
Advertisement
You can use a unit vector to store your facing direction.
If you want to turn, rotate the vector.
If you want to move, the velocity is speed*direction.

Another option is to use a quaternion, but I think starting with a vector is easier (more straightforward, anyway).

Also, how should the heading/direction be stored?

Via a normalized 3D vector.


So put another way, given a ship at an arbitrary 3D point, (x, y, z), a heading/direction, a velocity and a time(delta), how do I find the point where the ship should end up?

Take the previous 3D vector, multiply it by the speed (not velocity) and by time, then add it to the ship’s current 3D vector that represents its position. The position of the ship has now moved in the correct direction by the correct amount.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid


Take the previous 3D vector, multiply it by the speed (not velocity) and by time, then add it to the ship’s current 3D vector that represents its position. The position of the ship has now moved in the correct direction by the correct amount.

This makes sense, I think. You're saying that I have one vector at my world origin which gives the ships x, y, z, and after adding the speed*direction*time to the position vector, it's the ship's new position.



You can use a unit vector to store your facing direction.
If you want to turn, rotate the vector.

This I'm not so clear on. Is this unit vector centered about the world origin? I suppose it would be, right? If so, my difficulty now is in adjusting the direction of that unit vector. If the ship's "up" is in the positive Y direction, and the ship is facing the negative X, then when the user turns there ship to the left, I just rotate the facing direction about the Y axis, but I have trouble envisioning how it works when the "up" vector has X and Z components.

I think if I had an explanation and solution for the following problem, that might help.
The ship has a direction described by the vector D=(0.36, 0.59, 0.72)
The ship's "up" direction is described by U=(.62, .75, -.21)
When a user "turns" for time 't', (rotating only about the "up" axis, like turning on a flat road in a car), the ship rotates 'pi/10' radians in the direction turned.
If this ship turns for one time unit, what is its new direction vector?


Even describing the above problem though, I foresee more issues. It seems like the direction vector MUST be centered at the ship's origin because if the direction is centered about the world origin, when the ship turns, and there's a rotation about U by pi/10 at the origin, it won't "feel" like pi/10 for the ship. From the ship's point of view it will turn very quickly at some 't's and very slowly at others based on its position from the world origin.
So there must be some sort of translation between the ship's "reference frame" and the world's.
I'm brand new to all this, so as much as you can dumb down any insight, the better.

To give the above problem a little more practicality with reference to my previous paragraph monologue, consider the following:
The ship's position in the world's reference frame is P=(50, 35, 35).
The ship moves 30 units in direction D before it turns, what is its new position P'?
Now the ship turns for one unit of time and rotates about U by pi/10.
The ship moves another 30 units in it's new direction D'.
Where is the ship's position P'' now?

A detailed solution to this would be GREATLY appreciated. Thank you.
You are heavily overengineering this. There is nothing tricky about it.


This I'm not so clear on. Is this unit vector centered about the world origin?

It is a direction; it has no center point. This is like asking , “What is the center point of left?”.



When a user "turns" for time 't', (rotating only about the "up" axis, like turning on a flat road in a car), the ship rotates 'pi/10' radians in the direction turned.
If this ship turns for one time unit, what is its new direction vector?

D + Rotated amount. To combine rotations, look into quaternions or matrix rotation around axes.



The ship's position in the world's reference frame is P=(50, 35, 35).
The ship moves 30 units in direction D before it turns, what is its new position P'?

P + D * 30.


Now the ship turns for one unit of time and rotates about U by pi/10.
The ship moves another 30 units in it's new direction D'.
Where is the ship's position P'' now?

D += PI/10 (Matrix Rotation Around U).
P" = P' + D * 30.


There is nothing tricky, fancy, or non-trivial happening here.
Just add and multiply things.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

This topic is closed to new replies.

Advertisement