D3D Quake-cam Crashing
Below is a snip of code from my test program to move a cam. Basically, when you press Up or Down, the cam moves higher and lower while keeping the eye on the target. Left and Right strafes around the target. In and Out moves toward and away from the target. I'm having major issues with this...maybe I could get some help here.
Problems:
-When I zoom in and out, the camera also strafes slightly. I don't want this.
-When I zoom out too far, I want it to stop. I put a chunk of code at the bottom to try to do this, but the game crashes whenever it happens.
So with no further adeau, the code:
void CCamera::MoveCam(int Direction,float Distance)
{
//TargetPos = Hero.Pos;
CamDirection.x = TargetPos.x - CamPos.x;
CamDirection.y = TargetPos.y - CamPos.y;
CamDirection.z = TargetPos.z - CamPos.z;
//Normalize the direction
Math.normalize(CamDirection);
Strafe.x = (CamDirection.y * UpPos.z) - (CamDirection.z * UpPos.y);
Strafe.y = (CamDirection.z * UpPos.x) - (CamDirection.x * UpPos.z);
Strafe.z = (CamDirection.x * UpPos.y) - (CamDirection.y * UpPos.x);
switch(Direction)
{
case CAM_UP:
CamPos.y += CAM_SPEED;
if(CamPos.y >= TargetPos.y+2.0f)
CamPos.y = TargetPos.y+2.0f;
break;
case CAM_DOWN:
CamPos.y -= CAM_SPEED;
if(CamPos.y <= TargetPos.y)
CamPos.y = TargetPos.y;
break;
case CAM_LEFT:
CamPos.x += Strafe.x * Distance;
CamPos.y += Strafe.y * Distance;
CamPos.z += Strafe.z * Distance;
break;
case CAM_RIGHT:
CamPos.x -= Strafe.x * Distance;
CamPos.y -= Strafe.y * Distance;
CamPos.z -= Strafe.z * Distance;
break;
case CAM_IN:
CamPos.x -= (TargetPos.x/CamPos.x-1.0f) * Distance;
CamPos.y -= (TargetPos.y/CamPos.y-1.0f) * Distance;
CamPos.z -= (TargetPos.z/CamPos.z-1.0f) * Distance;
break;
case CAM_OUT:
CamPos.x += (TargetPos.x/CamPos.x-1.0f) * Distance;
CamPos.y += (TargetPos.y/CamPos.y-1.0f) * Distance;
CamPos.z += (TargetPos.z/CamPos.z-1.0f) * Distance;
break;
}
if(Math.distance(CamPos,TargetPos)>7.0f)
MoveCam(CAM_IN,10.0f);
}
Okay I'm open to better code ideas.
Also~ if ANYONE knows of a way to program a chase-cam, I'd like to see it. I tried searching in the forums, but kept getting an error with the search page.
One other problem I'm having: When I hold down the left arrow (to strafe left), my cam chops, then goes smoothly...like as if it's doing a windows key repeat rate thingy. Here's an example of how I have it set up:
KEYSTATE(VK_LEFT) ? Controls.CamLeft.bPressed=TRUE : Controls.CamLeft.bPressed=FALSE;
if(Controls.CamLeft.bPressed==TRUE)
Cam.MoveCam(CAM_LEFT);
If you have any knowledge on this one, I'm open. Thanks.
Dravidian Kayne~
PS: If anyone can tell me how to put my code into a code box, I'd appreciate it.
[edited by - Dravidian Kayne on May 19, 2004 2:39:03 AM]
[edited by - Dravidian Kayne on May 19, 2004 2:39:55 AM]
Uh if your cam in/out simply moves the cam towards/away from the target then why don't you use cam (+/-) CamDirection * speed, since you have computed camdirection as the normalised direction vector from the camera to the target.
Also are you using directinput for your key state capturing? I do and am not experiencing any problems or choppiness from key input.
EDIT:
Also for forum flags etc:
http://www.gamedev.net/community/forums/faq.asp
[edited by - SoulSpectre on May 19, 2004 2:46:31 AM]
Also are you using directinput for your key state capturing? I do and am not experiencing any problems or choppiness from key input.
EDIT:
Also for forum flags etc:
http://www.gamedev.net/community/forums/faq.asp
[edited by - SoulSpectre on May 19, 2004 2:46:31 AM]
The crash when zooming too far out is because you have an infinte loop. You checking if the zoom is greater than 7 but then your setting the zoom to 10 and calling function again so it keeps calling the same function over and over again until you get a stack overflow and it crashes.
-Dan
-Dan
Thanks for the help. I haven''t implimented DirectInput yet, and maybe I should stop what I''m doing and impliment it before the app gets too big.
Well anyways, I pulled out my Game Coding Complete book and read up a little on Chaper 9: 3D Graphics All Game Programmers Must Master (in case anyone else has the book you can look this up as well). Now things make a little more sense.
Dravidian Kayne~
Well anyways, I pulled out my Game Coding Complete book and read up a little on Chaper 9: 3D Graphics All Game Programmers Must Master (in case anyone else has the book you can look this up as well). Now things make a little more sense.
Dravidian Kayne~
Your camera chops because of how windows handles keyboard input. Try to keep pressed a letter in an edit box: there is a small time before the letter repeats. Windows waits a little before sending another WM_KEYDOWN message. There are ways to get around this, anyway: you can use a bool to track the real key state, that is, when you receive a WM_KEYDOWN message you set it to true, and when you get a WM_KEYUP you set it to false. Then you just check this value: when it is true, the key is pressed, when it is false it''s not.
Here''s the code I have for my input in my Controls.GetInput() function:
KEYSTATE(VK_UP) ? Controls.MoveUp.bPressed=TRUE : Controls.MoveUp.bPressed=FALSE;
Then in my ProcessInput() function I have:
if(Controls.CamUp.bPressed==TRUE)
Cam.MoveCam(CAM_UP);
And here''s my KEYSTATE(vknum) macro:
#define KEYSTATE(vknum) ((GetKeyState(vknum) & 0x80) ? TRUE : FALSE)
I don''t understand why it''s chopping even under the regular Windows API.
Dravidian Kayne~
KEYSTATE(VK_UP) ? Controls.MoveUp.bPressed=TRUE : Controls.MoveUp.bPressed=FALSE;
Then in my ProcessInput() function I have:
if(Controls.CamUp.bPressed==TRUE)
Cam.MoveCam(CAM_UP);
And here''s my KEYSTATE(vknum) macro:
#define KEYSTATE(vknum) ((GetKeyState(vknum) & 0x80) ? TRUE : FALSE)
I don''t understand why it''s chopping even under the regular Windows API.
Dravidian Kayne~
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement