Skip to main content
9 of 9
added 626 characters in body
Shoe
  • 917
  • 11
  • 22

Shaking objects near correct position (with video)

All right, so I'm testing two box objects. One is standing fixed in a position and another at the beginning of the program goes to the first one. And at the end the latter should stand completely over the first (so the center of both is equal).

Here's my method in the Object class:

Old code

void Object::moveTo(Point _p) { // _p is always the center of the other object we want to go to

    // adjust speed, don't worry it's not important here, I guess
    if (current_speed == 0)
        current_speed = speed/16;
    else if (current_speed >= speed)
        current_speed = speed;
    else
        current_speed *= 2;

    // we want to calculate the new center of the
    // our object (the one that run this method)
    Point center;
    // distance(x) calculate the distance between the center point of the object and
    // the x point passed
    if (distance(_p) <= current_speed) {
        // we don't want shaking things!
        center = Point(_p.x, _p.y);
    } else {
        // move diagonally
        center.x = getCenter().x + current_speed*std::cos(angle(_p)); // angle calculates the angle between the center of the object and the _p point
        center.y = getCenter().y + current_speed*std::sin(angle(_p));
    }
    // since we draw sprites by the top left corner we need to convert
    // the center point to the correct top left point
    center = toPosition(center);
    sprite.setPosition(center);
}

New code

Notice that the origin of the sprite is set to be in the center so setPosition will correctly set the center and that getCenter() returns sprite.getPosition() which is the center point.

void Object::moveTo(Point _p) {
    Point center;
    //std::cout << distance(_p) << std::endl;
    if (distance(_p) <= speed) {
        center = Point(_p.x, _p.y);
    } else {
        center.x = getCenter().x + speed*std::cos(angle(_p));
        center.y = getCenter().y + speed*std::sin(angle(_p));
    }
    sprite.setPosition(center);
}

This is what happens: http://youtu.be/Ngra3FXFe0A.

The distance keeps going from 15.xxx to 16.xxx and I don't know why.

Testing surprise

  • Doing distance(_p) <= 10 instead doesn't make it shake. With 9, 8... or minor it shakes.
  • Defining a minor speed the object gets nearer the correct position before start shaking.
  • Setting the white cube to follow the player (yellow sprite) it moves like a jagger: http://youtu.be/9FjRfHHprEQ
  • Here's a recent video: the 3 little squares points (its the specific top left corner) to: blue = box center, red = player center, green = top left corner of the box. As you can see they are correctly calculated (this test was made with speed of 1, while the others were made with a greater speed): http://youtu.be/iFAWGssk7NI

Distance function:

float Object::distance(Point _p) {
    return sqrt((pow((getCenter().x - _p.x), 2) + pow((getCenter().y - _p.y), 2)));
}

Angle function

float Object::angle(Point _p) {
    //  (180 / PI = 57.3065)
    return 57.3065f * atan2(getCenter().y - _p.y, getCenter().x - _p.x);
}

Why is that?

Shoe
  • 917
  • 11
  • 22