Math problem?

Discussion about everything. New games, 3d math, development tips...
Post Reply
Klasker
Posts: 230
Joined: Thu May 20, 2004 8:53 am
Contact:

Math problem?

Post by Klasker »

I have this code which is supposed to create a rectangle. Don't worry too much about IPlaneSceneNode, it simply draws two triangles using the coordinates given.
The problem seems to be corners[] being set wrong. It is supposed to be the corners of a rectangle, but for some akward reason the X coordinates almost won't change (when adding ca).

Target1 and Target2 are the middle points on two opposite edges of the rectangle. Width is the length of those two edges.
The length of the other two edges equals the distance between Target1 and Target2 (obviously, I hope).

Can someone point out what I'm wrong here?

Code: Select all

IPlaneSceneNode* createRectSceneNode(vector3df Target1, vector3df Target2, float width, SColor Colors[4])
{
    // Deltas
    float dx = Target1.X - Target2.X;
    float dy = Target1.Y - Target2.Y;
    double angle = atan2(dy,dx);
    angle += M_PI/2;

    cout << "Angle is: " << (angle * RAD_TO_DEG) << endl;

    double ca = cos(angle) * width;
    double sa = sin(angle) * width;

    cout << "ca = " << ca << "\nsa = " << sa << "\n";

    vector3df corners [4];
    corners[0] = Target1;
    corners[0].X += ca;
    corners[0].Z += sa;
    corners[1] = Target2;
    corners[1].X += ca;
    corners[1].Z += sa;
    corners[2] = Target2;
    corners[2].X -= ca;
    corners[2].Z -= sa;
    corners[3] = Target1;
    corners[3].X -= ca;
    corners[3].Z -= sa;
    
    IPlaneSceneNode* node = new IPlaneSceneNode(corners, Colors);
    node->drop();
    return node;
}
To better describe what goes wrong, I'll use a pesky text drawing to demonstrate:

Code: Select all

Correct rect: (It only makes this in some few situations)
 ------
|      |
|      |
|      |
|      |
|      |
 ------

Bad rect: (It keeps making this...)

|\
| \
|  \
|   |
|   |
|   |
|   |
 \  |
  \ |
   \|
Help!?!?
Post Reply