Advertisement

A few OpenGL maths questions for my Speedball game :)

Started by July 29, 2003 07:53 AM
14 comments, last by WoolyUK 21 years, 7 months ago
Hi guys First off... tried searching the forums all morning for answers cus thats how I usually find all the info I ever need, but search not working for me today So I thought I would ask I''m making a speedball type game, and have came up to a few maths problems I need help with, cus I suck at maths The following screenshot is from my game so far... the boxes are the players and will be replaced with 3d models soon http://www.woolyweb.pwp.blueyonder.co.uk/8.jpg My 2 problems are: 1) The blue box with the yellow circle around it is the currenctly active player, and the red boxes are the computer players. How do I work out the ANGLE between 2 points? I,e so I can work out the angle all the computer players should turn so they are facing the blue (human) player. Obviously the players are always moving and can be in different positions, so im looking for some kind of formula I guess? 2) How do I find the DISTANCE between 2 points? I.e so I can figure out the closest player to the ball (current player). Again im asuming I need a formula Thanks for any help at all you guys can give me, and I''ll continue to dig around and keep trying the search until it starts working again I''ll let u know if I solve them first Thanks again, Adam
Sounds like you''re missing some high school maths. Try a google search for "angle between vectors" and "distance between points".


Why you shouldn''t use iostream.h - ever! | A Good free online C++ book
Advertisement
Basically here is what you have.

1.) you cannot find an angle between 2 points, such a
measure does not exist. basically what you need to do is
define the front of each player with a vector, it is then possible to find the angle between 2 vectors, it is
called the dot product. basically i believe the formula
for the angle between 2 vectors is theta=inverse cos((x dot y)/(|x||y|)). where x and y are the 2 vectors.

2.)This is much more basic, and you should really know this one
if you are making a game. its the distance formula.
the distance between 2 pts (x1,y1),(x2,y2) is defined
by sqrt((x1-x2)^2+(y1-y2)^2). this extends into n-space
with the same basic form.

anyway hope this helped.
Rob
Thanks for the suggestions

When trying:
return sqrt((x-team1[currentplayer2].x)^2+(y-team1[currentplayer2].y)^2)

I got an error saying I couldnt use ^ on floats for some reason?

I replaced with:

return sqrt(((x-team1[currentplayer2].x)*(x-team1[currentplayer2].x)) + ((y-team1[currentplayer2].y) * (y-team1[currentplayer2].y)));

and it compiles... Am i right in thinking that performs the same thing or have I messed up? if it is the same thing, why will it compile that way?

Sorry but im really not good at maths

Thanks again
Adam

[edited by - WoolyUK on July 29, 2003 10:13:27 AM]
Naa you got it right. Just remember that if you only want to compare distances, you don''t need to take the square root at all.
WoolyUK:
Assuming you''re using C or C++, you''re getting the error about the ^2 because C/C++/some other languages use the carat (^) as the XOR operator, not as the power operator. The simplest way is just to manually write it out, as you have. Or you can define a macro/inline function to square a number, or you can use the pow() function from the math library. But, since the XOR operator isn''t defined for floats as it would produce some funky answers, it will give you a compiler error instead of just funky runtime stuff.

Hope that helps.



"Back to the code mines... ka-chink... ka-chink..."
Tachyon Digital - Down for the summer, be back in the fall.
"Back to the code mines... ka-chink... ka-chink..."Tachyon Digital - Down for the summer, be back in the fall.
Advertisement
It does help, thanks!
And ^ operator is XOR in c/c++.
just square the values by multiplying them with their own value.
(x*x) for example is the x squared

EDIT: just missed, see Hyren's answer

[edited by - Nik02 on July 29, 2003 10:29:27 AM]

Niko Suni

ya, your code does the exact same thing. I was writing out
the FORMULA, not the CODE. As was previously stated
in C/C++ ^ is the XOR operator which, being undfined for
any floating point data type will produce some funky
lowlevel bit flipping results. Also note, my formula
to get the angle from the dot product was also a formula
and not code, the |x| dictates the abs value of x, in c/c++
| is the bitwise or operator, make sure you convert
these formulas to code before using them.

Hope this helped,
Rob
Not having luck getting it working

Im trying to get it so when I press a key, it will make the current player on the team the player that is nearest to the current player.

Im using the following functions:

float CPlayer::DistanceToPlayer(){		if (team == 1) {		return sqrt(((x-team1[currentplayer1].x)*(x-team1[currentplayer1].x)) + ((y-team1[currentplayer1].y) * (y-team1[currentplayer1].y)));	}	if (team == 2) 	{		return sqrt(((x-team2[currentplayer2].x)*(x-team2[currentplayer2].x)) + ((y-team2[currentplayer2].y) * (y-team2[currentplayer2].y)));	}}void SelectClosePlayer()					{						for (int t=0; t<5; t++) 						{if ((team1[t].DistanceToPlayer() < 5.0) && (t != currentplayer1)) currentplayer1 = t;						}					}


For now im just checking if each player is withing 5.0 units and if so making it the current player.

However it doesnt work

Is it the maths thats wrong or something else?

Cheers, Ad

[edited by - WoolyUK on July 29, 2003 10:53:17 AM]

[edited by - WoolyUK on July 29, 2003 10:53:48 AM]

This topic is closed to new replies.

Advertisement