Advertisement

Godot Platformer - Throwing projectiles - How to make it land in a certain point

Started by July 05, 2020 12:09 PM
2 comments, last by andla 4 years, 7 months ago

Please I need your help to figure this out.

I have this formula that I have been trying to apply to my project.

I use the GoDot engine to create a 2D platformer game with projectiles.

static public Vector2 calculate_arc_velocity(Vector2 point_a, Vector2 point_b, float arc_height,float up_gravity=30, float? down_gravity=null)

{

   if(down_gravity==null)
       down_gravity=up_gravity;
   Vector2 velocity = new Vector2();
   Vector2 displacement = point_b-point_a;
   //GD.Print(displacement);
   if(displacement.y>arc_height)
   {
       var time_up = Mathf.Sqrt(-2F*(float)arc_height/(float)up_gravity);
       var time_down = Mathf.Sqrt(2F*((float)displacement.y-(float)arc_height)/(float)down_gravity);
       velocity.y = -Mathf.Sqrt(-2F*(float)up_gravity*(float)arc_height);
       velocity.x = (float)displacement.x/(float)time_up+(float)time_down;

   }
   return velocity;

}
}



public void launch(Node2D start, Node2D target, float direction)
{
    Vector2 startpos = start.GlobalPosition;
    targetpos = target.GlobalPosition;
    float up_gravity= (target.GlobalPosition.y-start.GlobalPosition.y)/2;
    float arc_heigh = (target.GlobalPosition.y-start.GlobalPosition.y)-50;
    //velocity = throw_velocity*new Vector2(direction,1);
    //Vector2 arc_height = target.GlobalPosition-start.GlobalPosition;
    velocity = PhysicsHelper.calculate_arc_velocity(startpos,targetpos,arc_heigh,-arc_heigh);
    //velocity =throw_velocity+velocity;
      GD.Print(arc_heigh);
       GD.Print(up_gravity);
    velocity/=2;
}

The calculate_arc_velocity should do the job I think but the 'launch' method is just really messy.

My up_gravity and arc_heigh is just a desperate attempt to make it work. I started to mess with that because I couldn't make it work when I started to move the mouse up.

I greatly appreciate your help with this.

andla said:
velocity.x = (float)displacement.x/(float)time_up+(float)time_down;

Why are you adding time_down to a velocity?

But anyway, the trick with math is that you cannot code it until you understand how to do the math calculation. So close the computer, grab a pencil and paper, and work out what to compute before you try to code it. Compute a few cases by hand until you are sure you understand it.

I don't know what formula you are using, I am assuming the usual gravity formula. Usually, you either have a fixed angle and you must decide a velocity, or you have a fixed velocity and you must compute an angle. I can't see what you're doing here, it seems like you compute a both velocity and an angle, but that gives you infinitely many solutions (for each velocity there is an angle and vice versa). A height can be input as well, as it's basically equivalent to vertical velocity.

The simpler axis is the horizontal axis, as you have linear movement there (assuming you have no friction), so that's usually the simplest way to compute the horizontal position of the highest point of the flight path, as wel as the time to reach the target (horizontally). No friction means the path is symmetric before and after the highest point.

Advertisement

I'm just following Game Endeavor on youtube and he made a video about ranged attacks. The formula will predict where the projectile will land. I know math is not my strong side and I accept that but most things in GoDot uses math behind the scenes but all that advanced stuff is already done. I can just plug it in to my game.

The formula I presented though is not. I just need to figure out the environment where it can work properly.
I will need time to read your comment and digest it but I thank you for your input. If you want I can send you the test project I'm working on.
Thanks ?

This topic is closed to new replies.

Advertisement