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 Juice118 · Apr 11, 2012 at 03:39 PM · physicscollisions

Accurate Position of Object on Collision

Hey guys,

Using collison points to attempt to spawn an object in the same position of an object at the collision.

 void OnCollisionEnter(Collision collision )
 {
   foreach (ContactPoint contact in collision.contacts)
     {
       if(contact.otherCollider.name == "Sphere2" && CollisionOccurred == false)
         {
           SpawnPoint = contact.thisCollider.transform.position;  
           CollisionOccured = true;     
         }
     }
 }

Then go on to create object at SpawnPoint.

My problem is contact.thisCollider.transform.position; seems to be a position a couple of frames after the collision and this is really noticable if the collision is at high speed. Eg if the object hits the "sphere2" and 'bounces' to the left quickly, SpawnPoint is noticeably left of where the sphere actually was at the collision point. Accuracy is important for my project.

Anyone else had this? I can't even understand how this is possible.

Any suggestions on how to Spawn at the exact location of an object at collision accurately?

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

2 Replies

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

Answer by Kryptos · Apr 11, 2012 at 09:58 PM

This is not due to high speed (although the difference is much more visible in those cases).

The Collision struct that you get in the OnCollisionXXX methods contains data about the current state of the objects, i.e. after the collision. In fact, the physics system need several cycles to reach a stable state. Those intermediary states are hidden (and happen in a single FixedUpdate), only the stable one is returned.

Position of objects at the collision:

The best solution is to track the previous values of position/rotation, with something like:

 private Vector3[] prevPosition = new Vector3[2];
 private Quaternion[] prevRotation = new Quaternion[2];

 void FixedUpdate()
 {
     prevPosition[1] = prevPosition[0];
     prevPosition[0] = rigidbody.position;
 
     prevRotation[1] = prevRotation[0];
     prevRotation[0] = rigidbody.rotation;
 }

Then prevPosition[1] and prevRotation[1] always contain the previous value.

Position of the average collision point:

 Vector3 v3AveragePoint;
 foreach( ContactPoint contact in collision.contacts )
 {
     v3AveragePoint += contact.position;
 }
 v3AveragePoint /= collision.contacts.Length;
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
avatar image
1

Answer by Kleptomaniac · Apr 11, 2012 at 03:53 PM

If your collisions are occurring at high speed, comparatively, your foreach could take some time to iterate through every contact point. This could mean it takes that fractional amount of time too long to save the transform.position for the spawnpoint.

I'm not particularly sure how you could get past this, unless of course you were to use a trigger, in which case you wouldn't need to iterate through the contacts array.

Hope that helps, Klep

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

6 People are following this question.

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

Related Questions

Mesh Collider colliding with Primitives is jittery 1 Answer

RigidBody controller - Where to start? 0 Answers

Collisions occassionaly fail. 4 Answers

How do I detect current collisions without using onCollisionEnter? 0 Answers

Ignore collisions with self but still trigger with self 2 Answers


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