Skip to main content
gamedev.net
🔒 Locked

rotation algorithm

Started by gillan 4 replies 500+ views
Original Post
gillan
gillan
Bear with me here, as this is a bit of a long problem. I have a 2D engine in C++, which im gonna port over to iso (sounds like a daft way of doing it, but theres a reason). One of the operations my sprites perform is to formate, in columns, squares and boars snouts. These formations are stored in a class, which takes the leaders position, and stores the position of all other members as an off-set of the leaders coordinates. My problem arises from sprite rotation whilst in formation. I am currently using an equation as follows, which rotates the sprites through 90 degrees: tempX = (sprite_X) - (X_point_of_rotation); tempY = (sprite_Y) - (Y_point_of_rotation); newX = (X_point_of_rotation) - tempY; newY = (Y_point_of_rotation) - tempX; where point of rotation is the leaders position and newX/Y is the new position of the sprite in question. This algorithm is all well and good, but I need to be able to rotate through 45 degrees, thus allowing sprites to face 8 directions rather than 4. Frankly I haven''t a clue on how to do this, so any help would be grately appreciated. As a final note, I realise that I''m pushing my luck here, but please don''t tell me I need to do a FFT or dot product, as I''ll just stare at the screen and dribble! Code is good, maths is bad. My thanks in advance, Gillan
ShmeeBegek
ShmeeBegek

Excuse me, but what is a ''boars snout'' in 2d graphics??? and also some more info on the API you''re using would be helpfull aswell, in OpenGL one might simply use glRotatef with glTranslatef to create this effect, while in DirectDraw it is a bit more complex...

~SPH
AIM ME: aGaBoOgAmOnGeR
gillan
gillan
a boars snout is a wedge to all intents and purposes.
I''m using DirectX 7 purely for the graphics. I haven''t used any APIs for any number crunching, I rather hoped someone would understand what one of the APIs is actually doing for you. Hope this helps, thanks for the post.
ShadowChaser
ShadowChaser
You need to do a matrix transformation.

Try this code:
(I should point out that these coordinates have X, Y, and Z, but if you want it 100% 2D then just ignore the Z lines and it will still work)

// Rotates a point around the "origin" (0x0x0)
Point3D Transform3D::XYAxisRotateOrigin(double Angle, const Point3D & OriginalPoint)
{
// xNew = x·CosT - y·SinT
// yNew = y·CosT - x·SinT
double x = OriginalPoint.X();
double y = OriginalPoint.Y();
return Point3D(
(x * cos(Angle)) - (y * sin(Angle)),
(y * cos(Angle)) + (x * sin(Angle)),
OriginalPoint.Z()
);
}

// 2D Rotation about an arbitrary axis
Point3D Transform3D::XYAxisRotateArbitrary(double Angle, const Point3D & OriginalPoint, const Point3D & RotationPoint)
{
// Translate all the points through [rx,ry] so that OriginalPoint coincides with the origin
Point3D tmpPoint;
tmpPoint.X = OriginalPoint.X - RotationPoint.X;
tmpPoint.Y = OriginalPoint.Y - RotationPoint.Y;
tmpPoint.Z = OriginalPoint.Z;

// Rotate about the origin through the angle
tmpPoint = XYAxisRotateOrigin(Angle, tmpPoint);

// Translate all points back to [rx,ry]
tmpPoint.SetX(tmpPoint.X + RotationPoint.X;
tmpPoint.SetY(tmpPoint.Y + RotationPoint.Y;

// Return result
return tmpPoint;
}
ShadowChaser
ShadowChaser
Don''t forget that the "Angle" parameter in the code I posted earlier is in *radians*, NOT degrees!
gillan
gillan
cheers boss, your a star, I''ll give thata whirl, and see if I can''t get my sprites to behave themselves.

Topic Locked

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

Sign in to reply to this topic.