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

Author Topic: Move sprite to attack a random target.  (Read 3300 times)

0 Members and 2 Guests are viewing this topic.

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Move sprite to attack a random target.
« on: December 08, 2012, 07:31:34 pm »
Hello. I have a problem, I am kinda get tired and it kinda late at night so don't get rough on me. Trust me I've researched on this and I am doing my best on studying vector math.

 I am able to make a sprite to move towards a target say, a survivor which I control around the screen and the zombie follow me around. I have done it nice and easy. But I have a problem though, the movement seems to act like limit function, whereas the closer it gets, the slower it becomes. I do not know how to make the speed constant given a vector.

So here is my code I prefer this one over mine:

void Infested_One::update( float elapsed )
{
    sf::Vector2f target_pos( target->getSpritePosition() ); // target position ( survivor)
    sf::Vector2f target_dest( target_pos ); // position to go to target

    sf::Vector2f start( getSprite().getPosition() ); // tells the position of the zomboid
    sf::Vector2f delta = target_dest - start;    // compute for the distance between the survivor and the zomboid

    // normalize the vector
    float dist = sqrt( ( target_dest.x - start.x ) * ( target_dest.x - start.x ) + ( target_dest.y * start.y ) * ( target_dest.y * start.y ) );
    delta /= dist;

    getSprite().setPosition( start + delta * distMoved);

}


I actually implement this code based on a site I just have found here on SFML,
http://en.sfml-dev.org/forums/index.php?topic=6654.0,

 I have my original one which is quite short, and I implemented it just by getting the delta of the distance between 2 objects, then calculating
the displacement by delta * ( elapsedTime / velocity ). I know this is a stupid math formula, please pardon me that. both gave me same result. The zombie gets slower as it approaches its target.


but seems like I don't really know the concept on making the sprite move on a vector with a constant speed.

Thank you very much!

Note: I am using SFML 2.0 and I am implementing this code using the elapsedTime concept
so I used clock.restart().asSeconds() as elapsedTime.


gyscos

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Re: Move sprite to attack a random target.
« Reply #1 on: December 08, 2012, 07:42:59 pm »
Read the distance formula again. You're multiplying instead of substracting the y values.
You could also use the delta variable :
delta.x * delta.x + delta.y = delta.y

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Re: Move sprite to attack a random target.
« Reply #2 on: December 08, 2012, 07:48:02 pm »
Read the distance formula again. You're multiplying instead of substracting the y values.
You could also use the delta variable :
delta.x * delta.x + delta.y = delta.y

THanks again! You're the guy who helped me in my other post.

I don't seem to understand, that part ... delta.y = delta.y? is it delta.y * delta.y


I may try your suggestion its about 3 hrs till 5am so I think I have to get some sleep and come back to this one later.

gyscos

  • Jr. Member
  • **
  • Posts: 69
    • View Profile
Re: Move sprite to attack a random target.
« Reply #3 on: December 08, 2012, 07:49:38 pm »
<_< Yeah, typo...

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Re: Move sprite to attack a random target.
« Reply #4 on: December 08, 2012, 07:52:24 pm »
Oh yeah, what a...

Damn, I think I can't be like this, coding past 1! Alright thanks again, you've helped me many times, I hope I could give you some points for that.

Thanks a lot!

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Re: Move sprite to attack a random target.
« Reply #5 on: December 08, 2012, 07:57:28 pm »
The right code for this is:

void Infested_One::update( float elapsed )
{
    sf::Vector2f target_pos( target->getSpritePosition() ); // target position ( survivor)
    sf::Vector2f target_dest( target_pos ); // position to go to target

    sf::Vector2f start( getSprite().getPosition() ); // tells the position of the zomboid
    sf::Vector2f delta = target_dest - start;    // compute for the distance between the survivor and the zomboid

    // normalize the vector
    float dist = sqrt( ( target_dest.x - start.x ) * ( target_dest.x - start.x ) + ( target_dest.y i start.y ) * ( target_dest.y i start.y ) );
    delta /= dist;
   
     float distMove = elapsed * velocity;

    getSprite().setPosition( start + delta * distMoved);

}
 

Now I can sleep yay! I earned it eh?
Thank you very much.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Move sprite to attack a random target.
« Reply #6 on: December 08, 2012, 08:06:42 pm »
target_dest.y i start.y
Looks like you have to stay awake :P
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything