Public Group

# Direct3D 9 and 360 degrees

This topic is 2464 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey,

I'm new here and I have a silly question to start off.

I have a simple program that draws a cube. You can rotate the cube with arrow keys and look around on one spot with the camera.
Now I've been messing around with it, thinking wouldn't it be fun to make a 360 degree turn and get back to my cube.
What the program instead does, is keep going infinitely in the direction I choose and it will take the same amount of moving the mouse to get back to the object (hence, it never makes a 360 degree turn).

So, the impression this left me is that direct3D thinks that there is an infinite amount of space or something along those lines.
Almost like every 360 turn is on a next level and to get back to the object, you need to turn around enough times to get to the level the object is at.

Anyway, enough fantasy, here's the code:
 D3DXMATRIX matView; // the view transform matrix static float LookX = 0.0f; LookX -= mousestate.lX * 0.0075f; static float LookY = 0.0f; LookY -= mousestate.lY * 0.0075f; D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3 (0.0f, 0.0f, 15.0f), // the camera position &D3DXVECTOR3 (LookX, LookY, 0.0f), // the look-at position &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView 

What should I do to fix this problem? I'd rather not start guessing on my own this time, but if I should stay vague, then I think I would probably need to do some checks on the LookX and LookY values??
------------------------------

I'm using directX 9 and Visual studio 2008

Cheers,
Theo

##### Share on other sites
You're moving your look position along an infinite line through space, not a circle.

##### Share on other sites

You're moving your look position along an infinite line through space, not a circle.

Yes, it seems like so. I dont exactly understand why the program does that. I seem to be lacking some knowledge on how the API works.
Could you fill me in on the information?

##### Share on other sites

What you want is something like this:

horizontal_angle += mouse.deltaX * some_factor; lookat.x = sin(horizontal_angle) * some_distance_value; lookat.y = cos(horizontal_angle) * some_distance_value;

I may have swapped the sin/cos and you may want z instead of y (I can never remember offhand which axis is which in D3D), so experiment a bit :-)

##### Share on other sites
In D3D Y is the "up" axis, and XZ is the "horizontal" plane.

##### Share on other sites

What you want is something like this:

horizontal_angle += mouse.deltaX * some_factor; lookat.x = sin(horizontal_angle) * some_distance_value; lookat.y = cos(horizontal_angle) * some_distance_value;

I may have swapped the sin/cos and you may want z instead of y (I can never remember offhand which axis is which in D3D), so experiment a bit :-)

Based on what you wrote, I tried this and even some experimenting
 D3DXMATRIX matView; // the view transform matrix static float horizontal_angle = 0.0f; horizontal_angle += mousestate.lX * 0.0075f; //what is this one_factor?? static float vertical_angle = 0.0f; vertical_angle += mousestate.lY * 0.0075; static float LookX = 0.0f; LookX = sin(horizontal_angle) * 0.0075f; static float LookY = 0.0f; LookY = cos(vertical_angle) * 0.0075f; static float MoveX = 0.0f; static float MoveY = 0.0f; if(keystate[DIK_A] & 0x80) { MoveX += 0.3f; //LookX = MoveX; } if(keystate[DIK_D] & 0x80) { MoveX -= 0.3f; //LookX = MoveX; } if(keystate[DIK_S] & 0x80) { MoveY -= 0.3f; //LookY = MoveY; } if(keystate[DIK_W] & 0x80) { MoveY += 0.3f; //LookY = MoveY; } D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3 (MoveX, MoveY, 15.0f), // the camera position &D3DXVECTOR3 (LookX, LookY, 0.0f), // the look-at position &D3DXVECTOR3 (0.0f, 1.0f, 0.0f)); // the up direction d3ddev->SetTransform(D3DTS_VIEW, &matView); // set the view transform to matView 

I did add some movement code, but it has nothing to do with the mouse movement right now.
The thing is, the camera now doesn't budge at all ( Y ) or desperately tries and stays put ( X ).

I do know my math, but I am learning programming independently and so I do not know how to use such geometry math
in my programs.

I certainly dont know how cinus and cosinus fits in there.
------------------

I would still say that it's the API that has no sense of space.
What I would do is think for a week and come up with an algorithm for D3D to understand
where the objects are relative to the camera.

But like you already pointed out, I am wrong about that aspect and need to learn some geometry.

So what did I do wrong in my program?

Theo

##### Share on other sites
Okay, after I put back my original code, I experimented a bit and found out that direct3D does not let me look away from the object too far. Or rather, after a certain point,
the camera looking movement slows extremely down. So how would I go about fixing that?

##### Share on other sites
Try multiplying the sin/cos by something large, not something small. Chances are you're providing numbers which are too small.

Also, you might try increasing the factor you use to scale the angle; try larger and larger numbers until you can see results, then narrow it down to a range that gives you a good feel.

##### Share on other sites

Try multiplying the sin/cos by something large, not something small. Chances are you're providing numbers which are too small.

Also, you might try increasing the factor you use to scale the angle; try larger and larger numbers until you can see results, then narrow it down to a range that gives you a good feel.

Okay, this actually works, but not the way I wanted it to.
What it does is that it goes to a certain degree and then comes back the same way.
I wanted a full 360 degree turn, while now it only goes to a certain point and comes back.

##### Share on other sites
So back to the real problem..Why does the mouse movement become almost unmovable after a certain point away from the object?

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 10
• 9
• 34
• 16
• ### Forum Statistics

• Total Topics
634124
• Total Posts
3015664
×