• Register
Forum Thread
  Posts  
Godot Platformer - Throwing projectiles - How to make it land in a certain point (Forums : Coding & Scripting : Godot Platformer - Throwing projectiles - How to make it land in a certain point) Locked
Thread Options
Jul 3 2020 Anchor

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.

Reply to thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.