Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Waypoint movement.  (Read 3890 times)

0 Members and 1 Guest are viewing this topic.

JLM

  • Newbie
  • *
  • Posts: 8
    • View Profile
Waypoint movement.
« on: June 05, 2014, 10:39:36 pm »
I would like to make sprite move from one waypoint to an other. But I have no idea how to calculate it. I tried to search it but google didn't help at all. If you have any idea how to make sprite move from one position to the other position in straight line instead moving right then down etc. Please tell me.
« Last Edit: June 05, 2014, 10:41:16 pm by JojololomenPL »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Waypoint movement.
« Reply #1 on: June 05, 2014, 10:49:26 pm »
If it's just a straight line, then all you do is move the sprite right and down at the same time.  If that doesn't answer you're question, you'll have to describe the problem in more detail so we can figure out where exactly you're stuck.

In general, to move things around in whatever 2D pattern you want, you're going to need to know some basic geometry, vector algebra and trigonometry.  There's plenty of google-able resources out there once you know that's what you're after.  Also, the Thor library has some useful stuff for vector math.

JLM

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Waypoint movement.
« Reply #2 on: June 05, 2014, 11:05:27 pm »
I mean moving for example from 0,0 to 458,135. Becouse it won't be 45o movement.

P.S. I thought it is able to do it without advanced maths, vectors etc. But I was wrong.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Waypoint movement.
« Reply #3 on: June 05, 2014, 11:17:01 pm »
Vector algebra is exactly what you use here.  Compute the unit vector that points from (0,0) to (458.135), then multiply that by whatever your desired speed is, and you have your desired velocity.  Use that velocity to increment the object's x and y positions each frame.  That's all there is to it.

JLM

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Waypoint movement.
« Reply #4 on: June 05, 2014, 11:20:38 pm »
Thanks man! Is there any way to give you cheer, like or something?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Waypoint movement.
« Reply #5 on: June 05, 2014, 11:23:59 pm »
You can always post your work in the projects forum when it's done =)

whitebeard86

  • Newbie
  • *
  • Posts: 5
  • Gibbo2D Engine Founder
    • View Profile
    • My Portfolio
    • Email
Re: Waypoint movement.
« Reply #6 on: July 01, 2014, 01:05:14 pm »
Example:

* atan2, cos and sin are from math.h

// rotate to waypoint:
myObj.rotation = atan2(waypoint.y - myObj.y, waypoint.x - myObj.x);
// or just calculate this value into a variable if you don't want to rotate your object

// move forward (into the waypoint, based on the current object rotation)
myObj.position.x = cos(myObj.rotation) * SPEED * deltaTime;
myObj.position.y = sin(myObj.rotation) * SPEED * deltaTime;

Note that SFML uses degrees and the cos/sin/atan2 functions use radians. Make the conversion first.

If you want to stop/change waypoint using the distance between your object and the waypoint here is a simple distance function:

float distance(sf::Vector2f p1, sf::Vector2f p2) {
        float xb = p2.x - p1.x;
        float yb = p2.y - p1.y;
        return sqrt((xb * xb) + (yb * yb));
}

I'm not sure if SFML has built in functions for this (i'm new here), but this is how I do it in most game development environments.
« Last Edit: July 01, 2014, 04:24:53 pm by whitebeard86 »
Everyone is entitled to their own opinion, you shared yours, and I shared mine.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Waypoint movement.
« Reply #7 on: July 01, 2014, 10:06:24 pm »
SFML does not, but I believe Thor does.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Waypoint movement.
« Reply #8 on: July 01, 2014, 10:18:08 pm »
Indeed :)

Distance would be the length of the vector (thor::length), and rotation would be the polar angle (thor::polarAngle). One option is also the thor::PolarVector2f class, which stores magnitude and angle instead of X and Y coordinates.

With Thor, whitebeard86's code would look as follows (including a few corrections):
myObj.rotation = thor::polarAngle(waypoint - myObj.position);
myObj.position += thor::unitVector(waypoint - myObj.position) * SPEED * deltaTime;
float distance(sf::Vector2f p1, sf::Vector2f p2) {
    return thor::length(p1 - p2);
}
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: