Jump to content

  • Log In with Google      Sign In   
  • Create Account

Syncing my camera to the FPS - returning weird error


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.

  • You cannot reply to this topic
3 replies to this topic

#1 Xooch   Members   -  Reputation: 179

Like
0Likes
Like

Posted 17 December 2012 - 04:18 AM

Hello,

I've recently tried to add some movement functions to my camera, all good so far, apart from in my "MoveCamera.cpp" class, one of my functions is breaking and returning a very weird error.

http://img89.imageshack.us/img89/9585/errorva.png

For the life of me I can't figure out why.

Here is where it is being called, in my graphics class

Graphics.cpp
/* Handle Input handles are user input via the keyboard */
bool Graphics::HandleInput(float frameTime)
{
bool keyDown;
float posX, posY, posZ;
float rotX, rotY, rotZ;
//Set the frame time used for updating positions
movecamera->SetFrameTime(frameTime);

And here is my MoveCamera class, including the header file

MoveCamera.h
//--------------------------------------------------\\
// MoveCamera.h - Allows the camera to move  \\
//			 \\
//--------------------------------------------------\\
#ifndef _MOVECAMERA_H
#define _MOVECAMERA_H
#include <math.h>
class MoveCamera
{
public:
  MoveCamera();
  ~MoveCamera();
  void SetPos(float, float, float);
  void SetRot(float, float, float);
  void GetPos(float&, float&, float&);
  void GetRot(float&, float&, float&);
  void SetFrameTime(float);   //Keep the camera in sync with the application
  /* Movement Keys */
  void MoveForward(bool);   //W
  void MoveBackwards(bool);  //S
  void MoveUp(bool);    //Q
  void MoveDown(bool);   //E
  /* Rotation keys */
  void TurnLeft(bool);   //A
  void TurnRight(bool);   //D
  void LookUp(bool);    //Up Arrow
  void LookDown(bool);   //Down Arrow
private:
  float posX, posY, posZ;
  float rotX, rotY, rotZ;
  float frameTime;
  float forwardSpeed;    //W
  float backwardSpeed;   //S
  float upwardSpeed;    //Q
  float downwardSpeed;   //E
  float LookLeftSpeed;   //A
  float LookRightSpeed;   //D
  float LookDownSpeed;   //Up Arrow
  float LookUpSpeed;    //Down Arrow
};
#endif



MoveCamera.cpp
//--------------------------------------------------\\
// MoveCamera.cpp - Allows the camera to move  \\
//			 \\
//--------------------------------------------------\\
#include "MoveCamera.h"
/* Constructor & Deconstructor */
MoveCamera::MoveCamera()
{
posX   = 0.0f;
posY   = 0.0f;
posZ   = 0.0f;
rotX   = 0.0f;
rotY   = 0.0f;
rotZ   = 0.0f;
frameTime  = 0.0f;
forwardSpeed = 0.0f;
backwardSpeed = 0.0f;
upwardSpeed  = 0.0f;
downwardSpeed = 0.0f;
LookLeftSpeed = 0.0f;
LookRightSpeed = 0.0f;
LookDownSpeed = 0.0f;
LookUpSpeed  = 0.0f;
}
MoveCamera::~MoveCamera(){}
/* Set the camera Position */
void MoveCamera::SetPos(float x, float y, float z)
{
posX = x;
posY = y;
posZ = z;
return;
}
/* Set the camera Rotation */
void MoveCamera::SetRot(float x, float y, float z)
{
rotX = x;
rotY = y;
rotZ = z;
return;
}
/* Get the camera Position */
void MoveCamera::GetPos(float& x, float& y, float& z)
{
x = posX;
y = posY;
z = posZ;
return;
}
/* Get the camera Rotation */
void MoveCamera::GetRot(float& x, float& y, float& z)
{
x = rotX;
y = rotY;
z = rotZ;
return;
}
/* Set Frame Time to sync camera movement to FPS */
void MoveCamera::SetFrameTime(float time)
{
frameTime = time;
return;
}
/*
The functions here use simple math to give the movement a realistic acceleration.
The speed of each direction is added to the frameTime to sync is probably.
Maths adopted from the Rastertek.com tutorials.
*/
/* Move Forward */
void MoveCamera::MoveForward(bool keydown)
{
float rads;
if(keydown)
{
  forwardSpeed += frameTime * 0.001f;
  if(forwardSpeed > (frameTime * 0.03f))
  {forwardSpeed = frameTime * 0.03f;}
}
else
{
  forwardSpeed -= frameTime * 0.0007f;
  if(forwardSpeed < 0.0f)
  {forwardSpeed = 0.0f;}
}
//Update new position
rads = rotY * 0.0174532925f;
posX += sinf(rads) * forwardSpeed;
posZ += cosf(rads) * forwardSpeed;
return;
}
/* Move Backwards */
void MoveCamera::MoveBackwards(bool keydown)
{
float rads;
if(keydown)
{
  backwardSpeed += frameTime * 0.001f;
  if(backwardSpeed > (frameTime * 0.03f))
  {backwardSpeed = frameTime * 0.03f;}
}
else
{
  backwardSpeed -= frameTime * 0.0007f;
  if(backwardSpeed < 0.0f)
  {backwardSpeed = 0.0f;}
}
//Update new position
rads = rotY * 0.0174532925f;
posX -= sinf(rads) * backwardSpeed;
posZ -= cosf(rads) * backwardSpeed;
return;
}
/* Move Upwards */
void MoveCamera::MoveUp(bool keydown)
{
if(keydown)
{
  upwardSpeed += frameTime * 0.003f;
  if(upwardSpeed > (frameTime * 0.03f))
  {upwardSpeed = frameTime * 0.03f;}
}
else
{
  upwardSpeed -= frameTime * 0.002f;
  if(upwardSpeed < 0.0f)
  {upwardSpeed = 0.0f;}
}
//Update new position
posY += upwardSpeed;
return;
}
/* Move Downwards */
void MoveCamera::MoveDown(bool keydown)
{
if(keydown)
{
  downwardSpeed += frameTime * 0.003f;
  if(downwardSpeed > (frameTime * 0.03f))
  {downwardSpeed = frameTime * 0.03f;}
}
else
{
  downwardSpeed -= frameTime * 0.002f;
  if(downwardSpeed < 0.0f)
  {downwardSpeed = 0.0f;}
}
//Update new position
posY -= downwardSpeed;
return;
}
/* Rotate Left */
void MoveCamera::TurnLeft(bool keydown)
{
if(keydown)
{
  LookLeftSpeed += frameTime * 0.01f;
  if(LookLeftSpeed > (frameTime * 0.15f))
  {LookLeftSpeed = frameTime * 0.15f;}
}
else
{
  LookLeftSpeed -= frameTime * 0.005f;
  if(LookLeftSpeed < 0.0f)
  {LookLeftSpeed = 0.0f;}
}
//Update new position
rotY -= LookLeftSpeed;
//Keep the value of rotY between 0-360
if(rotY < 0.0f)
{rotY += 360.0f;}
return;
}
/* Rotate Right */
void MoveCamera::TurnRight(bool keydown)
{
if(keydown)
{
  LookRightSpeed += frameTime * 0.01f;
  if(LookRightSpeed > (frameTime * 0.15f))
  {LookRightSpeed = frameTime * 0.15f;}
}
else
{
  LookRightSpeed -= frameTime* 0.005f;
  if(LookRightSpeed < 0.0f)
  {LookRightSpeed = 0.0f;}
}
//Update new position
rotY += LookRightSpeed;
//Keep the value of rotY between 0-360
if(rotY > 360.0f)
{rotY -= 360.0f;}
return;
}
/* Rotate Down */
void MoveCamera::LookDown(bool keydown)
{
if(keydown)
{
  LookDownSpeed += frameTime * 0.01f;
  if(LookDownSpeed > (frameTime * 0.15f))
  {LookDownSpeed = frameTime * 0.15f;}
}
else
{
  LookDownSpeed -= frameTime* 0.005f;
  if(LookDownSpeed < 0.0f)
  {LookDownSpeed = 0.0f;}
}
//Update the position
rotX += LookDownSpeed;
//Keep the rotation maximum 90 degrees
if(rotX < -90.0f)
{rotX = -90.0f;}
return;
}
/* Rotate Up */
void MoveCamera::LookUp(bool keydown)
{
if(keydown)
{
  LookUpSpeed += frameTime * 0.01f;
  if(LookUpSpeed > (frameTime * 0.15f))
  {LookUpSpeed = frameTime * 0.15f;}
}
else
{
  LookUpSpeed -= frameTime* 0.005f;
  if(LookUpSpeed < 0.0f)
  {LookUpSpeed = 0.0f;}
}
//Update the position
rotX -= LookUpSpeed;
//Keep the rotation maximum 90 degrees
if(rotX > 90.0f)
{rotX = 90.0f;}
return;
}

Could anyone shine some light on this situation? Thanks

Sponsor:

#2 BitMaster   Crossbones+   -  Reputation: 4088

Like
0Likes
Like

Posted 17 December 2012 - 04:32 AM

You are calling a member function of MoveCamera on a null pointer. You need to walk up the stack trace to find the culprit, although I assume Graphics::HandleInput line 345 is to blame. Either moveCamera should not be a pointer or you must allocate an instance using new before using it (and freeing it using delete) when you are done with it.

#3 Xooch   Members   -  Reputation: 179

Like
0Likes
Like

Posted 17 December 2012 - 06:17 AM

Thanks, Seem I was forgetting to initialize the MoveCamera class into Graphcics. Everything seems to be running error free now... Although when I use my keyboard to move the camera, nothing seems to be happening. Could you took a look at my input class for me and see if theres something up? This is my first time doing movement keys in C++

input.h
//--------------------------------------------------\\
// Input.h - handles user input	 \\
// Keyboard		  \\
//--------------------------------------------------\\
#ifndef _INPUT_H
#define _INPUT_H
//pre-processing directives
#define DIRECTINPUT_VERSION 0x0800
#pragma comment(lib, "dinput8.lib")
#pragma comment(lib, "dxguid.lib")
#include <dinput.h>
class Input
{
public:
Input();
~Input();
bool Init(HINSTANCE, HWND, int, int);
bool Frame();
void Shutdown();
bool IsEscapePressed();    //Esc for quitting
bool IsUpPressed();	 //Move camera up
bool IsDownPressed();    //Move camera down
bool IsWPressed();	 //Move camera forward
bool IsSPressed();	 //Move camera backwards
bool IsAPressed();	 //Rotate camera left
bool IsDPressed();	 //Rotate camera right
bool IsQPressed();	 //Rotate camera up
bool IsEPressed();	 //Rotate camera down
private:
bool ReadKeyboard();
IDirectInput8*   directInput;
IDirectInputDevice8* keyboard;
unsigned char keyboardState[256];
int rScreenWidth, rScreenHeight;
};
#endif

input.cpp
//--------------------------------------------------\\
// Input.cpp - handles user input	 \\
// Keyboard & Mouse devices	  \\
//--------------------------------------------------\\
#include "Input.h"
/* Constructor & Deconstructor */
Input::Input()
{
directInput = 0;
keyboard = 0;
}
Input::~Input(){}
/* Initialize Input Devices */
bool Input::Init(HINSTANCE hinstance, HWND hwnd, int screenWidth, int screenHeight)
{
HRESULT r;
//Store the screen size which will be used for positioning the mouse cursor.
rScreenWidth = screenWidth;
rScreenHeight = screenHeight;
//Initialize the input device - Keyboard
r = DirectInput8Create(hinstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&directInput, NULL);
if(FAILED(r))
{return false;}
//Create device for keyboard
r = directInput->CreateDevice(GUID_SysKeyboard, &keyboard, NULL);
if(FAILED(r))
{return false;}
//Set the data format
r = keyboard->SetDataFormat(&c_dfDIKeyboard);
if(FAILED(r))
{return false;}
//Set the key strokes to be exclusive to this application
r = keyboard->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE);
if(FAILED(r))
{return false;}
//Acquire keyboard
r = keyboard->Acquire();
if(FAILED(r))
{return false;}
return true;
}
/* Update and Process each frame the Input components */
bool Input::Frame()
{
bool r;
r = ReadKeyboard();
if(!r)
{return false;}
return true;
}
/* Read the Keyboard device */
bool Input::ReadKeyboard()
{
HRESULT r;
r = keyboard->GetDeviceState(sizeof(keyboardState), (LPVOID)&keyboardState);
if(FAILED(r))
{
  //If the keyboard lost focus or was not acquired then try to get control back.
  if((r == DIERR_INPUTLOST) || (r == DIERR_NOTACQUIRED))
  {keyboard->Acquire();}
  else
  {return false;}
}
 
return true;
}
/* Get Key presses */
/* Escape Key */
bool Input::IsEscapePressed()
{
if(keyboardState[DIK_ESCAPE] & 0x80)
{return true;}
return false;
}
/* Up Arrow Key */
bool Input::IsUpPressed()
{
if(keyboardState[DIK_UP] & 0x80)
{return true;}
return false;
}
/* Down Arrow Key */
bool Input::IsDownPressed()
{
if(keyboardState[DIK_DOWN] & 0x80)
{return true;}
return false;
}
/* W Key */
bool Input::IsWPressed()
{
if(keyboardState[DIK_W] & 0x80)
{return true;}
return false;
}
/* S Key */
bool Input::IsSPressed()
{
if(keyboardState[DIK_S] & 0x80)
{return true;}
return false;
}
/* A Key */
bool Input::IsAPressed()
{
if(keyboardState[DIK_A] & 0x80)
{return true;}
return false;
}
/* D Key */
bool Input::IsDPressed()
{
if(keyboardState[DIK_D] & 0x80)
{return true;}
return false;
}
/* Q Key */
bool Input::IsQPressed()
{
if(keyboardState[DIK_Q] & 0x80)
{return true;}
return false;
}
/* E Key */
bool Input::IsEPressed()
{
if(keyboardState[DIK_E] & 0x80)
{return true;}
return false;
}

/* Shutdown, Release and Delete the Input objects we are using */
void Input::Shutdown()
{
if(keyboard)
{
  keyboard->Unacquire();
  keyboard->Release();
  keyboard = 0;
}
if(directInput)
{
  directInput->Release();
  directInput = 0;
}
return;
}

The handle input function inside Graphics.cpp
/* Handle Input handles are user input via the keyboard */
bool Graphics::HandleInput(float frameTime)
{
bool keyDown;
float posX, posY, posZ;
float rotX, rotY, rotZ;
//Set the frame time used for updating positions
movecamera->SetFrameTime(frameTime);
//Handle the Key strokes
keyDown = input->IsAPressed();
movecamera->TurnLeft(keyDown);
keyDown = input->IsDPressed();
movecamera->TurnRight(keyDown);
keyDown = input->IsWPressed();
movecamera->MoveForward(keyDown);
keyDown = input->IsSPressed();
movecamera->MoveBackwards(keyDown);
keyDown = input->IsQPressed();
movecamera->MoveUp(keyDown);
keyDown = input->IsEPressed();
movecamera->MoveDown(keyDown);
keyDown = input->IsUpPressed();
movecamera->LookUp(keyDown);
keyDown = input->IsDownPressed();
movecamera->LookDown(keyDown);

//Get position & rotation
movecamera->GetPos(posX, posY, posZ);
movecamera->GetRot(rotX, rotY, rotZ);
//Set position of the camera
camera->SetPosition(posX, posY, posZ);
camera->SetRotation(rotX, rotY, rotZ);
return true;
}

Thanks

#4 yckx   Prime Members   -  Reputation: 1210

Like
0Likes
Like

Posted 17 December 2012 - 11:31 AM

Honestly, I'm not familiar with DirectInput, other than knowing that Microsoft recommends against using it for keyboard and mouse input. They recommend using standard Win32 methods for that. I cant really answer your specific problem with the input code, though.




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.



PARTNERS