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.
  1.  
  2. static public Vector2 calculate_arc_velocity(Vector2 point_a, Vector2 point_b, float arc_height,float up_gravity=30, float? down_gravity=null)
  3. {
  4. if(down_gravity==null)
  5. down_gravity=up_gravity;
  6. Vector2 velocity = new Vector2();
  7. Vector2 displacement = point_b-point_a;
  8. //GD.Print(displacement);
  9. if(displacement.y>arc_height)
  10. {
  11. var time_up = Mathf.Sqrt(-2F*(float)arc_height/(float)up_gravity);
  12. var time_down = Mathf.Sqrt(2F*((float)displacement.y-(float)arc_height)/(float)down_gravity);
  13. velocity.y = -Mathf.Sqrt(-2F*(float)up_gravity*(float)arc_height);
  14. velocity.x = (float)displacement.x/(float)time_up+(float)time_down;
  15. }
  16. return velocity;
  17. }
  18.  
  19.  
  20. }
  1.  
  2. public void launch(Node2D start, Node2D target, float direction)
  3. {
  4. Vector2 startpos = start.GlobalPosition;
  5. targetpos = target.GlobalPosition;
  6. float up_gravity= (target.GlobalPosition.y-start.GlobalPosition.y)/2;
  7. float arc_heigh = (target.GlobalPosition.y-start.GlobalPosition.y)-50;
  8. //velocity = throw_velocity*new Vector2(direction,1);
  9. //Vector2 arc_height = target.GlobalPosition-start.GlobalPosition;
  10. velocity = PhysicsHelper.calculate_arc_velocity(startpos,targetpos,arc_heigh,-arc_heigh);
  11. //velocity =throw_velocity+velocity;
  12. GD.Print(arc_heigh);
  13. GD.Print(up_gravity);
  14. velocity/=2;
  15. }
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 grand marshal polycounter
    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.
    1. public void launch(Node2D start, Node2D target, float direction)
    2. {
    3. Vector2 startpos = start.GlobalPosition;
    4. targetpos = target.GlobalPosition;
    5. //float up_gravity= (target.GlobalPosition.y-start.GlobalPosition.y)/2;
    6.  
    7. float arc_height = target.Position.y;
    8.  
    9. float gravity = Globals.GRAVITY;
    10. if(arc_height>0)
    11. {
    12. gravity = Globals.GRAVITY/3;
    13. arc_height *=-1;
    14. }
    15. //GD.Print(arc_height);
    16. //GD.Print(target.Position.x);
    17. //float arc_height = 100;
    18. //velocity = throw_velocity*new Vector2(direction,1);
    19. //Vector2 arc_height = target.GlobalPosition-start.GlobalPosition;
    20. velocity = PhysicsHelper.calculate_arc_velocity(startpos,targetpos,arc_height,gravity);
    21. GD.Print(velocity);
    22. //velocity.x = 100;
    23. //velocity.y = -100;
    24. //velocity =throw_velocity+velocity;
    25. // GD.Print(arc_height);
    26. // GD.Print(up_gravity);
    27. //velocity/=2;

    This works fine.

  • umutbalkan
    it's pronounced as go-doh not dot. like the book, waiting for godot by samuel beckett
Sign In or Register to comment.