Skip to main content
GameDev.net gamedev.net
🔒 Locked

Player Class

Started by Kris2456 Jun 7, 2004 at 2:08 PM 1 replies 450+ views
Original Post
Kris2456
Kris2456
Ive recently made a class to draw my player on the screen. But wheni run it, all i get is my player in the middle, andmy camera spins around it when i move the mouse. Iknow im close but i really hate the camera, and the way the slightet thing screws it up, pleez help. Here is my code:
//Constructor

CPlayer::CPlayer()
{
    double PlayerX = 0;
    double PlayerY = 0;
    double PlayerZ = 0;
    
    double PlayerRX = 0;
    double PlayerRY = 0;
    double PlayerRZ = 0;
    
    CVector3 vView = CVector3(0.0, 0.0, -1.0);
}

//Get Functions

double CPlayer::GetPlayerX()
{
    return PlayerX;
}
double CPlayer::GetPlayerY()
{
    return PlayerY;
}
double CPlayer::GetPlayerZ()
{
    return PlayerZ;
}

//Set position

void CPlayer::SetPlayerPosition(double X, double Y, double Z)
{
    PlayerX = X;
    PlayerY = Y;
    PlayerZ = Z;
}

//Move Player

void CPlayer::MovePlayer(float speed)
{
    PlayerX += vView.x*speed;
    PlayerY += vView.y*speed;
    PlayerZ += vView.z*speed;
}

//Rotate Player

void CPlayer::RotatePlayer(float angleX, float angleY, float angleZ)
{
    PlayerRX = angleX;
    PlayerRY = angleY;
    PlayerRZ = angleZ;
}

//Set View Vector

void CPlayer::SetViewVector()
{
    POINT mouse;
    GetCursorPos(&mouse);
    vView.x = (float)(400 - mouse.x)/100;
    vView.y = (float)(300 - mouse.y)/100;
}

//Draw Player

void CPlayer::DrawPlayer()
{
    glTranslated(PlayerX,PlayerY,PlayerZ);
    glRotatef(PlayerRX,1,0,0);
    glRotatef(PlayerRY,0,1,0);
    glRotatef(PlayerRZ,0,0,1);
    TieFighter();
}

CPlayer::~CPlayer()
{
}

And my Main.cpp code:
   
case GAME:
glLoadIdentity();	  			  
   	gluLookAt(256, 30, 256, g_Player.GetPlayerX(),g_Player.GetPlayerY(), g_Player.GetPlayerZ(),	0, 1, 0);
			  
	 
 	g_Player.DrawPlayer();
 	g_Player.SetViewVector();
 	g_Player.MovePlayer(speed);

...
EDIT: plz dont flame me if this is a crap way of doing it, im not good with vectors as such, and this code is based of GameTutorials.com's Camera Class. ------------ "Here lies a toppled God, His fall was not a small one, We but built his pedastle, A narrow, and a tall one" Frank Herbert (Dune:Messiah) [edited by - Kris2456 on June 7, 2004 3:11:56 PM]
------------ "Here lies a toppled God,His fall was not a small one,We but built his pedastle,A narrow, and a tall one" Frank Herbert (Dune:Messiah)
daedalux
daedalux
If I''m not mistaken, I think your problem may be here:

//Draw Player
void CPlayer::DrawPlayer()
{
glTranslated(PlayerX,PlayerY,PlayerZ);
glRotatef(PlayerRX,1,0,0);
glRotatef(PlayerRY,0,1,0);
glRotatef(PlayerRZ,0,0,1);
TieFighter();
}

You say that the camera just spins around your player, I think that is because you rotate the camera (or scene, I''m not sure which ^_^ ) and then leave the rotation to apply to everything else being drawn. I think what you need to do is first push a copy of the current matrix onto the stack, do the rotation & drawing and then pop the original matrix back off the stack so that the original point of view is restored. Here''s what I mean:

//Draw Player
void CPlayer::DrawPlayer()
{
glPushMatrix(); // Save current point of view

glTranslated(PlayerX,PlayerY,PlayerZ);
glRotatef(PlayerRX,1,0,0);
glRotatef(PlayerRY,0,1,0);
glRotatef(PlayerRZ,0,0,1);
TieFighter();

glPopMatrix(); // Restore that point of view
}

Hope that helps!

-dade
Kris2456
Kris2456
YES, thats it. Ity works now, altho it dosntmove forawrds for some reason, but i will fix that. Im really thankful 4 ur help. cheers

------------

"Here lies a toppled God,
His fall was not a small one,
We but built his pedastle,
A narrow, and a tall one"
Frank Herbert (Dune:Messiah)
------------ "Here lies a toppled God,His fall was not a small one,We but built his pedastle,A narrow, and a tall one" Frank Herbert (Dune:Messiah)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.