PrisonLove
Hard Realist
- Reaction score
- 78
Okay so I have a couple of questions. First of all I'm working on a little animation. In my animation there is a chubby person and he is constantly moving toward an ice cream cone. There is a monster chasing him. That is the scenario. I am working with 3D points.
I'm using a rotation matrix before drawing the characters to make certain that they always face the object that they're going for (chubby faces ice cream and monster faces chubby). I don't know how to calculate the angles that they should face.
If chubby is at point p1 and ice cream is at point p2, I need chubby to face the angle from p1 to p2. How do I calculate this? The same thing would apply to the monster.
Next is movement. I keep track of the coordinates of every character (including the ice cream) so I figure I would also use a translation matrix to make them move. That makes sense, but I need to know how to calculate the values to translate.
Here is what updating a character would look like in my code so far:
draw( gl ) is how I draw the characters, all characters have working draw methods, and animate properly. They're rotation and position is the focus of this thread.
I have a method to get the angle, but there is an issue. I'll illustrate with chubby and ice cream. When ice cream is in quadrants II or IV, chubby faces the correct location. However, when ice cream is in quadrants I or III, chubby faces the opposite direction. Why is this? This is my code for that function:
As for updating their locations, I'm fairly stumped. I figured I would need some sort of polar projection. How do I polar project points in Java?
I'm using a rotation matrix before drawing the characters to make certain that they always face the object that they're going for (chubby faces ice cream and monster faces chubby). I don't know how to calculate the angles that they should face.
If chubby is at point p1 and ice cream is at point p2, I need chubby to face the angle from p1 to p2. How do I calculate this? The same thing would apply to the monster.
Next is movement. I keep track of the coordinates of every character (including the ice cream) so I figure I would also use a translation matrix to make them move. That makes sense, but I need to know how to calculate the values to translate.
Here is what updating a character would look like in my code so far:
Code:
gl.glPushMatrix();
gl.glTranslated(chubbyLoc.x, chubbyLoc.y, chubbyLoc.z);
gl.glRotated(chubbyFacing, 0, 1, 0);
chubby.draw( gl );
gl.glPopMatrix();
draw( gl ) is how I draw the characters, all characters have working draw methods, and animate properly. They're rotation and position is the focus of this thread.
I have a method to get the angle, but there is an issue. I'll illustrate with chubby and ice cream. When ice cream is in quadrants II or IV, chubby faces the correct location. However, when ice cream is in quadrants I or III, chubby faces the opposite direction. Why is this? This is my code for that function:
Code:
public double angleBetweenPoints(Coord p1, Coord p2) {
double p1x = p1.x; double p2x = p2.x;
double p1y = p1.y; double p2y = p2.y; //I am aware these values are unused
double p1z = p1.z; double p2z = p2.z;
double radians = Math.atan2(p2z-p1z, p2x-p1x);
double degrees = Math.toDegrees(radians);
return degrees;
}
As for updating their locations, I'm fairly stumped. I figured I would need some sort of polar projection. How do I polar project points in Java?