#### Archived

This topic is now archived and is closed to further replies.

# Feeling good! Have 2 questions though...

This topic is 6588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

thank sfor your guys help on my alst question about textures... As always, ive found 2 more things to learn about: First off: I can draw spheres, but how do i erase them? Well, let me explain: I have my little character (a pyramid) and i want him to shoot a little ball so far in fron of him. I have figured out how to get it to draw spheres going out in fornt of the guy when you press psace bar, but it draws all of them and it ends up looking like a long green row of balls then one single bal moving forward? So how do i get my ball to move forward? And my second, harder (math) question: How do i get the angle that two vectors make? (I forgot how from trig) I have two vectors (or lines, if necessary) stemming from a single point. One vector is fixed, and the other moves. I have coords and lengths etc. for each vector/line. Thanks for being so cool! Jjesterr Stumped like a pirate slower than a cannonball...

##### Share on other sites
I''m a little confused by the first part of your question! If your drawing a sphere then moving it and drawing it again yes it''ll look like there forming a line of spheres. If your drawing the scene then moving the sphere then clearing the scene and drawing again then it''ll look like the sphere is moveing because the old one is erased with the rest of the scene.
If your trying to do it more like a sprite then you have to find the area of the screen the sphere ocupies then store the color and depth buffer info for that area draw the sphere then you can move the sphere restore the color and depth info and repeat.

for the second question...
The cosine of the angle between two vectors is equal to the dot product of the normalized vectors.
i.e.
1) normalize the vectors (make them unit vectors)
2) calculate the dot product of the vectors.
3) calculate the arccosine of the result to get the actual angle.

This will not tell you the direction the vectors are form each other but it will tell you the difference between them 0 to 180 degrees.

##### Share on other sites
As to the first part of your question, couldn''t you just translate the ball along its intended path? That''s the easiest way to solve the problem.... Assuming I''m right, you would want to use glTranslate() like:

//***Somewhere in your declarations, most likely global***
GLfloat ballZ = -4.0; //arbitrary starting pos for your ball
//this will get decremented each pass through the render code
//so that your ball will move further into the distance.

//***during screen refresh/draw code***

glTranslatef(0.0f, 0.0f, ballZ); //translation along z-axis

....

//end of rendering code, where everything else gets incremented
//or changed, decrement your translation counter.
ballZ -= -0.1f; //larger number = faster movement; smaller =
//slower movement.

I hope this helps....

--David

##### Share on other sites
OK, leaving the first question alone (i can get that later) its the Second one thats been bugging me for days...

I tried doing that, and it didnt work right (i chechedk my matha dozen times over). It only moves like 1 degree in either direction??!?!?

Oh well, let me describe what im doing in fuller detail: I have this cylinder (column) in the middle of a square playing field. I want to be able to rotate the cylinder so that it is always facing the player. How do i do this?

Jjesterr
Perplexed...

##### Share on other sites
OK, leaving the first question alone (i can get that later) its the Second one thats been bugging me for days...

I tried doing that, and it didnt work right (i chechedk my matha dozen times over). It only moves like 1 degree in either direction??!?!?

Oh well, let me describe what im doing in fuller detail: I have this cylinder (column) in the middle of a square playing field. I want to be able to rotate the cylinder so that it is always facing the player. How do i do this?

Jjesterr
Perplexed...

##### Share on other sites
OK, leaving the first question alone (i can get that later) its the Second one thats been bugging me for days...

I tried doing that, and it didnt work right (i chechedk my matha dozen times over). It only moves like 1 degree in either direction??!?!?

Oh well, let me describe what im doing in fuller detail: I have this cylinder (column) in the middle of a square playing field. I want to be able to rotate the cylinder so that it is always facing the player. How do i do this?

Jjesterr
Perplexed...

##### Share on other sites
quote:
Original post by Jjesterr

OK, leaving the first question alone (i can get that later) its the Second one thats been bugging me for days...

I tried doing that, and it didnt work right (i chechedk my matha dozen times over). It only moves like 1 degree in either direction??!?!?

Sounds like you may be mixing up your radians and degrees. Are you using C library function acos() to do your arccosine? If so, that returns a result in radians. To convert it to degrees you need to multiply by 180/pi (that''s about 57.29578). Give that a try?

##### Share on other sites
quote:
Original post by Anonymous Poster

Oops forgot my name & password. ;-)

Anyway, yeah, no offence intended by suggesting that you don''t know your radians & degrees, but if you''re getting results in the vicinity of 1 that could be it..

Ah, I remember when every C program I wrote started with "const double RAD2DEG=57.2957795132609" ;-)

##### Share on other sites
dammit, well, that almost worked....

I have this going on:

the coords are: xpos,z
and 0,-3.6
so my formula is
keep in mind i simplified xpos*0 and simplified
sqrt(0*0 + (-3.6*-3.6)

(acos( (z*-3.6) / (sqrt(xpos*xpos + z*z) *3.6) * (180/3.1459)

Is this right?

Grrr , this is really pissing me off.

While i was just screwing around with numbers and stuff, before i attempted this mathmatically, i found that

(xpos*z)*18 worked while you were in front of teh cylinder and
((xpos*z)*-18)+180 worked somewhat while you were behind it...

(well, they work as long as you dont get too close the cyldinder and go form in fornt to behind, it jumps...

Oh well, if anyone can hlep me out with how to make my guy shoot a ball (the first question) id like that too.. (i dotn know how to clear a certain depth buffer bit, or whatever...)

Jjesterr
Napping in the cave of contemplation

##### Share on other sites
I had typed up a wonderfull (and long) explanation of how to fix the problem but due to a server error it got whiped out! I don''t have another hour to type it again so here''s the short version.

the values.

c = [cx,cy,cz] = colum position
p = [px,py,pz] = player postition
d = [dx,dy,dz] = direction from colum to player
a = angle from player to colum

first get the direction from the colum to the player

d = p - c;

We want to ignor the y axis as thats not part of the rotation were looking for.
d.y = 0;

then normalize the direction (make it a unit vector)

d = Normalize(d);

now you can find the cosine of the angle between the player and a reference vector. I''ll assume that the zero rotation will be on the positive z axis.

a = Dot(d,[0,0,1]);

in this case knowing that the dot product is

dx * 0 + dy * 0 + dz * 1

we can see that the x and y values are droped and only the z value remains so we end up with.

d = p - c;
d.y = 0;
d = Normalize(d);
a = arccos(d.z);

The only problem is that the angle tells us how far around the circle but not wich way! Since we want a rotation around the z axis we have to look at the x axis. In opengl from the positive z axis a positive y rotation would put the point in the positive side of the x axis. Therefor we need to make the roataion postive if d.x is postive.

d = p - c;
d.y = 0;
d = Normalize(d);
if(d.x >= 0)
a = arccos(d.z);
else
a = -arccos(d.z);

Now you should have the angle as a value between -3.1415 and 3.1415 if you need the value to be positive you could do the following

if(d.x > 0)
a = arccos(d.z);
else
a = (2 * pi) - arccos(d.z);

I haven''t tryed this yet but it should work!?!?!?

--------------------------------------------------------
"Math, is that that number thing?" - Me

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633735
• Total Posts
3013596
×