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
1
Question by Hoeloe · Dec 04, 2019 at 12:00 AM · physicsphysics2dcollision2dclosest

Get point on surface of Collider2D from inside collider.

Hey there,

I'm trying to do some 2D Verlet Integration physics. It's mostly working, but one hurdle that's popped up is trying to merge it with existing physics objects. The effect I'm going for is to treat Unity's colliders as static geometry that my Verlet objects are not allowed to intersect. The trouble is, to do this I need to construct a constraint for how to resolve the case where a Verlet node IS inside a collider, and how to move it out. The simplest and most effective result would be "move the node to the nearest point on the surface of the Collider". Sounds simple enough, and at first glance it seems like Collider2D.ClosestPoint would do the job.

However, with ClosestPoint, if the point specified is already inside the collider, that point is just returned without being changed. This of course is completely useless for this use case.

I've tried a few methods to resolve this, such as using an iterator to find a point near to the surface, but outside the collider, and finding the closest point to that, but it's somewhat unstable. I've also tried constructing my own function to find this point, but this is quite slow, and complicated to build.

I'm wondering if there's any sensible way to achieve this. Does anyone have any ideas? Thanks!

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
0

Answer by lgarczyn · Dec 04, 2019 at 07:00 PM

I think that if you use Collider.Raycast backward (not Physics.Raycast) so that the ray passes through both the center of mass and your point from the outside of the collider, you might get an approximation of your nearest point. However I believe the math for the nearest point depends on the collider type, and can only be generalized through some algorythm slowly approaching the target value.


Of course it would be better to avoid such problem by using raycasts to prevent a collision from happened in the first place.

Comment
Add comment · Show 4 · 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 Hoeloe · Dec 04, 2019 at 07:02 PM 0
Share

Unfortunately, this doesn't work. Collider.Raycast may do this, but Collider2D.Raycast behaves differently, and acts as a ray from the centre of the collider, ignoring the collider provided.

Also, since this is a Verlet simulation, "prevent collisions" isn't an appropriate method, since that's not how Verlet simulations work.

avatar image lgarczyn Hoeloe · Dec 05, 2019 at 10:43 PM 0
Share

A raycast from inside the collider will always ignore it, that's why I told you to invert it.

Let A be your center of mass, B the point you wish to move.

Let A + (B-A) * 100 be the start of your raycast, and (A-B) the direction.

The former can also be written Vector2.LerpUnclamped(A, B, 100)

avatar image Hoeloe lgarczyn · Dec 05, 2019 at 10:44 PM 0
Share

That won't work either. Collider2D.Raycast doesn't allow you to set the position of the start of the raycast - it always goes from the centre of the collider. There is no way, as far as I know, to perform a raycast against a specific collider when using 2D physics.

Show more comments

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

193 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 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 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 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 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 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 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 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 avatar image

Related Questions

Is it possible to find the contact point of a circle collider 2D with another collider inside of OnTriggerEnter2D? 1 Answer

Trigger is not executed 1 Answer

Inexact rebound when colliding into corner of two walls. 0 Answers

What exactly is RelativeVelocity? 1 Answer

Workarounds for Higher Control When Using Physics2D? 0 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