• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Setting the desired view direction

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1theo2005  Members

Posted 27 April 2012 - 03:45 AM

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


### #2Dave  Members

Posted 27 April 2012 - 04:05 AM

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.

### #3theo2005  Members

Posted 27 April 2012 - 08:18 AM

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

### #4ApochPiQ  Moderators

Posted 27 April 2012 - 10:16 AM

Change the initial values of AngleX and AngleY.
Wielder of the Sacred Wands

### #5theo2005  Members

Posted 27 April 2012 - 01:39 PM

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?

### #6ApochPiQ  Moderators

Posted 27 April 2012 - 01:50 PM

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

### #7theo2005  Members

Posted 27 April 2012 - 02:04 PM

I'm really sorry, but I have no idea how to compute the necessary view matrix..

### #8ApochPiQ  Moderators

Posted 27 April 2012 - 02:44 PM

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

### #9theo2005  Members

Posted 28 April 2012 - 05:56 AM

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

### #10Trienco  Members

Posted 28 April 2012 - 06:42 AM

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

### #11theo2005  Members

Posted 28 April 2012 - 07:48 AM

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.

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:

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


### #12theo2005  Members

Posted 28 April 2012 - 02:20 PM

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!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.