Rotation driven/commanded thanks the cursor's position.

:slight_smile: I don’t know I’m allowed to post two topic in the same time… if I’m not, I’m very sorry :s !

I am a new problem, lol!

I try to make a game with tanks. It’s a 2D game, and I use a upper view (I don’t know if I use the good words, I’m sorry) I have done a target so as to aim at ennemy but the tank’s cannon must be rotated toward the target.

I have used my Pythagore and “trigonometrie” knowledge in order to get the angle created by the horizontal axe and the axe formed by the position point of tank and position point of cursor, as is shown on the following page:

http://www.developpez.net/forums/showthread.php?t=95419

But, my rotated Objet disapears when the angle is close to 45° … I don’t understand why… ???

Here is the part of my code:

 int adj = cible_X-tank_X;
		int opp = cible_Y-tank_Y;
		double hyp = (adj^2+opp^2)^(1/2);
		double cosi_a = Math.abs(adj/hyp);
		double angl =  Math.acos(cosi_a);
		double pi = Math.PI;
		double angle = 0;
		 
		if (adj<0 && opp<0){angle= angl + pi;}
		if (adj>0 && opp<0){angle= -angl;}
		if (adj<0 && opp>0){angle= -(angl + pi);}
		if (adj>=0 && opp>=0){angle= angl;}
			c.setColor(new Color(100,60,80));
			c.rotate(angle,tank_X,tank_Y);
			c.drawRect(tank_X-40,tank_Y-30,80,60);

:’( please!! help me!

PS: oups, I would want to say “thanks to the cursor!”: “grâce à la souris” in french.

Math.atan2 is your friend. :wink:

Thanks :slight_smile: ! it’s a very good method which help me. But… the center of the Default Locate (“repère” in french, I’m not sure that it’s the good word, sorry) is in the upper-left corner of the screen. I will try to put my “adj” and “opp” in the “atan2” metod.

But is it possible to set the center of the Locate on my tank’s position?

I’m going to try the “atan2” method tomorow, it may be the solution that I search… :-\ I’m so tired, I don’t really suceed in thinking this evening, hihi !

re-Thanks and good night!

int a = 3;
int b = 4;
int c = (a^2+b^2)^(1/2);

c != 5…

the ^ operator is a bitwise XOR function.

try sqrt(aa+bb) or pow(pow(a,2.0)+pow(b,2.0), 0.5)

;D WAAAAAAAAAAAAWOUUUUUHHH!! Finally, I have tried the “atan2” method and it’s fantastic, you don’t know how I’m happy!!

Really, Thanks you oNyx! I don’t have enough vocabullary in english so as to show my gratitude but in short: “Youpiiiii!!!”.

As for the ^ operator… I’m sorry, I don’t understand " a bitwise XOR function" :s and I would like understand because I would like undestand why my code must not work.

In any case, Thank you for this precision: I will try if my code works with a “adjadj" and "oppopp” instead of “adj^2” and “opp^2” even if the method of “atan2” is better than mine. I just want to see if my method could have functioned…

Re-good night! Thank you Riven and oNyx.

XOR:

0 ^ 0 = 0
1 ^ 0 = 1
0 ^ 1 = 1
1 ^ 1 = 0

ints are 32 bits.

a = 010101000010111101010
b = 001011011100010010011
XOR
c = 011110011110101111001

Further, the last part: …^(1/2) is really flawed. integer-division doesn’t have decimals, so 1/2=0, you’re doing ‘value’ XOR 0, which is ‘value’ again.

??? hu!.. oups, I didn’t know that.

To end, I have try my technique with yours correction and it works!! It’s very cool, because I thought I have made a mistake in my calculations! Now, I know I don’t have made mistake but I will use the “atan2” method.

So, I can close this topic !!

Merci! :wink: