Restricting Camera

Started by
17 comments, last by Tom Sloper 11 years, 3 months ago

?I'm not sure how I can use the way you mentioned, maybe you can explain more programmatically instead of mathematically.


We are actually doing you a favor by explaining the theory, instead of just giving you code. L. Spiro has told you several times to clamp your pitch, so do that. You cannot have your look-vector be the same as the up-vector and calculate a right vector the way you are doing. Because the vectors are the same, you cannot do a cross product on them (or you can, but it will be zero). This means that you have limit the pitch to something smaller than 90 degrees, if your up-vector is 90 degrees (eg. upwards).
Advertisement
I want to mention that the my camera update code is probably different than what you expect, please take a look over here:

void updateCamera()
{
if ( D3DXVec3Length( &m_velocity ) > m_maxVelocity )
{
m_velocity = *(D3DXVec3Normalize( &m_velocity, &m_velocity )) * m_maxVelocity;
}

m_position += m_velocity;
m_velocity = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
m_lookAt = m_position + m_look;

// Calculate the new view matrix
D3DXVECTOR3 up = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
D3DXMatrixLookAtLH( &m_view, &m_position, &m_lookAt, &up );

// Set the camera axes from the view matrix
m_right.x = m_view._11;
m_right.y = m_view._21;
m_right.z = m_view._31;
m_up.x = m_view._12;
m_up.y = m_view._22;
m_up.z = m_view._32;
m_look.x = m_view._13;
m_look.y = m_view._23;
m_look.z = m_view._33;

// Calculate yaw and pitch
float lookLengthOnXZ = sqrtf( m_look.z * m_look.z + m_look.x * m_look.x );
m_pitch = atan2f( m_look.y, lookLengthOnXZ );
}


The FPS camera is working perfectly, except the little problem that I mentioned (When the up angle is 90 degree and the player try to rotate the mouse up more, right/left start to work which show that the player is rotating around himself).

I have uploaded a video to explain the problem better:
[media]
[/media]
Are you seriously unable to add semicolons for yourself?

I said this:
m_vLook.x = sin( ? ) * cos( ? )
m_vLook.z = sin( ? ) * sin( ? )
m_vLook.y = cos( ? )

Which becomes this in code:
m_vLook.x = sin( i ) * cos( a );
m_vLook.z = sin( i ) * sin( a );
m_vLook.y = cos( i );

Where i was “inclination” (the radians up or down your player faces) (notice how I said it was “inclination” and then gave the variable “i” as a substitute) and “a” is azimuth (the radians horizontally your player faces) and you weren’t able to figure out how to convert symbols to letters and add semicolons?
It was code from the first time I posted it.
Pick your variable for ? (I chose i but it could be anything), pick your variable for ? (I chose a but it could be anything), and add semicolons. Seriously?
You already know that ? is the vertical angle and ? is the horizontal angle. Moving the mouse up and down changes ? and moving it sideways changes ?. Cap vertical movement? Cap ?.


I am sorry, but how am I supposed to make this any more obvious?
I gave you the code from the start. You took one glance, saw a single symbol that was not C++, and decided it was a useless post. “Please give me code,” you instantly replied, yet the code was already there.
This is exactly what almost every book is going to give you, so if you can only handle to be spoon-fed I am not sure how far you will be able to go as a programmer.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid


This is exactly what almost every book is going to give you, so if you can only handle to be spoon-fed I am not sure how far you will be able to go as a programmer.

[/quote]

@L. Spiro:
I don't appreciate your reply, I have been programming for 10 years in around 12 different programming/scripting languages and I'm completely new to DirectX and 3D games programming.

You seriously need to never judge other people.

Anyway, Thanks for trying to help, the question was resolved by simply decreasing max_pitch value.
Time spent doing something isn't necessarily the best metric for skill, especially in an ambiguously multi-disciplinary field like programming. Given some of your recent threads and the kind of feedback you give in discussion, I'm surprised to hear you're a seasoned programmer with 10 years of experience.

Spiro knows their stuff, and explained what you needed the first time around. Computer science and programming is effectively a subset and intersection of branches of mathematics, so if you've been programming for 10 years and that should indicate "how far you've gone" as a programmer, you should be able to make use of a mathematical explanation. Not to mention, 3d programming and graphics is HUGELY math-driven. Your learning process is going to be pretty blind until you grasp the mechanics behind the functions you're calling.

Looking a several of your recent threads, you're missing a lot of the "why" and theory behind what you're trying to accomplish, which is making your task much harder. I'd embrace any assistance that tries to teach you theory and reasoning over directly handing you code.

Tip: "You seriously need to" ditch the ego.

As for your camera issues, I know you've solved the problem but here's another approach that I think is simpler to manage: create a rotation matrix that represents the camera's per-axis rotations (yawpitchroll in some form) which can be calculated off stored rotation values. Here's where the clamp can be applied to your pitch, as well. Then every update call, just apply the newly calculated rotation matrix to a unit forward, up, and right vector (unit x, y, and z respectively) and use those to create your look-at (view) matrix.

...And in retrospect I just described MJP's approach. Hooray redundancy.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

Just to let you know, I started programming since I was around 10-11 years old, right now I'm 20, have been focusing on software and web development much more than mathematics, I know 3D games programming has alot of math involved and physics as well, I'm in hurry to complete the 3D Game Engine that I'm working on.

Thanks everyone,
I started programming since I was around 10-11 years old

Whee-hee. So did many of the other users here. Heck, I'm pretty sure some of the people who post around here are STILL 10-11 years old. However, as stated before, you are asking for someone to just give you the code you want. All that will do is force you to come back to the forums and beg when you can't do the same thing later on. MJP gave you a REALLY easy way to do it, L. Spiro, although somewhat curtly, gave you the answer you needed as well. Just... Listen.

"Only idiots quote themselves" - MisterFuzzy

@MisterFuzzy: You really need to show some respect to other members, the problem was resolved weeks ago and I have reported your reply and yes I tried L. Spiro solution and it didn't work as expected.

That's enough. Closing this down.

-- Tom Sloper -- sloperama.com

This topic is closed to new replies.

Advertisement