Page 1 of 1

Question about world space and local space movement

Posted: April 21st, 2021, 9:27 am
by rubixcube6
Scene.jpg
I have been working on combining what I have learned from Wituz's youtube series, and code from Lameguy64's TMD model viewer example. I'm at a point where I can easily load in a model, move it around, rotate it in world space. What I can't seem to figure out is how to move an object along its own forward axis (in this case it's z axis). There is a bit of code that moves the camera like this in Lameguy64's TMD model viewer example, however, it acts very odd if the camera is rotated on its z-axis.

I put together a very simple project to illustrate my question. The goal of this project is to make the shuttle move forward by pressing X on the controller. you can already steer it left and right by using the D-Pad.

Re: Question about world space and local space movement

Posted: April 22nd, 2021, 11:37 am
by rubixcube6
To anyone seeing this later, the fine folks from the PSXDEV discord helped me solve the problem! Download the working attachment below to see the code in context.

Here is the function

Code: Select all

VECTOR pos;
MATRIX omtx;
VECTOR returnPos;
VECTOR Translate (SVECTOR rot, int x, int y, int z) {

	pos.vx = x;
	pos.vy = y;
	pos.vz = z;

    RotMatrix(&rot, &omtx);

	ApplyMatrixLV(&omtx, &pos, &returnPos);

	return returnPos;

}
And here is how to use it in your main loop, or Update function.

Code: Select all

//A vector to store your new position
VECTOR newVec;

//This takes the object's rotation and a local direction.
//In this case the Z axis is the object's forward direction.
//So 0, 0, 1 is forward
//0, 1, 0 is up
//and 1, 0, 0 is sideways
//you can replace the 1 with the speed you want your object to move

newVec = Translate(shuttle.rotation, 0, 0, 1 * ONE/500);
yourObject.position.vx += newVec.vx;
yourObject.position.vy += newVec.vy;
yourObject.position.vz += newVec.vz;