Setting the desired view direction

Recommended Posts

theo2005    114
Hello,

My last topic died, but since my last question was a bit off-topic I'll just go ahead and make a new one.

How would I go about setting the initial view direction? (Setting it in the code)
Given that I can't mess with the formula, I really have no idea.

Here's the code:

[CODE]
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
[/CODE]

Share on other sites
dave    2187
Hmm,

Ok so my guess is that you're only calculating the view matrix when you press a key?

I usually take a different approach to this. In your scene you'd have the concept of a camera, whether this is a node in your scene or not depends on lots of things, but ultimately somewhere there will be a Camera object that has a position and view direction etc. Typically you can recalculate the view and projection matrices from the camera object and SetTransform each time you're about to draw.

The camera object allows you to set up the default camera position and orientation (and other properties) in the constructor. Which part of your project owns this camera object is up to you.

It looks to me like your camera is always starting at (0,0,0) right? So in your solution you can set your movex/y/z variables to something more sensible or look at structuring this a bit better.

Share on other sites
theo2005    114
[quote name='Dave' timestamp='1335521145' post='4935339']
Ok so my guess is that you're only calculating the view matrix when you press a key?
[/quote]

No, the calculation and drawing are all in the same function and you do not need any buttons
to be pressed for the screen to draw. It will however make the addition/subtraction when a button is pressed.

My move variables are all at 0,0,0.
My look variables are however determined by the sin/cos formula.
The program starts with the camera looking up and a bit to the right
from the object which is at 0,0 3. Thus moving the camera has no effect
on fixing the problem I have.

So I ask once again, how do I set the camera to look into the right direction?
------------

I do not use classes in my program, so talking about objects and constructors isn't needed.

---------

What I wanted to say is that I have no control over where I look at, except for when
the program is running and I manually move my mouse

Share on other sites
ApochPiQ    23004
Change the initial values of AngleX and AngleY.

Share on other sites
theo2005    114
[quote name='ApochPiQ' timestamp='1335543374' post='4935416']
Change the initial values of AngleX and AngleY.
[/quote]

I thought there should be some other way.
But oh well, here's what I came up with:
[CODE]
static float AngleX = 4.69f; AngleX -= mousestate.lX * MouseSpeed;
static float AngleY = 4.5f; AngleY -= mousestate.lY * MouseSpeed;
[/CODE]

These are the values to make the camera look more or less straight at the object.
Where do these absurd numbers come from? Why????????

I'll have to implement those magic numbers into every formula I will make up to make the camera
follow the character. Do I really need such nonsense?

Share on other sites
ApochPiQ    23004
You can always pick a look direction vector and compute the necessary view matrix from that. There's no need to do it with angles, which will eliminate a lot of the seemingly "magic" numbers.

Share on other sites
theo2005    114
I'm really sorry, but I have no idea how to compute the necessary view matrix.. [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]

Share on other sites
ApochPiQ    23004
You're already calling D3DXMatrixLookAtLH. That's how you get the matrix.

Instead of computing angles for the look direction, pass it a vector that represents the direction you want to look in.

Share on other sites
theo2005    114
[quote name='ApochPiQ' timestamp='1335559454' post='4935472']
Instead of computing angles for the look direction, pass it a vector that represents the direction you want to look in.
[/quote]

I just cannot understand. I need the formula to look around the world properly.

[CODE]
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
[/CODE]
What's the point if I just put in the LookX/Y/Z values manually?
Then I may be able to set the direction I want at first but... ARGHH >.<!
-------------

Anyway, this is just a very big head ache to me and it seems that I won't be able
to do much because I like to keep things as simple as possible even though I'm always
willing to learn.

I'll just stick with the magic numbers I've got and try to implement a calculation system of some sorts for
a few things I need. (particularly to make a 3rd person camera and to have logical looking/moving system.

Right now I'm facing problems like it doesn't matter where I look, it will still move in the same direction
no matter where I look. Going to have to figure this one out ;)

Share on other sites
Trienco    2555
His suggestion actually IS the simple way, because you won't have to deal with any trigonometry, can let the D3DX matrix library do all the work for you and fix your movement problem in one step. It just requires reading up on transformation matrices and vector math. Considering that these things are pretty much the absolute core of any 3D game or application you won't get around learning to love those anyway.

Share on other sites
theo2005    114
[quote name='Trienco' timestamp='1335616966' post='4935576']
His suggestion actually IS the simple way, because you won't have to deal with any trigonometry, can let the D3DX matrix library do all the work for you and fix your movement problem in one step. It just requires reading up on transformation matrices and vector math. Considering that these things are pretty much the absolute core of any 3D game or application you won't get around learning to love those anyway.
[/quote]

Does that mean that I don't actually need the formula?

If I don't then I'll just say hallelujah and ask.. how would I handle
everything I need with the D3DX library?

With trigonometry, I managed to get SOMETHING like what I wanted,
but it's still complete bull**** what's going on in my program.
Here's the code I came up with:

[CODE]
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 = 4.7f; AngleX -= mousestate.lX * MouseSpeed; //increase = left
static float AngleY = 4.7f; AngleY -= mousestate.lY * MouseSpeed; //increase = up

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

static float MoveX = 0.0f;
static float MoveY = 1.0f;
static float MoveZ = 0.0f;

//in order for the camera to keep looking in the same direction
LookX = cos(AngleX) * sin(AngleY);
LookY = cos(AngleY);
LookZ = (sin(AngleX) * sin(AngleY));
FileInputZ = LookZ;
FileWrite ();

if (LookZ > 0) //looking infront of center
{
if(keystate[DIK_A] & 0x80)
{
MoveX -= 0.3f;
}
if(keystate[DIK_D] & 0x80)
{
MoveX += 0.3f;
}
if(keystate[DIK_S] & 0x80)
{
MoveZ -= 0.3f;
}
if(keystate[DIK_W] & 0x80)
{
// MoveZ += 0.3f;
MoveZ += (sin(AngleX) * sin(AngleY));
MoveY += cos(AngleY) ; //too fast
MoveX += cos(AngleX) * sin(AngleY);
}
}
if (LookZ < 0) //if looking behind the center
{
if(keystate[DIK_A] & 0x80)
{
MoveX += 0.3f;
}
if(keystate[DIK_D] & 0x80)
{
MoveX -= 0.3f;
}
if(keystate[DIK_S] & 0x80)
{
MoveZ += 0.3f;
}
if(keystate[DIK_W] & 0x80)
{
//MoveZ -= 0.3f;
MoveZ += (sin(AngleX) * sin(AngleY));
MoveY += cos(AngleY) ; //too fast
MoveX += cos(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
[/CODE]

Share on other sites
theo2005    114
Nevermind, I found a good tutorial on vectors.
I wont get any further than this with just trigonometry and my seemingly non-existent knowledge in math.

Thank you for all of your input!