# Help Me out with Math for OpenGL - I'm 14!

Being the 14 year old that I am, I was wondering if anybody could hint to me any opengl formulas or math that would aid me in my sail boat project in OpenGL. My problem is, I have a wind angle, and I have a sail angle (from 0 to 180 I believe) and I have to determine how to translate the ship based upon the wind angle and the sail angle and the wind speed (I want to be able to increase/decrease wind speed).

Thanks a bunch folks, and if you need to see my source code, just check out my post about texture rendering not working (I know I know, its not texture RENDERING)

I think you’ll want to look into vectors and vector reflections. Heres the formula:

R = I - 2*(N·I)*N, where R is the direction vector of the reflection, I is the incident (direction of the incoming ray,) and N is the normal to the surface.

Then translate by the negitive of that vector.

Remember to normalize your vectors too.

Ok, you should represent your wind as a vector and velocity. This will be your wind’s diretion and speed.
Since the wind will be universal the Y component can be “ignored” and will always be 0, which will simplify things! Therefore you have a vector pointing in some direction on the XZ plane.

ok, so let’s assume your sail is just a flat plane, or a board if that is easier to visualize.
You will need to figure out the normal of your sail, that is, the direction that is perpendicular to the surface of the sail.
. o
. |
. <-- |
. |
. -------
. _____/
the arrow represents the normal of your sail in the crude picture above. (which i see is messed up, hope you get the idea)

Now you know which way your sail is pointing and which way the wind is blowing.

Now you need to multiply these 2 vectors together, that is, take their cross product (there are many sites online which will tell you how to do this). This will give you a new vector!
Based on the velocity of your wind, you should then translate your boat in the direction this new vector points by a certain amount depending on how strong the wind is (refered to as it’s magnitude).
Then rotate your ship so that it points in the direction of the new vector as well.

Hope this helps! And don’t be afraid to ask either your teacher or a physichs teacher from your school with help on this. That’s what they’re there for

[This message has been edited by Penance (edited 05-17-2002).]

[This message has been edited by Penance (edited 05-17-2002).]

[This message has been edited by Penance (edited 05-17-2002).]

Hey thanks a bunch both of you but here’s my idea.

So what I’ve gathered is that I add the wind angle and the sail angle together and then I divide that by 2, therefore finding what’s inbetween the two. Is this what you were getting at?

My second question is, now that I know the angle at which the sailboat must travel, how do I translate that? I always have problems with that in OpenGL, I usually end up just using doom style movement heh.

By the way, I have went to both of my physics teachers for help (1 isn’t my official teacher) and they have both been of tremendous help so far, however when trying to implement the formulas and ideas with sine, cosine, and tangent it always seems to act strangely. Remember if I’m wrong, I was only introduced to sine, cosine, and tangent about a week ago so I don’t know all of it

Thanks again guys n’ gals

A question for iNsaNEiVaN, what do those variables mean? Could you explain it a little more? I like the idea and understand the concept but not how to implement it into my program.

Wait a second, after reading the second reply over again, you said MULTIPLY the vectors (the angles??) and then do something with the cross product? I’m confused I guess

I am 13 programming in OpenGL, and don’t know all the math necessary either… someone recommended to me: www.mathworld.com but i haven’t found the time to visit yet. I would like it if you told me if it was good or bad. During the summer vacation (starting thursday! 4 days of school and programma-time!) thanx.

You can think of a vector as an arrow defined by x,y,z coordinates. The starting point is the origin. If i have a vector to the point 1,0,1 then i have an “arrow” pointing 45 degrees on the XZ plane (which in your program would be the ocean). The magnitude of the vector can be thought of as the length, or strength, of the arrow.

It sucks you have to do all this after only recently learning trig functions since alot of this is basic Physics, which you probably won’t get to until junior or senior year! However, knowing what a vector is now, you also should know that it is possible to multiply 2 vectors together to get a vector as a result.

For instance if i take the vector 0,0,1 (which would be pointing straight up the z axis starting at the origin) and multiply it by 1,0,0 (pointing along the x axis starting at the origin) and their magnitudes are the same, then I will end up with 1,0,1 the same 45 degree angle vector i mentioned before!

Again, do a search online for “cross product”, “basic physics”, “vector arithmetic”, etc.

glPushMatrix();
glTranslatef( x, y, z );
glRotatef( angle, 1.0f, 0.0f, 0.0f );
glRotatef( angle, 0.0f, 0.0f, 1.0f );

//code to build ship

glPopMatrix();

this will move whatever distance you specify with glTranslatef along x, y and z. Then it will rotate everything on the x axis by the angle you give. Then you will rotate everything on the z axis by the angle you give. now your ship will be drawn in the correct place and at the correct angle!

The Crossproduct of (1,0,0) and (0,0,1) is not (1,0,1) but (0,1,0)!

Imagine the two cross-multiplied Vectors as two edges of a parallelogram. The crossproduct results in a vector that is orthogonal to the plane of the vectors and the length of this vector is equal to the area size of the parallelogram.

A crossproduct is not as simple as it was explained here. You get different results, when you exchange the first and the second vector (exactly the opposite vector).

I also recommend reading www.mathworld.com, but vector geometry is not the easyest to understand, when you never had vectors in school, yet.

Good, luck

wanders off to the corner to lick his wounds

Well, i DID tell him to look it up himself But hopefully i at least gave him enough info to set him on the right track.

sets off to beat his Finals over the head with a rubber mallet

I recently wrote down the whole way that leads to the crossproduct in this forum. Go check for postings of me in the last two months. Shouldnt be too many.
If I get the time I’ll write down a little tutorial, but I’m currently really busy with other stuff.

Wow I find this all to be very interesting. However I’m not sure how to actually implement these things into my saiboat simulation. My cousin recently gave me some feed back. He said that I could use the magnitude to be the wind speed (although he did say it would be inaccurate) and then to translate x as the sine of the angle multiplied by the angle and to do the same for the z. What angle is he talking about? Does anybody see what he’s getting at? How do I find that angle? Thanks folks

Ack does anyone know anything about sailing here?
I sure don’t, but it seems to me the boat has to go the way its facing, not the way the wind is blowing.
Isn’t what that big fin on the bottom is for? Mwahaha.

And you shouldn’t need anything above 2d vectors (no cross products) because the boat surely won’t be moving up or down.

Maybe I’m crazy