continous velocity aligned rotation problem

Started by
2 comments, last by too_many_stars 9 years, 6 months ago

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

Advertisement

given:

pos x = right (east).

pos z = up (north).

a unit direction vector.

compass heading = atan(x/z)

where:

x = x magnitude of the direction vector

z = z magnitude of the direction vector

its usually easier to store headings as angles than as direction vectors. either will work, and you can convert between the two:

heading=atan(x/z)

tan(heading)=x/z

but it usually turns out that the math is easier with angles than vectors, and you seem to more often require headings in an angle format rater than a vector format, especially for drawing and AI. as with everything it depends on the title in question as to which is less work. from a speed standpoint, obviously, if you for example need to do calculations using angles, its wasteful to constantly convert between vectors and angles. in all my titles, both 2D and 3D, i store orientation as angles, not direction vectors. Only SIMSpace 3.0 used up, right, and forward vectors, with rotation about an arbitrary axis and Gaussian re-ortho-normalization. It had to do a fair amount of conversion between vectors and angles. heavy use of 3d unit direction vector to xr and yr Euler angles. just using Euler angles would probably have been easier.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

I have the exact opposite advice to Norman Barrow's: The math is much easier if you store headings as unit vectors. It is true that switching back and forth between angle and vector representations is a bad idea, but you basically never need to use angles.

In your code, you use atan2 to convert a vector to an angle, and then you call `rot'. The only thing `rot' is going to do with the angle is compute its sine and cosine... which are the components of the vector you started with!!

Now, to your question: I am not sure what all those "points" represent, but I bet life will be easier if you keep an unrotated version of the points and then apply the absolute rotation each frame, instead of trying to apply incremental rotations.

Of course Alvaro, that makes perfect sense. Keep the unrotated version stored, and apply an absolute rotation as required.

Here I am trying to turn off the rot function with booleans, augmenting things and all the while simplicity is the key. Although simplicity that admiteddlly did not occur to me.

Thanks for the help guys! I think problem is solved.

This topic is closed to new replies.

Advertisement