Home Coding, Scripting, Shaders

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

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.

Replies

  • poopipe
    Offline / Send Message
    poopipe polycount lvl 666
    Can you explain what you want to happen a bit?

    you want to move the projectile from point a to point b 

    Do  you need to calculate the velocity and arc that it follows or do you already have some of tha information? 
  • andla
    I have a video that explains the whole problem but I'm not sure if I can post it here.
  • andla
    However I figured things out but I can't understand why it works.
    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_height = target.Position.y;

         float gravity = Globals.GRAVITY;
         if(arc_height>0)
         {
             gravity = Globals.GRAVITY/3;
             arc_height *=-1;
             
         }
         //GD.Print(arc_height);
          //GD.Print(target.Position.x);
         //float arc_height = 100;
       
         //velocity = throw_velocity*new Vector2(direction,1);
         //Vector2 arc_height = target.GlobalPosition-start.GlobalPosition;
         velocity = PhysicsHelper.calculate_arc_velocity(startpos,targetpos,arc_height,gravity);
         GD.Print(velocity);
         //velocity.x = 100;
         //velocity.y = -100;
        
         //velocity =throw_velocity+velocity;
         //  GD.Print(arc_height);
          //  GD.Print(up_gravity);
         //velocity/=2;
     }

    This works fine.

Sign In or Register to comment.