Solved

Click and rotate?

Posted on 2000-04-05
9
165 Views
Last Modified: 2010-04-15
Ok, here is what I am trying to do. I am sure you have all played rts(real
time strategy) games, where you click on one unit, and then click on another
unit, and the first one turns toward the second, and starts walking. I am
trying to do a similar thing. What I really need help on is this. I know the
x,y location of the 2 objects, but how do I get the angle between the two? I
have tried using some stuff, but It doesn't work :-(. Could anyone put me on
the right track? I understand trig, so if you use those functions I can
follow you.
Jesse
0
Comment
Question by:graphix_man
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 1

Expert Comment

by:guyss
Comment Utility
assuming (x1,y1) is bigger than x,y
(otherwise you must use absolute values)

what I propose is the following method:
x1-x will give you the X distance between the points,
y1-y will give you the Y between the points.

now i build a right angle triangle
where one angle is 90 degrees.
and two lengths are known
to get the angles:

(x1-x)/(y1-y)=(a)/(90-a)

so just compute (x1-x)*(y1-y) into z
and you angle is either 90*z/z+1
or 90 minus that.

try to draw it out, and it'll be clearer.
0
 

Author Comment

by:graphix_man
Comment Utility
I follow you up
=(a)/(90-a), cause I did the same thing. What does a/90-a mean? also what is a defined as?


0
 
LVL 1

Expert Comment

by:guyss
Comment Utility
well, when you have a right angle triangle the ratio of the two sidies of the triangle is the SAME as the ratio
for the two angles, and the two angles
are x and 180-90-x which equals 90-x.
0
 
LVL 3

Expert Comment

by:3rsrichard
Comment Utility
If one is at X1,Y1  and the other is at X2,Y2 then you could use;

float line1, line2;
double angle;

line1 = (float)X2 - (float)X1;
line2 = (float)Y2 - (float)Y1;

angle = atan(line1/line2);

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

by:3rsrichard
Comment Utility
Heres a console example, and remember that the angle will be in radians (not degrees).


#include <math.h>
#include <stdio.h>
#include <errno.h>

void main( void )
{
   double x1, x2, y;

   printf( "Enter a real number: " );
   scanf( "%lf", &x1 );
   y = atan( x1 );
   printf( "Arctangent of %f: %f\n", x1, y );
   printf( "Enter a second real number: " );
   scanf( "%lf", &x2 );
   y = atan2( x1, x2 );
   printf( "Arctangent of %f / %f: %f\n", x1, x2, y );
}
0
 
LVL 1

Expert Comment

by:ntdragon
Comment Utility
sorry if i'm trying to say something that already have been said

object 1:(x1,y1)
object 2:(x2,y2)

i"ll give a math formula
x=|x1-x2|//modul i mean it have to be >0
y=|y1-y2|//modul i mean it have to be >0
tan(alpha)=y/x
alpha=arctan(y/x)

don't forget to use float's or casting to floats that you won't get your numbers rounded
0
 

Author Comment

by:graphix_man
Comment Utility
I have this code:
            if(xmatrix<0)
            {xmatrix=(-xmatrix);}
            if(ymatrix<0)
            {ymatrix=(-ymatrix);}
            float tantest;
            tantest=ymatrix/xmatrix;
      

            double test=atan(tantest);
            test=test*(180/PI);
That I got from looking at the posts. Is this correct, in that is spits out the angle between the 2 points? If so I think 3drich gets the points. If not whoever can fix this up wins.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Why are you taking the absolute value of xmatrix and ymatrix?  What do you mean by "angle between the 2 points"?
0
 
LVL 3

Accepted Solution

by:
3rsrichard earned 50 total points
Comment Utility
What you have is OK.
You don't need to use only positive values, atan works for negative as well.

You need to consider that point 2 could be right/left or up/down from point 1.

#include <math.h>
#include <stdio.h>
#include <errno.h>

void main( void )
{
   double x, y, angle;

   printf( "Enter a real number x: " );
   scanf( "%lf", &x );
 
   printf( "Enter a second real number y: " );
   scanf( "%lf", &y );
   angle = atan2( x, y );
   printf( "Arctangent of %f / %f: %f\n", x, y, angle );

   printf( "Arctangent of %f / %f: %f\n", x, y, (angle*180/3.14159) );

}


0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now