Direct3D 9 and 360 degrees

Started by
31 comments, last by theo2005 12 years ago
Apoch and Bacterius, thank you :)

The code Apoch gave me did indeed seem to work, but even so, as I moved away from the object, strange things seemed to arise.
The magical constant 20.0f seemed to work the best with no twiching at the location I was at, but as I moved farther away from the object,
something fishy was happening anyway. And the curve that the Y rotated by .. it was fishy as well.

Question about your code Bacterius, the Sin with the capital S does not work in the program.
I searched google and it seemed that the domain (whatever that is) is different for sin and Sin.
So what would be the function for Sin in directX?

Cheers,
Theo
Advertisement
Sorry, it was obviously a typo. I was typing pseudocode quickly. It's just the standard sin function.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Argghh.. headacheee!

I can't see my object... why?? (cannot find it)

Here's the whole code (related to transformations):


D3DXMATRIX matView; // the view transform matrix

static float MouseSpeed = 0.0075f;

if (keystate[DIK_F1])
{
MouseSpeed += 0.0001f;
}
else if (keystate[DIK_F2])
{
if (MouseSpeed > 0.0001f)
{
MouseSpeed -= 0.0001f;
}
}
static float AngleX = 0.0f; AngleX += mousestate.lX * MouseSpeed;
static float AngleY = 0.0f; AngleY += mousestate.lY * MouseSpeed;

static float LookX = 0.0f;
static float LookY = 0.0f;
static float LookZ = 0.0f;

static float MoveX = 0.0f;
static float MoveY = 0.0f;
static float MoveZ = 0.0f;
if(keystate[DIK_A] & 0x80)
{
MoveX -= 0.3f;
}
if(keystate[DIK_D] & 0x80)
{
MoveX += 0.3f;
}
if(keystate[DIK_S] & 0x80)
{
MoveY -= 0.3f;
}
if(keystate[DIK_W] & 0x80)
{
MoveY += 0.3f;
}
LookX = cos(AngleX) * sin(AngleY);
LookY = sin(AngleY);
LookZ = sin(AngleX) * sin(AngleY);
D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3 (MoveX, MoveY, MoveZ), // the camera position
&D3DXVECTOR3 (LookX + MoveX, LookY + MoveY, LookZ + MoveZ), // the look-at position + to go along with movement
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction
d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView
D3DXMATRIX matProjection; // the projection transform matrix
D3DXMatrixPerspectiveFovLH(&matProjection,
D3DXToRadian(45), // the horizontal field of view
(FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio
1.0f, // the near view-plane
1000.0f); // the far view-plane
d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection
// ----------------------------------------------------------- CAMERA SETUP <END> --------------------------------------------------------------------------//

// select the vertex buffer to display
d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
d3ddev->SetIndices(i_buffer); //select the index buffer
D3DXMATRIX matTranslateA; // a matrix to store the translation for triangle A
D3DXMATRIX matRotateX; // a matrix to store the rotation for each triangle
D3DXMATRIX matRotateY;
static float indexX = 0.0f; // index+=0.03f;
static float indexY = 0.0f;
if(keystate[DIK_LEFT] & 0x80)
{
indexY += 0.03f;
}
if(keystate[DIK_RIGHT] & 0x80)
{
indexY -= 0.03f;
}
if(keystate[DIK_DOWN] & 0x80)
{
indexX += 0.03f;
}
if(keystate[DIK_UP] & 0x80)
{
indexX -= 0.03f;
}
//building matrices
D3DXMatrixTranslation(&matTranslateA, 0.0f, 0.0f, 15.0f); //15.0f away from the camera
D3DXMatrixRotationX(&matRotateX, indexX);
D3DXMatrixRotationY(&matRotateY, indexY);
// tell Direct3D about each world transform, and then draw another triangle
d3ddev->SetTransform(D3DTS_WORLD, &(matRotateX * matRotateY * matTranslateA));
d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
//single primitive: d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);


Also, even tho it was a typo, Sin actually means arcussinus or sin^-1. (asked the math teacher and only one search result on google)
Also, even tho it was a typo, Sin actually means arcussinus or sin^-1. (asked the math teacher and only one search result on google)[/quote]
Really... that's just stupid. What a messed up convention. Anyway.

Can you make your program print out the values of AngleX, and AngleY each frame (to the console preferably) as well as the values of LookX/Y/Z? Then we'll see if the camera setup code is actually working properly.

I also don't understand something in your code. Is the camera setup executed every frame? Because it should if you want to be able to move around. And if so, why do you reset the values of MoveX, MoveY, MoveZ to zero each frame?

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

It doesn't reset the Move variables AFAICT. They're defined static, so they are initialized once to 0, and then accumulated with movement increments thereafter.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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:

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


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?

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


You must be mistaken. http://en.wikipedia....ian_coordinates.

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 nothing 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 nothing 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.



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 at whatever the sin and cos gives you. It looks in the direction 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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

As promised here is the diagram (click to enlarge):


10pd5rs.png

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).

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

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 smile.png

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
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).


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).

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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

This topic is closed to new replies.

Advertisement