Welcome, Guest. Please login or register.

Author Topic: C++ Trigonometry  (Read 5597 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show all replies
C++ Trigonometry
« on: September 11, 2007, 08:25:39 PM »
I'm trying to calculate an angle given the distances between two objects:

int distx = playerx-x;
int disty = playery-y;
int angle = atan( disty/distx) * (180/3.14159265);
fprintf( stdout, "%d \n",angle );

Where x=100 y=100, playerx=0 and playery=0, the angle returned is 45 which sounds correct. But if the object is put on the opposite side (x=-100, y=-100) then the angle is still 45. What an I doing wrong?

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show all replies
Re: C++ Trigonometry
« Reply #1 on: September 11, 2007, 08:29:19 PM »
Also, moving the object to x=100,y=-100 makes the angle -45 degrees.

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show all replies
Re: C++ Trigonometry
« Reply #2 on: September 11, 2007, 08:42:58 PM »
Thanks. In atan2(), are y and x the distances between the two points?

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show all replies
Re: C++ Trigonometry
« Reply #3 on: September 11, 2007, 08:45:37 PM »
Yeah! atan2( disty, distx ) works :-D

Cheers Karlos

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show all replies
Re: C++ Trigonometry
« Reply #4 on: September 12, 2007, 08:00:47 AM »
Thanks Boot_WB, very interesting. I did consider calculating which quadrant the object is in and adding a multiple of 90 degrees to compensate, but fortunately atan2(x,y) does the hard work for me.

Now, here's another problem. The following code is supposed to move an object towards the player:

    //Find the angle required to move towards the player
    int distx = playerx-x;
    int disty = playery-y;
    int angle = atan2( disty, distx) * 57.29578;

    //Set the velocities
    dx = seekerspeed * cos(angle);
    dy = seekerspeed * sin(angle);

    //Move
    x+=dx;
    y+=dy;


As you can see in this Quicktime video, the effects aren't quite as desired. The enemy stops when it gets close to the player instead of attempting to collide with it, and if the player moves the enemy actually seems to move away from it!

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show all replies
Re: C++ Trigonometry
« Reply #5 on: September 12, 2007, 08:20:31 AM »
Someone on another forum has solved this for me. The  "* 57.29578" shouldn't be there because cos() and sin() take radians, not degrees.

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10
 

Offline motorollinTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 8669
    • Show all replies
Re: C++ Trigonometry
« Reply #6 on: September 12, 2007, 12:09:24 PM »
Actually I am now using this:

int distx = playerx-x;
int disty = playery-y;
int dist=sqrt(distx*distx+disty*disty);

dx=speed*distx/dist;
dy=speed*disty/dist;



This is much faster, since floating point maths and trig is very slow on the GP2X (no FPU so all float operations are done in software). Also this code seems to result in smoother and more accurate motion.

--
moto
Code: [Select]
10  IT\'S THE FINAL COUNTDOWN
20  FOR C = 1 TO 2
30     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NAAAA
40     DA-NA-NAAAA-NAAAA DA-NA-NA-NA-NA-NA-NAAAAA
50  NEXT C
60  NA-NA-NAAAA
70  NA-NA NA-NA-NA-NA-NAAAA NAAA-NAAAAAAAAAAA
80  GOTO 10