Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 JPB18 · Aug 26, 2014 at 01:08 PM · physics3dprojectileintersect

Calculate Projectille impact point

Hey all!

So I'm here attempting to so a function that calculates the most probable impact point between the projectile and the moving object. However, when I run it, the projectille appears to choose a random direction instead of the desired point, and it doesn't even gets close to the target.

Here's the function:

  ///<summary>This function calculates the most likelly colision between two objects a (target) and b (projectille)</summary>
 ///<param name="Pa">Vector3 with the position of object a</param>
 ///<param name="Va">Vector3 with the velocity of object a</param>
 ///<param name="Pb">Vector3 with the position of object b</param>
 ///<param name="Vb">Vector3 with the velocity of object b</param>
 ///<returns>Most likelly intersect point</returns>
 function calculateIntersectPoint(Pa : Vector3, Va : Vector3, Pb : Vector3, Vb : Vector3) : Vector3 {
     var t : float = calculateIntersectTime(Pa, Va, Pb, Vb);
     return calculatePosition(Pa, Va, t);
 }
 
 function calculatePosition(p : Vector3, v : Vector3, t : float) : Vector3 {
     return p + (v*t);
 }
 ///<summary>This function calculates the intersect time</summary>
 ///<param name="Pa">Vector3 with the position of object a</param>
 ///<param name="Va">Vector3 with the velocity of object a</param>
 ///<param name="Pb">Vector3 with the position of object b</param>
 ///<param name="Vb">Vector3 with the velocity of object b</param>
 ///<returns>Most likelly intersect point</returns>
 function calculateIntersectTime(Pa : Vector3, Va : Vector3, Pb : Vector3, Vb : Vector3) {
     var Pab : Vector3 = Pa - Pb;
     var Vab : Vector3 = Va - Vb;
     var a  : float = Vector3.Dot(Vab, Vab);
     var b : float = Vector3.Dot(Pab, Pab);
     var c : float = b;
     
     var discriminant : float = calculateDiscriminant(a,b,c);
     
     var t : float;
     
     if(discriminant <= 0) {
         t = calculateSinglePoint(a, b);
     } else {
         t = calculateTwoPoints(a, b, discriminant);
     }
     
     if(t < 0) {
         t = 0;
     }
     
     return t;
 
 }
 
 ///<summary>Calculates de discriminant of a quadratic function</summary>
 function calculateDiscriminant(a : float, b : float, c : float) : float {
     return b * b - 4 * a * c;
 }
 
 function calculateSinglePoint(a : float, b : float) : float {
     return -(b/(2*a));
 }
 
 function calculateTwoPoints(a : float, b : float, discriminant : float) : float {
     var t0 : float = (-b + Mathf.Sqrt(discriminant))/(2*a);
     var t1 : float = (-b - Mathf.Sqrt(discriminant))/(2*a);
     return Mathf.Min(t0, t1);
 }

And here's how it's called:

 function OnEnable() {
     
     var audio : AudioSource = gameObject.GetComponent(AudioSource);
     audio.Play();
     
     rigidbody.velocity = status.speed * transform.forward;
     
     if(target)
     {    
         var trans : Transform = target.transform;
         var Pa : Vector3 = trans.position;
         var Va : Vector3 = trans.rigidbody.velocity;
         var Pb : Vector3 = transform.position;
         var Vb : Vector3 = rigidbody.velocity;
         var intersect : Vector3 = calculateIntersectPoint(Pa, Va, Pb, Vb);
         transform.LookAt(intersect);
     }
 
 
     
     
     launched = Time.time;
     effect.setExploded(false);
     
     
     
 }
 

So, does anyone knows why this is happening? If so, what can I change in order to make it work? Thanks in advance!

João Borrego

Comment
Add comment · Show 2
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
avatar image jokim · Aug 26, 2014 at 03:30 PM 0
Share

Just a quick question, is this supposed to be in a 3d world or 2d? I see vector3, but we never know...

also, is it that much of a problem to change the bullet's orientation every frame ?

avatar image JPB18 · Aug 26, 2014 at 03:57 PM 0
Share

3D world. The point here is that the "bullet" direction only gets setup at the start, so if the target changes direction, the bullet will miss. Also there might be situations where 500 projectiles are flying around and changing the orientation every frame might kill a bit of performance...

2 Replies

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

Answer by jokim · Sep 04, 2014 at 07:46 PM

Aight, After searching the net for a week, I finally found a satisfying (and understandable - or at least well explained) answer!

Here : http://danikgames.com/blog/?p=52

This guy actually illustrates what he does, it's a very good starting point.

Now, at the end of his blog post, he actually links to a Unity3d version of it, Here

Comment
Add comment · Show 1 · 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
avatar image JPB18 · Sep 06, 2014 at 02:03 PM 0
Share

Thank you, that helped a lot! :D

avatar image
0

Answer by LSPressWorks · Aug 26, 2014 at 04:05 PM

I just found this while trying to find more info on my issue, seems like it might be helpful.

http://forum.unity3d.com/threads/calculating-impact-velocity-on-collision.35909/

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

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to make bullet ascend/descend relative to uneven ground level? 2 Answers

Throwing a Spear 1 Answer

Network physics problem in client side,collision problem over network 0 Answers

Jump with physics 1 Answer

How to make my Character run through walls with the use of the Character Controller component 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