Jump to content
  • Advertisement
Sign in to follow this  
andrew_wardobe

Acess violation error

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

i get an acess violation error when i try to run my code i know from expierence that this normally when i try to access uninitailed pointers however from debuging the code the error seems to trigger when i access the variable m_yaw which is a floati n the animate function of my camera class. does anyone why this is happening and/or how i can fix it here the code camera.h:
#ifndef CAMERA_H_
#define CAMERA_H_
#include "core.h"

class CObject;

class CCamera
{
	CVector iPos, fPos; //initial and final position
	CVector iLook, fLook; //initial and final lookats

	CVector m_lookVel; //velocity for looking at objects
	CVector m_lookAccel; //acelleration for looking a objects

	void UpdateLookAt();
	void UpdateMoveTo();

public:
	CVector m_Pos;  //Position of Camera
	CVector m_Vel;  //Cameras velocity
	CVector m_accel;  //accelleration of Camera
	CVector m_look;  //look at 

	CVector m_up, m_forward, m_right; //forward up and right
	
	float m_yaw;
	float m_pitch; //Yaw and pitch angles

	CCamera(void);
	CCamera(int width, int height) {}
	CCamera(CVector *look);
	CCamera(CVector *pos,CVector *look);
	~CCamera(void);

	void LookAt(CObject *obj);  //look at object over time
	void LookAtNow(CObject *obj); //snap view to object
	void MoveTo(CObject *obj);  //move to object
	void MoveToNow(CObject *obj); //move instantly to object
	void MoveToNow(scalar_t x, scalar_t y, scalar_t z); //move instantly to location

    void RotateYaw(scalar_t rads); // rotate along y Axis
	void RotatePitch(scalar_t rads); //rotate along x Axis
	void RotateRoll(scalar_t rads); //rotate along z Axis

	void Animate(float deltaTime);  //Perform Physic Calulations
};

#endif


camera.cpp:
#include "camera.h"
#include "object.h"
#include <math.h>

CCamera::CCamera()
{
	m_Pos = CVector(0.0, 0.0, 0.0);
	m_look = CVector(0.0, 0.0, 1.0);

	m_forward = m_look;
	m_up = CVector(0.0, 1.0, 0.0);
	m_right = CVector(1.0, 0.0, 0.0);

	m_Vel = CVector(0.0, 0.0, 0.0);
	m_accel = CVector(0.0, 0.0, 0.0);

	m_yaw = 0.0f;
	m_pitch = 0.0f;
}

CCamera::CCamera(CVector *look)
{
	m_Pos = CVector(0.0, 0.0, 0.0);
	m_look = look->UnitVector();

	m_forward = m_look;
	m_up = CVector(0.0, 1.0, 0.0);
	m_right = m_forward.CrossProduct(m_up); //CVector(1.0, 0.0, 0.0);

	m_Vel = CVector(0.0, 0.0, 0.0);
	m_accel = CVector(0.0, 0.0, 0.0);	

	m_yaw = 0.0f;
	m_pitch = 0.0f;
}

CCamera::CCamera(CVector *pos, CVector *look)
{
	m_Pos = *pos;
	m_look = look->UnitVector();

	m_forward = m_look;
	m_up = CVector(0.0, 1.0, 0.0);
	m_right = CVector(1.0, 0.0, 0.0);

	m_Vel = CVector(0.0, 0.0, 0.0);
	m_accel = CVector(0.0, 0.0, 0.0);

	m_yaw = 0.0f;
	m_pitch = 0.0f;
}

CCamera::~CCamera()
{
}

void CCamera::UpdateLookAt()
{
	CVector look = CVector(fLook.x - m_look.x,
					   fLook.y - m_look.y,
					   fLook.z - m_look.z);

	m_lookVel = look * 0.5;
}

void CCamera::UpdateMoveTo()
{
	CVector pos = CVector(fPos.x - m_Pos.x,
					  fPos.y - m_Pos.y,
					  fPos.z - m_Pos.z);

	m_Vel = pos * 0.5;
}

void CCamera::RotatePitch(scalar_t radians)
{
	float sine = sinf(radians);
	float cosine = cosf(radians);

	m_up.y = cosine * m_up.Length();
	m_up.z = sine * m_up.Length();

	m_forward.y = -sine * m_forward.Length();
	m_forward.z = cosine * m_forward.Length();
	/*     x   y      z       p
         |  1  0       0       0 |
     M = |  0  cos(A) -sin(A)  0 |
         |  0  sin(A)  cos(A)  0 |
         |  0  0       0       1 |
	*/
}

void CCamera::RotateYaw(scalar_t radians)
{
	float sine = sinf(radians);
	float cosine = cosf(radians);
	
	m_right.x = cosine * m_right.Length();
	m_right.z = sine * m_right.Length();

	m_forward.x = -sine * m_forward.Length();
	m_forward.z = cosine * m_forward.Length();
	
	/*	   x      y    z      p
         |  cos(A)  0  -sin(A)  0 |
     M = |  0       1   0       0 |
         |  sin(A)  0   cos(A)  0 |
         |  0       0   0       1 |
	*/

}

void CCamera::RotateRoll(scalar_t radians)
{
	float sine = sinf(radians);
	float cosine = cosf(radians);
	
	m_right.x = cosine * m_right.Length();
	m_right.y = sine * m_right.Length();

	m_up.x = -sine * m_forward.Length();
	m_up.y = cosine * m_forward.Length();
	/*
         |  cos(A)  -sin(A)   0   0 |
     M = |  sin(A)   cos(A)   0   0 |
         |  0        0        1   0 |
         |  0        0        0   1 |
	*/
}


void CCamera::LookAtNow(CObject *object)
{
	m_look = object->m_pos;
}

void CCamera::MoveToNow(scalar_t x, scalar_t y, scalar_t z)
{
	m_Pos.x = x;
	m_Pos.y = y;
	m_Pos.z = z;
}


void CCamera::MoveToNow(CObject *object)
{
	m_Pos = object->m_pos;
}

// look at an object over time
void CCamera::LookAt(CObject *object)
{
	CVector v = CVector(object->m_pos - m_look);

	iLook = m_look;
	fLook = object->m_pos;

	m_lookAccel = -m_look * 0.25f;

	UpdateLookAt();
}

// move to an object over time
void CCamera::MoveTo(CObject *object)
{
	CVector v = CVector(object->m_pos - m_Pos);

	iPos = m_Pos;
	fPos = object->m_pos;

	m_accel = -m_Pos * 0.25f;

	UpdateMoveTo();
}


void CCamera::Animate(scalar_t deltaTime)
{
	if ((m_yaw >= 360.0f) || (m_yaw <= -360.0f))
          m_yaw = 0.0f;

	 //Controls Maxium and minium angle of camera
     if (m_pitch > 60.0f)
          m_pitch = 60.0f;		
     if (m_pitch < -60.0f)
          m_pitch = -60.0f;
     
     float cosYaw = (scalar_t)cos(DEG2RAD(m_yaw));
     float sinYaw = (scalar_t)sin(DEG2RAD(m_yaw));
     float sinPitch = (scalar_t)sin(DEG2RAD(m_pitch));

	 // added line
	 float cosPitch = (scalar_t)cos(DEG2RAD(m_pitch));

     float speed = m_Vel.z * deltaTime;
     float strafeSpeed = m_Vel.x * deltaTime;

     if (speed > 15.0)
          speed = 15.0;
     if (strafeSpeed > 15.0)
          strafeSpeed = 15.0;
     if (speed < -15.0)
          speed = -15.0;
     if (strafeSpeed < -15.0)
          strafeSpeed = -15.0;

     if (m_Vel.Length() > 0.0)
          m_accel = -m_Vel * 1.5f;

     m_Vel += m_accel*deltaTime;

     m_Pos.x += float(cos(DEG2RAD(m_yaw + 90.0)))*strafeSpeed;
     m_Pos.z += float(sin(DEG2RAD(m_yaw + 90.0)))*strafeSpeed;
     m_Pos.x += float(cosYaw)*speed;
     m_Pos.z += float(sinYaw)*speed;

	 // added *cosPitch
     m_look.x = float(m_Pos.x + (cosYaw*cosPitch));
     m_look.y = float(m_Pos.y + sinPitch);
     m_look.z = float(m_Pos.z + (sinYaw*cosPitch));

     gluLookAt(m_Pos.x, m_Pos.y, m_Pos.z,
               m_look.x, m_look.y, m_look.z,
               0.0, 1.0, 0.0);
}



i would grateful any help andrew

Share this post


Link to post
Share on other sites
Advertisement
It is likely that the value of this is 0 or garbage. For example, if the code crashes when accessing m_yaw in this line:
    pCamera->Animate( time ); 
the value of pCamera is probably 0 or garbage.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!