Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kheryo · Oct 28, 2015 at 05:55 PM · colliderstrajectoryprediction

2D Circle projectile trajectory prediction : taking collider radius into account

Greetings.

I am currently working on a game in which I will use trajectory prediction on 2D gameObjects using 2D Circle Colliders, in order to get an AI to intercept a projectile.

I've made a pretty simple trajectory prediction algorithm, based on looping this pseudocode

 // FixedUpdate simulation : Apply projectile curve effect
     
 // Physics simulation :
 // Check for collision
 // |- If collision :
 // |  |- Set position to collision point (remember travelled distance) 
 // |  |- Set new velocity vector according to projectile parameters (custom bounce)
 // |  |- Set new curve effect according to projectile parameters
 // |  '- Travel along new velocity vector (remaining travel distance for this tick)
 // '- Else (no collision) :
 //    '- Travel along current velocity vector

Actual code can be found at the bottom of this post as it is quite long.

The problem I encountered is that my prediction algorithm is based on simulating position, velocity and curve of the projectiles, and as such, does not take their collider radius into account as is.

When using it to predict trajectory, even though it allows me to visualize the custom behaviour of my projectiles, the collision point is wrong, as a larger radius projectile will collide "sooner" than the prediction and thus make it inexact.

Example : http://puu.sh/l0LEJ/58280476d4.gif

I've been trying a few different ways to take the collider radius into account, be it during the raycast, or during the repositionning relative to collision point, but didn't manage to achieve satisfactory results.

I would be very grateful if you could assist me in solving this problem, or point me to something I might have missed or overlooked in my implementation.

 List<Vector2> PredictTrajectory(ProjectileScript projectileScript, int cycles)
 {
     List<Vector2> projectileInterceptPoints = new List<Vector2>();
     Vector2 projectilePosition = projectileScript.GetPosition();
     Vector2 projectileVelocity = projectileScript.GetVelocity();
     float projectileCurve = projectileScript.GetCurve();
     ProjectileInfo projectileInfo = projectileScript.GetProjectileInfo();
 
     for (int i = 0; i < cycles; i++)
     {
         // FixedUpdate simulation : Apply projectile curve effect
         if (projectileCurve != 0)
             projectileVelocity = AddAngleToVector2(projectileVelocity, projectileCurve);
 
         // Physics simulation :
         // Check for collision
         // |- If collision :
         // |  |- Set position to collision point (remember travelled distance)
         // |  |- Set new velocity according to projectile parameters (custom bounce)
         // |  |- Set new curve according to projectile parameters
         // |  '- Travel along new velocity vector (remaining travel distance for this tick)
         // '- Else (no collision) :
         //    '- Travel along current velocity vector
         RaycastHit2D hit;
         hit = Physics2D.Raycast(projectilePosition, projectileVelocity.normalized, (projectileVelocity.magnitude * Time.fixedDeltaTime), 1 << LayerMask.NameToLayer("Walls"));
 
         if (hit.collider != null)
         {
             // Get distance to collision point and remaining distance
             // TODO take into account circle collider radius
             float distanceToCollisionPoint = Vector2.Distance(projectilePosition, hit.point);
             float remainingTravelDistance = (projectileVelocity.magnitude * Time.fixedDeltaTime) - distanceToCollisionPoint;
 
             // Set position to collision point
             projectilePosition = hit.point;
 
             // Set new velocity according to projectile parameters
             if (hit.transform.tag == "BottomWall") // UNITY ANSWERS : Other cases omitted
                 projectileVelocity = projectileVelocity.magnitude * (GetVector2FromAngle(projectileInfo.customBounceAngles.BottomWall));
 
             // Set new curve according to projectile parameters
             if (hit.transform.tag == "BottomWall")
                 projectileCurve = projectileInfo.customBounceCurves.BottomWall; // UNITY ANSWERS : 0° in this case
 
             // Travel along new velocity vector (remaining travel distance for this tick since collision)
             projectilePosition += projectileVelocity.normalized * remainingTravelDistance;
         }
         else
         {
             // Travel along current velocity vector
             projectilePosition += projectileVelocity * Time.fixedDeltaTime;
         }
 
         projectileInterceptPoints.Add(projectilePosition);
     }
 
     return projectileInterceptPoints;
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Kheryo · Nov 02, 2015 at 01:55 PM

Fixed with some RTFM

Reading material : http://docs.unity3d.com/ScriptReference/Physics2D.OverlapCircleAll.html Used this to detect collision instead of raycast like so :

                     Collider2D[] colliders = Physics2D.OverlapCircleAll(projectilePosition, projectileCollider.radius, 1 << LayerMask.NameToLayer("Walls"));
 
                     if (colliders.Length > 0)
                     {
                         // Proceed with collision stuff
                     }
                     else
                     {
                         // Travel along current velocity vector
                         discPosition += discVelocity * Time.fixedDeltaTime;
                     }

The only downside is that it doesn't take the current projectile velocity / direction into account like a raycast, and results in the simulation getting "stuck" on the first valid collider hit unless I manually move it away on the same tick collision is processed.

Result : http://puu.sh/l6C66/dbb7d0e7ff.gif

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

2D Trajectory prediction in open space 1 Answer

2D projectile trajectory prediction 2 Answers

how to draw trajectory of rocket for faux gravity in unity???? 0 Answers

How to predict where the ball will land after being hit by bat? 4 Answers

Trajectory prediction basketball 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges