gameObject Move Forward By Trigonometry

I am trying to move me gameObject by getting its rotation and calculating where its next position will be according to its rotation and increment it inside a loop, its like, go forward according to where its “facing” or “pointing”.

Here is the piece of code im using to try to achieve that:

NpcPos = this.transform.position;
NpcRot = this.transform.rotation.eulerAngles;
double movQuantity = 0.2;
float anglex = (float)(NpcRot.x * (Math.PI / 180));
float angley = (float)(NpcRot.z * (Math.PI / 180));

NpcPos.x = NpcPos.x + (float)(Math.Sin((double)angley) * Math.Cos((double)anglex) * movQuantity);
NpcPos.z = NpcPos.z + (float)(Math.Cos((double)angley) * Math.Cos((double)anglex) * movQuantity);
NpcPos.y = NpcPos.y + (float)(Math.Sin((double)anglex * movQuantity));

this.transform.position = NpcPos;

Unity already does that a bit easier for us with the following code, which is exactly the functionality I want to “copy”

var moveDirection = transform.TransformDirection(Vector3.forward);
controller.Move(moveDirection * Time.deltaTime);

I was following this tutorial to do that:
http://answers.yahoo.com/question/index?qid=20080121092931AAC9flk
, It looks like in this tutorial the z and y axes are swapped…

Anyway… I know that there is another way to do that, which is by vector calculation, I think thats how unity actually does that, but I cant wrap my head around it. Ive been reading on the internet about it but its a bit hard to find as most game engines already do that part, even someone programming directly on directX or OpenGL can use its internal functions to do the vector calculation.

You may be asking by now, why the hell you want to do that if unity can do that for you.
Answer, I dont intent do use that in unity, I want to use the code in my server (photon) because when the player shoots, I want the server to controll the projectiles, which I think its how it has to be.

Im trying to get this working for the past few days and the problem is that I think the trigonometry on that tutorial must be wrong beacuse the projectile never goes straight to where it is “facing” according to its rotation.

I am desperate to solve that as I am spending too much time trying to get this working and going nowhere.

If anyone knows a better way to do that please, pretty please, let me know.

Thanks
:slight_smile:

If your question is “how do I make a direction vector from Euler angles with no help from Unity’s geometry classes?” my answer is “don’t”. The gruesomeness of that sin/cos stuff illustrates why most folks don’t do it that way. :wink:

And once you start accumulating rotations as Euler Angles, it’ll only get worse.

You should learn to use Quaternions and Matrices. The “rotation” member of a Unity transform is a Quaternion. A Quat ernion can initialize a Rotation Matrix then matrices can transform points. If you were writing this from scratch, like on your server, you’d do something along the lines of

Matrix m = MatrixFromQuaternion(rotationQuat);
Vector3 forward = m.TransformNormal(ReferenceForward);
Position += forward * distance;

But in practice, your server-side object graph would probably have a Forward vector cached from the last update. Just like Unity’s does.

I can recommend “3D Math Primer for Graphics and Game Development” by Fletcher Dunn and Ian Parberry.

Having to do that would be my worse case scenario, but I saved two links that i think that would help me.

One is a library that I think that has what I need, it was written in c++ but I think i can convert what I need from it to c#.

The other one is a Math library for c#:
http://sharp3d.codeplex.com/releases/view/121

Oh… almoust forgot… ode.net:
OdeDotNet - Browse Files at SourceForge.net

I just dont know which option Id pick if I had to use one of them.

@dalep :
If you were in my place and had to the quaternions and matrices on the server side what would you do?
Would you pick a library that has all coded or go from scratch and only picking what would be necessary?

Do you have anything to recommend from personal experience that helped you to learn all that?

If I was to undertake a object graph from scratch in c# that was going to drive a Unity client, I’d find a 3D math library designed to work with OpenGL and link it in. The OpenGL part is kind of important because there can be some assumptions about coordinate systems built into these libraries. OpenGL and DirectX use different ones: Left-Handed vs Right-Handed. You can work around that kind of thing but when learning its best to keep it simple.

But before doing that, I’d probably try developing my server in Unity. Then I’d have really good compatibility between client and server. Only if there was some profound technical problem would I look to start a server from File/New in Visual Studio.

The thing is that any good 3D math library is going to let you treat the guts of matrices and quaternions as black boxes. Just use the functions and who cares what’s inside. I promise that, after a little bit of frustration at the beginning, this will make your life a whole lot easier.

If it weren’t for the coordinate system handedness issue (and if you work on a Windows platform), I’d suggest doing some XNA tutorials since that’s in C# and XNA has a really nice low(er than Unity’s) level math library. And I guess I still wouldn’t recommend against it. There are probably some good C# wrappers for OpenGL out there too. But I don’t know what they might be. You could look around at GameDev.net - that place is a treasure trove of good info on these things.

I actually found one yesterday:
http://csgl.sourceforge.net/

Yea im crying for help over there already :stuck_out_tongue:

Thanks for helping though

Also, if you are going to do any rigidbody stuff at all, you are going to really have your hands full. The difficulty of physics modelling dwarfs the technical challenges of 3D geometry.

That’s how I ended up in Unity - got frustrated with building a physics model from scratch :slight_smile:

Ill try to avoid server side physics at all cost =)

Hey dalep, after days of research into all that game programming math that is new to me, stuff are finally starting to come together, slowly but surely =)

Could you tell me if I can convert the c++ example from this link:

Do you think that the code by the user codemattic is usabe? In the beggining of the post, the question that the guy asks is exactly what I want to do as you replied to me:

The thing about your example is that I have no idea what the m.TransformNormal is, nor the ReferenceForward.
I tried looking on the openGL library but I couldnt find anything that looks like that.

If you build a 3x3 matrix from your quaternion, then TransformNormal() equates to a simple matrix-vector multiplication.

If you’re working with 4x4 matrices, then TransformNormal() will multiply the matrix with a 4-d vector, the x, y, and z elements of which are the same as those of the input vector, and the w element of which is set to 0. The w element of the result is typically then discarded and a 3-d vector is returned.

ReferenceForward isn’t anything special; it’s just the forward vector for your object in local space. For example, if the object points down the +z axis in local space, then ReferenceForward would be (0, 0, 1).

As for the code you’re asking about, I didn’t proofread it or anything, but assuming there are no errors, it should do what you’re wanting. You’ll probably have to adapt it however to work with whatever indexing scheme your matrix class uses.

Also, if you use a pre-existing math library it will probably already have a quaternion-to-matrix function, in which case you shouldn’t have to write one from scratch.

Also, why can’t you just use the right, up, and forward fields of the Unity transform? Do you not have those available? If your object points along a cardinal axis in local space, then that would be by far the easiest way to get the direction vector for the object.

Hey Jesse thanks for your reply.
I think I can and should use the references that are already stored in unity, I think its a good idea to keep things simple, because the only thing I really want to do on the server side is to update the position(make it move forward), if it were possible I would do a position+=distance inside the loop and that would be all :slight_smile:

To maybe give you a better idea what I want to do, it is to shoot a missile in a straight forward line. I guess i can get the references from the ship that is already facing the direction that the missile has to go.

So in simple steps, all I have to do is:
Transform the quaternion that I will get from unity into a matrix.
Multiply that matrix with the forward vector of the object(that means, transform.forward correct?)
Then multiply that forward by a distance i want it to go each loop?

About the distance, it should be a vector too? How then I say I want it to move 0.5? (0,0,0.5)maybe?

Oh yea, and if I am to do this from scratch I still have to find a way to convert the matrix, after its multiplied by the forward, into a vector to be able to multiply the distance right?

I think what you might be missing is that the ‘basis vector’ fields of the transform class (right, up, and forward) already point in the right direction - they’re the local axes of the object expressed in world space, which I think is exactly what you want.

Another way to put it is that if your object points along one of the cardinal axes (+ or - x, y, or z) in local space (which I assume it does), then you don’t need to rotate anything, or convert any quaternions to matrices, or really do any math at all to speak of. In fact, rotating transform.forward by transform.rotation will generally just give you a more or less meaningless result. (It’s kind of like trying to translate English text into English - there’s really no point :wink:

I certainly advocate understanding the theory behind all this, but for practical purposes, you might just try out what I’m suggesting and see if it works. So in summary:

  • When the projectile is fired, grab the (presumably) forward direction vector of the ship; this will be the direction vector for the projectile.

  • Each update, add this direction vector to the projectile position, scaled by speed and by the time step for the update.

Exactly, i dont pretend to rotate anything for now.

That will be great if it works that simple! :smile:

I’ll try that out and let you know, thanks for the help though

Yay! it did work =)

Although looking at it now, i think someone else told me the same solution as you Jesse, but it was so alien to me that I couldnt wrap my head around it and implement it, but anyway it works now thanks a lot for your help =)

Now i think i have a different problem, the server movement function uses a vector to place objects on space and that function receives an Int vector.

Im having a weird result because of that I think. When I shoot, the missiles start stopped and increase the speed a little in 1 or 2 seconds then after fly away real quick like a bullet, im guessing this is because the ints of the vector are ignoring the part after the dot (eg: 0.84354) and thats not giving a realistic result. I saw that the forward vector that I use to multiply in the position increase is always smaller then 1 so I guess I will have no choice if not change the server vector class from int to float right?

Yes, probably. I don’t know the details of what you’re doing exactly, but it’s rarely practical to use integer types for this sort of simulation.