Hi Guys,
I am having some problems with my enemy class. The world is 2d, with enemy agents running around. I need every update step for the enemies to orient themselves to their normalized velocity vector called "heading".
here is the rotation algorithm
void rot(float angle){
for(int i=0;i<points.size();i++){
float x=((points[i].x-center.x)*cos(PI/180.0f*angle))-((center.y-points[i].y)*sin(PI/180.0f*angle))+center.x;
float y=((points[i].y-center.y)*cos(PI/180.0f*angle))-((points[i].x-center.x)*sin(PI/180.0f*angle))+center.y;
points[i].x=x;
points[i].y=y;
}
}
Here is part of the enemy constructor
velocity.x*=speed;
velocity.y*=speed;
setHeading();
rotation=(float)atan2(heading.x,heading.y)*180.0f/PI+180;
rot(rotation);
the setHeading func
void setHeading(){
heading=velocity;//heading and velocity are vectors with x,y components
heading.normalize();
}
and lastly, the enemy update
void update(double dt){
for(int i=0;i<3;i++)//each enemy is a triangle
points[i]+=velocity*dt/1000.0f;
center+=velocity*dt/1000.0f;//center of the enemy
setHeading();
//NEED TO HANDLE AUTONOMOUS ROTATION HERE BASED ON NEW VELOCITY
//currently the velocity is constant to keep the func simple to read
}
Everything is set correctly in the constructor, with respect to the initial rotation, however, the issue is in the update method. Since the roation algo is a function of center, all vertecis, and angle, if it's constantly updated the object keeps spinning except for the case of 0 angle.
What i need is for the enemy update method to as smoothly as possible rotate the enemy if I apply impulses, forces etc to the object in the direction of the new normalized velocity, preferably polling each update step for the most accurate result.
Could someone please describe, and or show how I can implement a rotation algo which will in real time align the orientation of the enemy object with velocity?
Thanks,
Mike