Direct3D 9 and 360 degrees

This topic is 2097 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I can move properly (or at least when I could see the object I could). I'll look up the code for printing text on the screen and then edit this post.
------------------

Nevermind. Bacterius, your formula was wrong. The right formula is:
[CODE]
LookX = cos(AngleX) * sin(AngleY);
LookY = sin(AngleX) * cos (AngleY);
LookZ = sin(AngleX) * sin(AngleY);
[/CODE]

Now a problem, how the heck do I make the camera look at the object?
It looks somewhere else to the point that I have to search for it, even though the camera and object are both at 0,0.

So the object is at 0,0,15
The camera at 0,0,0
and looks at whatever the sin and cos gives me.
If I multiply LookZ with 15.0f, I can make it look at it, but then the camera doesn't move properly for obvious reasons.

What do I do now?

Share on other sites
[quote name='theo2005' timestamp='1334999516' post='4933477']
Nevermind. Bacterius, your formula was wrong. The right formula is:
[CODE]LookX = cos(AngleX) * sin(AngleY);
LookY = sin(AngleX) * cos (AngleY);
LookZ = sin(AngleX) * sin(AngleY);[/CODE]
[/quote]
You must be mistaken. [url="http://en.wikipedia.org/wiki/Spherical_coordinate_system#Cartesian_coordinates"]http://en.wikipedia....ian_coordinates[/url].

If the object you want to look at is at the same position as the camera, then the camera's direction is undefined. It's like trying to look at your own eye: you can't do it. It doesn't make sense.

Now the explanation:

The idea is that the D3D lookat matrix function takes a *camera position* (which is the position of your camera as MoveX, MoveY, MoveZ), a *camera target* (which is MoveX + LookX, MoveY + LookY, MoveZ + LookZ) where LookX, LookY and LookZ is the direction vector you are looking at (obtained by the cos/sin formulas), and an *up vector* (which is (0, 1, 0) for your purposes). It doesn't get any more complicated.

You don't multiply LookZ by MoveZ or anything : you add them. The idea is that LookX, LookY and LookZ is a direction vector which describes the direction in which you are looking. It tells [b]nothing[/b] about position. It's a vector. So to obtain the actual *point* in 3D where the camera is looking at, you must add the direction vector to the camera's position.

Imagine: you are looking straight ahead of you at a ball 1 metre ahead, in the X direction. The direction vector (LookX, LookY, LookZ) is going to be (1, 0, 0). This information tells you [u]nothing[/u] about where you or the ball actually are. On the other hand if you know that you are standing at say, (3, 4, 5), then the ball must be at (3 + 1, 4 + 0, 5 + 0) = (4, 4, 5). You simply add the direction vector to the camera's position to obtain the camera's target.

[quote]

So the object is at 0,0,15
The camera at 0,0,0
and looks at whatever the sin and cos gives me.
[/quote]
This is where your logic is failing. The camera doesn't look [u]at[/u] whatever the sin and cos gives you. It looks [u]in the direction[/u] whatever the sin and cos gives you. To actually get the camera target for the D3D lookat function you need to add that direction to your camera's position to get another position corresponding to the target.

The object's position doesn't even come into play at all, at any time in the camera setup.

I hope it makes sense. When I come back from uni in a couple hours I will draw you a diagram to help you picture it if you are still confused.

Share on other sites
As promised here is the diagram (click to enlarge):

[img]http://i39.tinypic.com/10pd5rs.png[/img]

This is for the 2D case (in 3D the concept is exactly the same, except the vectors have three components and you need two angles to describe an arbitrary direction vector instead of one - this is where spherical coordinates come in). I hope this makes it a bit clearer to you.

So basically you use your two angles (controlled by the mouse) to change the direction in which the camera is looking (and you obtain the "look" direction vector by using spherical coordinates from the two angles and an arbitrary radius*), you use the keyboard to change the camera's position as usual, and you derive the camera's target by adding these two vectors together as the diagram shows. You then give this to the D3D LookAt function which will give you the view matrix you want.

* the radius does not matter in this case because as I explained earlier, using a bigger or smaller radius wouldn't change the direction of the "look" direction vector, which means the resulting matrix will be identical (because the function normalizes the "look" direction directly, which cancels out any scaling).

Share on other sites
Thank you for the thorough explanation!
I think I actually understand now.

You explained the theory and helped me get over
my misunderstandings, but my question is still unanswered [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

I am always at the mercy of what sin and cos gives me.
Sin and cos defines the point (excuse me, direction) where I look at when my program starts, I don't like that.
I want to set my starting view direction MYSELF.

It's not cool when you start a 3D rpg and instead of starting by
looking at the landscape, you look at the dirt below your feet.

Also, how am I supposed to make a camera follow a character and keep looking
at the direction of the character if I cannot define where I look at except for manually
following him with my mouse?

Oh and... my formula is right.
With your formula, the camera's view doesn't even shift on the Y axis and does some weird stuff.
With the formula I defined earlier, everything works perfectly.

I dont know why that is, but I wouldn't lie.

Cheers,
Theo

Share on other sites
[quote]I am always at the mercy of what sin and cos gives me.
Sin and cos defines the point (excuse me, direction) where I look at when my program starts, I don't like that.
I want to set my starting view direction MYSELF.[/quote]
You should be able to set the initial view direction by setting the initial values for the angles. You can use the inverse of the spherical coordinates to obtain angles from an actual direction (which can be more convenient, say you want to look ahead of you in the Z direction when you first start your program, you use the inverse spherical coordinates on the (0, 0, 1) vector, and obtain the correct two angles).

[quote]Also, how am I supposed to make a camera follow a character and keep looking
at the direction of the character if I cannot define where I look at except for manually
following him with my mouse?[/quote]
You still seem to be stuck on this. What I'm showing you is for first-person perspective (I thought this is what you wanted). It's obviously a lot different for third-person perspective, because then it depends on how what perspective you actually want (in terms of what happens when you move the mouse, what when you zoom in/out, etc...). As for the mouse, this is just the interface between you (the player) and your ability to change the view angles (which allow you to change in which direction the camera is looking).

[quote]Oh and... my formula is right.
With your formula, the camera's view doesn't even shift on the Y axis and does some weird stuff.
With the formula I defined earlier, everything works perfectly.[/quote]
I actually did make a mistake in the formula, it should be (this explains why it didn't work on the Y-axis):

LookX = cos(AngleX) * sin(AngleY);
LookY = cos(AngleY);
LookZ = sin(AngleX) * sin(AngleY);

My mistake - but it still doesn't make your formula correct. Your formula does not work. It only *appears* to work at first but as you said, when you make it do some stuff it eventually breaks down. If you implement spherical coordinates properly they will work in every single case (I have done it in my own camera code, as well as hundreds of other people, so they obviously work otherwise people wouldn't be using them and I wouldn't be explaining them to you).

I am not sure what you are looking for anymore. What is your goal? I was under the impression you wanted to implement a first-person perspective camera, where you use the keyboard to move the camera around the world and the mouse to look around (much as you would in any 3D first-person game). Please correct me if I was wrong because this is what I'm working under.

Share on other sites
[quote name='Bacterius' timestamp='1335170460' post='4934008']
say you want to look ahead of you in the Z direction when you first start your program, you use the inverse spherical coordinates on the (0, 0, 1) vector, and obtain the correct two angles).
[/quote]

How would I go about doing this in my program? And which vector are you referring at exactly?

[quote name='Bacterius' timestamp='1335170460' post='4934008']
LookX = cos(AngleX) * sin(AngleY);
LookY = cos(AngleY);
LookZ = sin(AngleX) * sin(AngleY);
[/quote]

Yup, this seems to work as well.

[quote name='Bacterius' timestamp='1335170460' post='4934008']
I am not sure what you are looking for anymore. What is your goal? I was under the impression you wanted to implement a first-person perspective camera, where you use the keyboard to move the camera around the world and the mouse to look around (much as you would in any 3D first-person game). Please correct me if I was wrong because this is what I'm working under.
[/quote]

My goal is to have a camera to look around in the world freely. But even so,
I'd love to have the camera pointing in the direction << I >> personally choose.
-----------------

The reason I also want to have the ability to choose where I look at, is because
then I can later use it to convert to 3rd person camera. My idea of it was to
make the camera follow the player from a certain distance and stay looking at
the character just by supplying the character movement values to the view vector.
But due to the fact that MY common logic does not work here, I deduce that just
putting the character movement values into the view vector will be of no good, thus
I still need to work with the formula you supplied me.

The problem is, the only way
I currently know how to make it point where I want, is multiplying one formula
for the chosen axis with the float number where I want it to point. That of course
breaks the camera view movement and yields incorrect results.

So the conclusion is that I was happy when the camera view movement finally worked, but at
the same time confused and disappointed that I have no means to choose the direction.
I thought that it might be okay if it's only to look around the world, but then I realized that
I wont be able to make a 3rd person camera.

--------------

The reason I might have confused you is because I never thought, nor still think of my current camera as a 1st person camera.
I think of it as spectator mode, or just a camera to look around the world. It highly upsets me that not only I cannot set where I look at
initially, but when I change the initial camera location (move values), even my camera view movement will become inverted on one axis.
(Let alone that for some reason increasing the value on Y axis will make the camera go up, but increasing the value on X axis will make the camera move LEFT not right.)
So.. I'm lost in a mystery.

Share on other sites
You can get a view direction from some angles, and you can invert this process to get the appropriate angles for a view direction... or just set an initial direction that you like the look of?

Share on other sites
[quote name='return0' timestamp='1335222674' post='4934273']
You can get a view direction from some angles, and you can invert this process to get the appropriate angles for a view direction... or just set an initial direction that you like the look of?
[/quote]

"Some angles"?
Please do show me how to set the initial direction in my code.