Setting the desired view direction

Started by
10 comments, last by theo2005 11 years, 11 months ago
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:


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

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


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
Change the initial values of AngleX and AngleY.

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


Change the initial values of AngleX and AngleY.


I thought there should be some other way.
But oh well, here's what I came up with:

static float AngleX = 4.69f; AngleX -= mousestate.lX * MouseSpeed;
static float AngleY = 4.5f; AngleY -= mousestate.lY * MouseSpeed;


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

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

I'm really sorry, but I have no idea how to compute the necessary view matrix.. sad.png
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.

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


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


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


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

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

Thank you for your help.
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.
f@dzhttp://festini.device-zero.de

This topic is closed to new replies.

Advertisement