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

Author Topic: Steering behavior problem.  (Read 4006 times)

0 Members and 1 Guest are viewing this topic.

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Steering behavior problem.
« on: August 21, 2013, 08:14:45 am »
Hello! I am reading this tutorial:
http://gamedev.tutsplus.com/tutorials/implementation/understanding-steering-behaviors-seek/

So how do you truncate? I did my homework and I can see all that is there is outputting it. There are some, that truncate a value but I don't even know if that is what I need. I don't need outputs. So how do you do this and why do I need it? Is there SFML or C++ function that I could use?

Also I have a problem, action script is too abstract, what do you mean by scale by? I am not a great fan of functional programming. I don't what scaleBy means in mathematical formula. If you can just give me formula scaleBy and truncate that be great.

Anyway here is action script of that tutorial I am stuck at truncate and scaleBy. I am sorry for my shortcomings when it come to this thing but this is pretty much wasting my time deciphering a hieroglyph.

This code is not exactly how I move things in SFML and I am stuck at truncate and scaleBy.

public function truncate(vector :Vector3D, max :Number) :void {
                        var i :Number;

                        i = max / vector.length;
                        i = i < 1.0 ? 1.0 : i;
                       
                        vector.scaleBy(i);
                }
               
                public function update():void {
                        target = Game.mouse;
                       
                        steering = seek(target);
                       
                        truncate(steering, MAX_FORCE);
                        steering.scaleBy(1 / mass);
                       
                        velocity = velocity.add(steering);
                        truncate(velocity, MAX_VELOCITY);
                       
                        position = position.add(velocity);

                        x = position.x;
                        y = position.y;
                }
 

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Steering behavior problem.
« Reply #1 on: August 21, 2013, 09:09:18 am »
I believe those are just basic vector operations.  scaleBy appears to be simple scalar multiplication, and truncate appears to mean "if the vector's magnitude > x then scale it so it equals x".

A quick example:
Let v be the 2D vector (3,4).
The magnitude of v is sqrt(3^2 + 4^2) = 5.
If we scale v by a factor of 2, we get 2 * (3,4) = (2*3, 2*4) = (6, 4), which has a magnitude of sqrt(6^2 + 4^2) = 10.  Notice that's 2 times the original magnitude.
If we truncate v to have a magnitude of at most 3, that would mean multiplying v by the scalar 3/5, which gets us (1.8, 2.4).


I'd recommend googling some tutorials on vector arithmetic in general since you'll want to know a lot more than just this if you intend on implementing anything resembling physics or intelligent movement.


To answer your other question, since these operations are so simple (once you understand them), SFML and the C++ libraries don't bother providing them directly.  The closest thing I know of is a couple functions in the standard <complex> header.  For instance, std::norm calculates the norm of a complex number, which is basically the same thing as the magnitude of a 2D vector (since a complex number is typically represented with a 2D vector).  But even then, it might be better to implement these functions yourself if only to ensure you actually understand them.

P.S. This has absolutely nothing to do with functional programming, so don't use that term.
« Last Edit: August 21, 2013, 09:14:56 am by Ixrec »

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Re: Steering behavior problem.
« Reply #2 on: August 21, 2013, 04:22:20 pm »
Yes thanks. I've read some tutorials there and that, but I am still struggling.

So I come up with a code, the problem is, my sprite doesn't steer. I don't know what I miss here.

Here is my code so far:
void PathFinder::moveToDestination( float e )
{

    if( !_isPathFound ) return;

    float tilesize = static_cast< float >( tileset->getTilesize() );
    float speed    = e * movableObjects->getVelocity();// NOTE : This should be getSpeed()

    const float MAX_FORCE = 0.4;
    const float mass      = 20.0f;
    const float tolerance = 1.0f;

    sf::Vector2f desired_velocity;
    sf::Vector2f steer;
    sf::Vector2f velocity;

    spritePosition.x = movableObjects->getSpritePositionX();
    spritePosition.y = movableObjects->getSpritePositionY();

    current_velocity = normalize( targetPosition - spritePosition   );
    desired_velocity = normalize( targetPosition - current_velocity ) * speed;

    steer            = desired_velocity - current_velocity;
    steer            = truncate< float >( steer , MAX_FORCE );
    steer           /= mass;

    velocity         = truncate<float>( current_velocity + steer , speed );

    float movex = spritePosition.x + velocity.x;
    float movey = spritePosition.y + velocity.y;

    cout << " current velocity : " << current_velocity.x << " x " << current_velocity.y << endl;
    cout << " desired velocity : " << desired_velocity.x << " x " << desired_velocity.y << endl;
    cout << " steer            : " << steer.x << " x " << steer.y << endl;
    cout << " velocity         : " << velocity.x << " x " << velocity.y << endl;
    cout << endl;

    if( abs( targetPosition.x - spritePosition.x ) <= tolerance
     && abs( targetPosition.y - spritePosition.y ) <= tolerance )
    {
        _isPathFound = false;
    }

    movableObjects->setSpritePosition( movex , movey );

}
 

Here is the functions for my VectorMath its just a bunch of templated functions that I could use to get the magnitude, normal and the truncated vector. So here it is:
#ifndef VECTORMATH_HPP
#define VECTORMATH_HPP

#include<cmath>

template< typename T >
T length( const sf::Vector2< T > &v )
{
    return sqrt( ( v.x * v.x ) + ( v.y * v.y ) );
}

template< typename T  >
sf::Vector2< T > normalize( const sf::Vector2< T > &v )
{
    return v / length( v );
}

template< typename T >
sf::Vector2< T > truncate( const sf::Vector2< T > &v , float nmax )
{
    float i = nmax / length( v );

    i = ( i < 1.0f )? 1.0f : i;

    sf::Vector2< T > n = v;
    n *= i;

    return n;
}

#endif // VECTORMATH_HPP

 

Maybe I got something wrong here? I just base everything on the explanation you have given. Thanks for your time to reply. :D
« Last Edit: August 21, 2013, 04:29:37 pm by magneonx »

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Re: Steering behavior problem.
« Reply #3 on: August 21, 2013, 07:01:14 pm »
Phew after so many moons!
Here is my code:

void PathFinder::moveToDestination( float e )
{
    if( !_isPathFound ) return;

    float tilesize = static_cast< float >( tileset->getTilesize() );

    const float MAX_SPEED    = e * movableObjects->getVelocity();
    const float MAX_VELOCITY = 3.0f;
    const float MAX_FORCE    = 10.4;
    const float mass         = 50.0f;
    const float tolerance    = 1.0f;

    sf::Vector2f desired_velocity;
    sf::Vector2f steer;
    sf::Vector2f velocity;

    spritePosition.x = movableObjects->getSpritePositionX();
    spritePosition.y = movableObjects->getSpritePositionY();

    desired_velocity = normalize( targetPosition - spritePosition ) * MAX_VELOCITY;
    steer.x = desired_velocity.x - MAX_SPEED;
    steer.y = desired_velocity.y - MAX_SPEED;

    steer = truncate( steer , MAX_FORCE );
    steer /=  mass;

    velocity = truncate( velocity + steer , MAX_SPEED );

    float movex = spritePosition.x + velocity.x;
    float movey = spritePosition.y + velocity.y;

    if( abs( targetPosition.x - spritePosition.x ) <= tolerance
     && abs( targetPosition.y - spritePosition.y ) <= tolerance )
    {
        _isPathFound = false;
    }

    movableObjects->setSpritePosition( movex , movey );
}

 

Thanks for your help! I have finally made my character to steer!!! yes. I just have to fiddle with speed and mass to get what I really wanted. phew...
« Last Edit: August 22, 2013, 08:01:00 pm by magneonx »

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Re: Steering behavior problem.
« Reply #4 on: August 22, 2013, 08:02:41 pm »
Hello again I have a problem, how can I set the "orientation" of my steering sprite? I want to make my sprite point to the direction it is heading.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Steering behavior problem.
« Reply #5 on: August 22, 2013, 08:06:03 pm »
setRotation() ?

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Re: Steering behavior problem.
« Reply #6 on: August 25, 2013, 07:50:48 pm »
Ah yes thanks I do know about setRotation. but my problem is how am I suppose to rotate my sprite base on the velocity is heading in. How can I translate that into angle? I am using steering behavior by the way, so its steers and its orientation gradually aligns to the target. I am not good in numbers.

Thanks a lot for your patience!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Steering behavior problem.
« Reply #7 on: August 25, 2013, 08:57:08 pm »
I am not good in numbers.
Then you should read more about vector algebra. Vectors are essential if you implement steering behaviors!

but my problem is how am I suppose to rotate my sprite base on the velocity is heading in. How can I translate that into angle?
With trigonometry, in this case std::atan2(). You could also have a look at Thor.Vectors, more exactly thor::polarAngle(), so you don't have to reinvent the wheel.
« Last Edit: August 25, 2013, 09:07:47 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: