[Solved] Calculating Velocity

Discussion in 'Programming' started by ChaosX2, Jun 13, 2017.

  1. ChaosX2

    ChaosX2 Member

    Joined:
    Aug 14, 2016
    Posts:
    88
    Hello,

    I'm having trouble setting up the pursuit steering behavior to fit my situation. I need to know my players velocity and the player is controlled via arrow keys and I'm not sure how to calculate the player's velocity since I'm not using a steering behavior to propel it in any way. I tried something along the lines of...

    Code:
    steering = scr_vect2(0,0);
    
    steering = scr_vect_add(steering, scr_vect2(x,y));
    
    velocity = scr_vect_add(velocity, scr_vect_subtract(position, steering));
    
    position = scr_vect_add(position, velocity);
    
    I also thought about using the formula of change in position / time, but I'm not sure how to do that or if it's even the right approach :x Can someone enlighten me?
     
  2. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,955
    Player velocity is a vec2 assigned with x and y positional change calculated during the step. That is, vec2(delta x, delta y).
     
  3. ChaosX2

    ChaosX2 Member

    Joined:
    Aug 14, 2016
    Posts:
    88
    Thanks for the reply! I was taking a crack at it before I left for work this morning xD The pursuit behavior still has shaky results. It's probably the code within the behavior script itself but what I did so far was....

    Code:
    //Begin Step Event
    xprevious = x;
    yprevious = y;
    
    //Step Event
    velocity = vect_subtract(vect2(x, y), vect2(xprevious, yprevious));
    
     
  4. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,955
    Pursue behavior should work like this: take the distance between pursuer and player, divide by pursuer's max velocity and call the result a scalar or something. Calculate player's predicted position by multiplying player velocity vector by said scalar and add it to their position vector. The resulting vector is the destination that pursuer should seek towards. In pseudocode
    Code:
    scalar = (length of (player position vector - pursuer position vector)) / pursuer max speed
    pursuit target vector = player position vector + (player velocity vector * scalar)
    call seek behavior for pursuing agent with pursuit target vector as destination vector
    
    Further, in my experience, a steering behavior agent is best constructed as a state machine.
     
  5. ChaosX2

    ChaosX2 Member

    Joined:
    Aug 14, 2016
    Posts:
    88
    Thanks again for the help! I am indeed using a combination of steering behaviors and finite state machines :) I made adjustments to my code and it works.

    The velocity formula has been adjusted to divide by time since it's not frame dependent.

    Code:
    velocity = vect_divR(vect_subtract(scr_vect2(x, y), vect2(xprevious, yprevious)),  global.deltaTime);
    
    Pursuit Behavior
    Code:
    var target = argument0;
    var weight = argument1;
    
    var toEvader = vect_subtract(target.position, position);
    
    var lookAheadTime = vect_len(toEvader) / maxSpeed + (player.playerSpeed * global.deltaTime);
    
    return seekTarget(vect_add(target.position, vect_multiplyR(target.velocity, lookAheadTime)), weight);
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice