6
\$\begingroup\$

Here's the problem: A player starts the game in the 0 coordinate (x=0,y=0). When the user clicks on the screen, it returns the coordinates of the destination. Now the player has to move from its current position A(0.0) to B(x,y). Take into account that B is in the first Quadrant so x>0 and y>0.

How will the player move? I know that I will have to use this: atan2(positionY-destinationY, positionX-destinationX) to get the angle.

Now what? How will the player move with a fixed velocity (which by the way will be a variable, so a player may move faster while using some "items")?

Whose answer is better? What is more accurate? Trigonometry or Linear Algebra?

\$\endgroup\$
2
  • \$\begingroup\$ en.wikipedia.org/wiki/Euclidean_vector \$\endgroup\$
    – Casey
    Jan 26, 2013 at 16:15
  • \$\begingroup\$ what @Sidar said and also look into steering behaviors particularly seek and flee \$\endgroup\$
    – edin-m
    Jan 29, 2013 at 10:04

3 Answers 3

6
\$\begingroup\$

Basic trigonometry example

cosine for x axis

sine for y axis

speed is whatever you would like the movement each time to increment by... Obviously the higher the number the more pixels it will move at any given time so the quicker it will be.

dx = (double) (Math.cos(angle) * speed);
dy = (double) (Math.sin(angle) * speed);            

A.x += dx;
A.y += dy;

Linear algebra example

Check Sidar's answer :).. Explains it way better than I could.

This is in java... You can easily convert it to C++ though.

\$\endgroup\$
2
  • \$\begingroup\$ where speed is defined by... \$\endgroup\$
    – user25581
    Jan 25, 2013 at 22:28
  • 1
    \$\begingroup\$ whatever you would like the movement each time to increment by... Obviously the higher the number the more pixels it will move at any given time so the quicker it will be. \$\endgroup\$
    – Savlon
    Jan 25, 2013 at 22:30
11
\$\begingroup\$

To add to Savlons answer:

There are two ways of doing this

Vector2D objects(Assuming your positions are vectors):

Vector2D diffVec(B.posVec-A.posVec);
diffVec.normlize(); //Assuming it's applied on the vector itself
// and does not return a new one.   
//This vector is now a unit vector which represents the heading/Direction   
//without the "speed" (Well its speed is 1, more precisely its MAGNITUDE is 1).

diffVec.multiply(speed);

camera.velocity = diffVec;

The normalize function would probably look something like this:

void Vector2D::normalize(){
     float len = length();
     this.x /= len;
     this.y /= len;    
};

 float Vector2D::length(){
    return sqrt(x*x+y*y);
 };

Now if you're not using vectors this could be translated to:

float dx = destinationX-positionX;
float dy = destinationY-positionY;

Let's pretend dx and dy are the components of an imaginary vector. ( And by components I mean the x and y properties ).

We can perform the same calculations as we have in our Vector2D class: First we need to "normalize" our imaginary vector by dividing it by its length. Pythagoras theorem, remember that one?

float length = sqrtf(dx*dx+dy*dy);

Now we use this to normalize our imaginary vector:

dx/=length;
dy/=length;

Now if we were to apply the Pythagoras theorem on our new dx and dy value you will notice that the length is 1. Meaning that we have a direction, and a magnitude of 1. To change it to our speed simply multiply each component by it.

dx *= speed;
dy *= speed;

cam.velocityX = dx;
cam.velocityY = dy;

Imagine a vector to be an arrow with a specific length. When you normalize it you bring the length to one.

enter image description here

So if you multiply it ( by a scalar ), the speed in this case, the x and y components adjust accordingly that the length of the vector is the speed it's multiplied with.

\$\endgroup\$
8
\$\begingroup\$

Forget atan2. You don't need angles. Think in terms of linear algebra approaches.

You have a source and a destination. Subtract source from destination to get a vector representing the change. Normalize this. You now have a unit vector pointing from source to destination. Multiply by speed. You now have the offset to apply each update. It's that easy.

\$\endgroup\$
9
  • \$\begingroup\$ you might want to help him out with some code mate. I'm guessing if he knew what you were talking about he probably wouldn't be asking this question yeh? Lol... I agree though +1 for your answer if you give him an example :) \$\endgroup\$
    – Savlon
    Jan 26, 2013 at 11:59
  • \$\begingroup\$ actually, indeed I dont get it. It sounds interesting though \$\endgroup\$
    – user25581
    Jan 26, 2013 at 12:59
  • \$\begingroup\$ @jimmis98 en.wikipedia.org/wiki/Euclidean_vector \$\endgroup\$
    – Casey
    Jan 26, 2013 at 16:15
  • \$\begingroup\$ I don't know how to teach linear algebra while on my phone, sorry. All I've got these days, been in the hospital for a few weeks due to an accident. \$\endgroup\$ Jan 26, 2013 at 17:23
  • 1
    \$\begingroup\$ @jimmis98 I have updated my answer to include a linear algebra method as stated by Sean. \$\endgroup\$
    – Savlon
    Jan 27, 2013 at 0:13

You must log in to answer this question.