Sign in to follow this  

help with top down game

Recommended Posts

Ive posted a thread in the beginners section but its not really getting as much attention as I had hoped, so im going to give it a shot here. Im trying to create a top down game using directx and im having a problem. Right now all i have is a tank on the screen and im trying to drive it around. Unfortunately im having a hard time turning and moving forward. I can turn it the way i would like, but its not translating down the correct axis. What i mean by this is that if i turn it, say 30 degrees, and accelerate, it continues to move aound in the x axis, despite the fact that ive rotated the axis. here is the matrix multiplication im using: device.Transform.World = Matrix.RotationAxis(new Vector3(0, 0, 1), (2.0f * (float)Math.PI) * tank.getAngle() / 1000.0F) * Matrix.Translation(pos); pos is a Vector3 which consists of (tank.x, tank.y,0). If anyone could shine some light on me, that would really be helpful. Thank you in advance.

Share this post

Link to post
Share on other sites
You have to translate the tank along its forward axis. Let's say that in the tank's default position, it's forward axis is +Y. Let's say it's rotated 90 degrees clock-wise. Now its forward axis is actually +X.

In code, something like:

Vector3 forward = new Vector3(0.0, 0.0, 1.0);
Matrix rotation = Matrix.RotationAxis(...);
// Transform the forward vector by the matrix. Don't recall the method name
// or where it is--it's likely to be in Matrix: Matrix.TransformSomething

Vector3 newPosition = oldPosition + displacement * forward;

Share this post

Link to post
Share on other sites
I don´t see where you´ve rotated the axis of movement for the tank.
You´re only setting a transformation for the tank´s model, that means how it is going to be displayed. As you have not shown any code regarding the tank´s movement I don´t think anyone can help you with that except give you general advice. I´ll try to do that now:
I guess you want the tank to move in the direction it is facing. For that you will need a "forward" vector, i.e. a direction vector which indicates the direciton of movement for the tank. From the line of code you supplied I assume your tank does a 360° rotation when its angle value is 1000.
I suppose that you want your tank to move along the X-axis for an angle of 0.
So you could try to rotate a unit vector parallel to the X-axis using the Matrix.RotationAxis you used for the transformation matrix:

Vector3 dir = Vector3(1.f, 0.f, 0.f);
Matrix rotMatrix = Matrix.RotationAxis(new Vector3(0, 0, 1), (2.0f * (float)Math.PI) * tank.getAngle() / 1000.0F);
dir = dir * rotMatrix; // now dir should be the forward vector of the tank

//when you want to move do the following:
pos = pos + moveAmount * dir;

After that you should be able to compute the transformation matrix as you did in your post.

Some things I´d like to mention: I´m not too sure about the order for vector-matrix multiplication, so it might be 'dir * rotMatrix' or 'rotMatrix * dir'. I´m not too sure whether the D3D vector and matrix classes support the * operator at all, but if they don´t, there is definitely a function doing the same thing.
Probably you might need to use a Vector4 to be able to do that matrix multiplication. In that case it shouldn´t matter whether you´re using a w-coordinate of 0 or 1, as you´re only rotating it anyway, but as it is a direciton vector I´d suggest using 0 for w, just in case you accidentally apply a translation to it.

Hope that helped

Share this post

Link to post
Share on other sites
yeah those functions definitely arent build in directly, ill have to dig around some more. but i also was wondering in the code i posted earlier,

device.Transform.World = Matrix.RotationAxis(new Vector3(0, 0, 1), (2.0f * (float)Math.PI) * tank.getAngle() / 1000.0F) * Matrix.Translation(pos);

i was under the impression that the matrix.translation(pos) would be what actually moves it around. and it is moving it down that axis, but only the X axis and as you pointed out, it doesnt move it in the direction that i would like it to. but this however is the problem im running into.

Share this post

Link to post
Share on other sites
The Matrix.Translation(pos) does move your tank, but not how you suppose it does:
What device.Transform.World is for is to move the object from object space to world space. Object space normally is the space (coordinate system) the object was modelled in. Now it wouldn´t be any good if the object would always remain at the origin of the world space. In order to place your object in the world you use the device.Transform.World.
Now, because the world transform places the object in the "world", it "refers" to the world´s coordinate system, and therefore a world transform of Matrix.Translation(Vector3(1,0,0)) will always place your object at (1,0,0) in the world, regardless of the objects orientation.
However rotations always occur around the origin. So if you first move the object to (1,0,0) and then rotate it, it will be placed on a circle around the origin with radius 1.
What the line of code you posted does is the following:
It rotates the object by the angle you specified and then moves it along the _world´s coordinate axes_ by 'pos'.
Therefore you have to calculate 'pos' in world coordinates, which is what you need the direction vector for.

Hopefully I didn´t confuse you too much, I find it difficult to explain such things with only words. Perhaps you should play around a bit with transformation matrices, for example try swapping translation and rotation (instead of Matrix.Rotation... * Matrix.Translation try Matrix.Translation * Matrix.Rotation...) and try to get a grip on the results you get. There should also be some articles / tutorials about the Direct3D Transformation Pipeline around, just try google or have a look in the DirectX SDK docs and search for Transformation there.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this