I have the start of a simple game, but the movement of my character across the screen is slightly choppy. Also the mouth appears to curve also. My terminal says I have at least 60 FPS.
What sort of improvements can I do to my below code which calculates the players position and draws them.
#include <iostream>
#include <math.h>
#include <algorithm>
#include <sys/time.h>
#include "util.h"
#include "player.h"
#include "constants.h"
#include "room.h"
#include "SOIL.h"
using namespace std;
player::player(room &tmproom){
loaded=false;
troom = tmproom;
init();
}
void player::init(){
p_acel = 0.000008;
p_vterm = 0.0005;
p_fric = 0.008;
p_head = 0.08;
p_body = 0.06;
p_pos[0]=0.0;
p_pos[1]=0.0;
p_vel[0]=0.0;
p_vel[1]=0.0;
pt2=get_time();
t2=get_time();
}
void player::moveplayer(){
pt1=get_time();
float dt=pt1-pt2;
pt2=pt1;
t1 = get_time();
frames = frames + 1;
if ((t1 - t2) > 1000.0)
{
cout << "FPS "<<frames << endl;
t2 = t1;
frames=0;
}
if(but[0]){//a
p_vel[0] = p_vel[0] - 0.5*p_acel*dt*dt; //apply acceleration
if(p_vel[0] < -1*p_vterm) p_vel[0] = -1*p_vterm; //terminal v
}
if(but[1]){//w
p_vel[1] = p_vel[1] + 0.5*p_acel*dt*dt; //apply acceleration
if(p_vel[1] > p_vterm) p_vel[1] = p_vterm; //terminal v
}
if(but[2]){//d
p_vel[0] = p_vel[0] + 0.5*p_acel*dt*dt; //apply acceleration
if(p_vel[0] > p_vterm) p_vel[0] = p_vterm; //terminal v
}
if(but[3]){//s
p_vel[1] = p_vel[1] - 0.5*p_acel*dt*dt; //apply acceleration
if(p_vel[1] < -1*p_vterm) p_vel[1] = -1*p_vterm; //terminal v
}
//normalize vector
float mag = sqrt(p_vel[0]*p_vel[0] + p_vel[1]*p_vel[1]);
if(mag > p_vterm){
p_vel[0] = p_vterm*p_vel[0]/mag;
p_vel[1] = p_vterm*p_vel[1]/mag;
}
// update positions
p_pos[0] = p_pos[0] + p_vel[0]*dt;
//check collisions
solid_collide(true,false,dt);
p_pos[1] = p_pos[1] + p_vel[1]*dt;
//check collisions
solid_collide(false,true,dt);
// cout << t1 << " " << p_pos[0] << endl;
//apply friction
// p_vel[0] = p_vel[0] - copysign( p_fric*dt*dt,p_vel[0]);
// p_vel[1] = p_vel[1] - copysign( p_fric*dt*dt,p_vel[1]);
//kill small velocities
if(fabs(p_vel[0]) < 0.9*p_acel*dt*dt) p_vel[0] = 0.0;
if(fabs(p_vel[1]) < 0.9*p_acel*dt*dt) p_vel[1] = 0.0;
}
void player::drawplayer(){
glLoadIdentity();
if(!loaded){
playerid[0] = SOIL_load_OGL_texture("../textures/player/head.tga",SOIL_LOAD_AUTO,SOIL_CREATE_NEW_ID,SOIL_FLAG_INVERT_Y);
loaded=true;
}
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, playerid[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTranslatef(p_pos[0], p_pos[1], 0.0f);
glBegin(GL_TRIANGLES);
// Head
//Lower Triangle
glTexCoord2f(1.0f, 1.0f);
glVertex3f(0.5*p_head, 0.5*p_head, 0.0f);//top right
glTexCoord2f(1.0f, 0.0f);
glVertex3f(0.5*p_head, -0.5*p_head, 0.0f);//bottom right
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-0.5*p_head, -0.5*p_head, 0.0f);//bottom left
//Upper Triangle
glTexCoord2f(1.0f, 1.0f);
glVertex3f(0.5*p_head, 0.5*p_head, 0.0f);//top right
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-0.5*p_head, 0.5*p_head, 0.0f);//top left
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-0.5*p_head, -0.5*p_head, 0.0f);//bottom left
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
// Body
//Lower Triangle
glVertex3f(0.5*p_body, -0.5*p_head, 0.0f);//top right
glVertex3f(0.5*p_body, -1*p_body-0.5*p_head, 0.0f);//bottom right
glVertex3f(-0.5*p_body, -1*p_body-0.5*p_head, 0.0f);//bottom left
//Upper Triangle
glVertex3f(0.5*p_body, -0.5*p_head, 0.0f);//top right
glVertex3f(-0.5*p_body, -0.5*p_head, 0.0f);//top left
glVertex3f(-0.5*p_body, -1*p_body-0.5*p_head, 0.0f);//bottom left
glEnd();
}
void player::solid_collide(bool xcheck,bool ycheck,float dt){
bool col=false;
float inc = 0.125;
float bx,by;
for(int i=0;i<9;i++){
for(int j=0;j<16;j++){
if(troom.room_map[0][i][j] > 19){
bx = -1.0+j*inc+half*inc;
by = 0.5625-i*inc-half*inc;
//if(abs(p_pos[0]-bx)*2.0 <= (p_head + inc) && abs(p_pos[1]-by)*2.0 <= (p_head + inc)) col = true;
if(abs(p_pos[0]-bx)*2.0 <= (p_body + inc) && abs(p_pos[1]-half*p_head-half*p_body-by)*2.0 <= (p_body + inc)) col = true;
}
}
}
if(xcheck && col){
p_pos[0] = p_pos[0] - p_vel[0]*dt;
p_vel[0] = 0.0;
}
if(ycheck && col){
p_pos[1] = p_pos[1] - p_vel[1]*dt;
p_vel[1] = 0.0;
}
}