**0**

# Direct3D 9 and 360 degrees

###
#21
Members - Reputation: **114**

Posted 20 April 2012 - 02:53 AM

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

###
#23
Members - Reputation: **114**

Posted 20 April 2012 - 08:34 AM

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)

###
#24
Crossbones+ - Reputation: **11537**

Posted 20 April 2012 - 04:43 PM

Really... that's just stupid. What a messed up convention. Anyway.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)

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.”*

###
#25
Moderators - Reputation: **18040**

Posted 20 April 2012 - 04:48 PM

[Work - ArenaNet] [Epoch Language] [Scribblings] [Journal - peek into my shattered mind]

###
#26
Members - Reputation: **114**

Posted 21 April 2012 - 03:11 AM

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

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?

###
#27
Crossbones+ - Reputation: **11537**

Posted 22 April 2012 - 04:20 PM

You must be mistaken. http://en.wikipedia....ian_coordinates.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);

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.

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.So the object is at 0,0,15

The camera at 0,0,0

and looks at whatever the sin and cos gives me.

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.”*

###
#28
Crossbones+ - Reputation: **11537**

Posted 22 April 2012 - 07:56 PM

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.”*

###
#29
Members - Reputation: **114**

Posted 23 April 2012 - 02:15 AM

I think I actually understand now.

You explained the theory and helped me get over

my misunderstandings, but my question is still unanswered

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

###
#30
Crossbones+ - Reputation: **11537**

Posted 23 April 2012 - 02:41 AM

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

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

I actually did make a mistake in the formula, it should be (this explains why it didn't work on the Y-axis):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.

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.

###
#31
Members - Reputation: **114**

Posted 23 April 2012 - 05:46 AM

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

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

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

LookY = cos(AngleY);

LookZ = sin(AngleX) * sin(AngleY);

Yup, this seems to work as well.

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.

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.

###
#33
Members - Reputation: **114**

Posted 24 April 2012 - 01:20 AM

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?

"Some angles"?

Please do show me how to set the initial direction in my code.