Announcement

Collapse
No announcement yet.

Best way to achieve cannon auto aiming?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Best way to achieve cannon auto aiming?

    I am trying to create a self aiming cannon, that depending on the x and y coordinates of the target, would determine the perfect angle and velocity.

    I have a cannon made from this tutorial: https://www.youtube.com/watch?v=uFr-7At2lXQ

    And as a next step for the project I have added a target in front of the cannon like so [pic1]

    The problem is I cannot find a decent way to make the cannon itself properly set the values.
    I have tried using the Projectile Motion equations from wikipedia, but they do not seem to do the trick at all!

    My idea is to get present distance and the height of the target and keep increasing the angle and V, untill the predicted path matches the one of the target.

    Does anyone happen to know a decent source I can use for such equations, or how to implement them?


    I know this is a noob question, sorry for that. new on the topic.

    In the images i present the setup along with the setters and how firing the cannon works
    A human would do everything to make his life more simple..
    even absurdly over-complicate it.
    -Christopher Henselt "How I Broke Atlantis"

    #2
    Originally posted by aln447 View Post
    I am trying to create a self aiming cannon, that depending on the x and y coordinates of the target, would determine the perfect angle and velocity.

    I have a cannon made from this tutorial: https://www.youtube.com/watch?v=uFr-7At2lXQ

    And as a next step for the project I have added a target in front of the cannon like so [pic1]

    The problem is I cannot find a decent way to make the cannon itself properly set the values.
    I have tried using the Projectile Motion equations from wikipedia, but they do not seem to do the trick at all!

    My idea is to get present distance and the height of the target and keep increasing the angle and V, untill the predicted path matches the one of the target.

    Does anyone happen to know a decent source I can use for such equations, or how to implement them?


    I know this is a noob question, sorry for that. new on the topic.

    In the images i present the setup along with the setters and how firing the cannon works
    So I can help you a bit, but not fully and probably not the "best way". As this explanation only work with no arcs(no gravity) but hopefully points you in the right direction.

    Click image for larger version  Name:	CannonRotation.jpg Views:	1 Size:	86.3 KB ID:	1470262

    So what you want your cannon to always aim at a predicted location. The predicted location is pretty self explanatory. But its the predicted location of the character by the time the projectile will land. Or something...

    Get the distance between the projectile spawn and player. Then divide the distance by speed to get "Time to Player". (This you'll have to change as arc's add more time and the formula distance/speed = time wont work). Ok now that you figure the "Time to Player" you can find out predicted distance.
    Click image for larger version  Name:	CannonTimeToPlayer.jpg Views:	1 Size:	55.8 KB ID:	1470263

    Now with that we know how long it'll take the projectile to hit our player we can setup the prediction. Get the players velocity as that give the direction the player is moving to divide it by the player speed or just normalize it if speed is always changing. Now we find the predicted distance by multiplying Player Speed to "Time to Player". 600 is my players speed in this case.

    Now we have the predicted distance we can add that to the player location to give us the "Predicted Location"

    Click image for larger version  Name:	CannonSetWorldLocation.jpg Views:	1 Size:	78.4 KB ID:	1470264

    The Result is this.



    As you can see the white plane is the predicted location. The cannon fires every second at that location. Obviously you'll need to tweek it for your own needs (pitch/angle and arc time). Also the predicted location isn't 100% accurate. Time to Player has a different time to the predicted location but its only off by milliseconds I think.

    I don't know if this helps you but I hope it points you in the right direction and that it'll help someone. Good luck.
    Last edited by SkeetonYu; 05-02-2018, 04:33 PM.
    Checkout Project "TwinBlaster" my current game in progress.
    [FREE] - 2D ROPE! Find out more here!
    Forgive my username... It's an old game name and wrote it down as a habit.

    Comment

    Working...
    X