Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
4
Question by GlitchEnzo2 · Dec 05, 2010 at 10:48 PM · physicscolliderterraincustomspherical

Create a custom Collider?

Is it possible to make your own custom Collider object in Unity?

I know there is the MeshCollider object, but I'm talking about creating a shape that is defined by an equation. For example, if you wanted to make your own sphere collider (I know there is one built in, this is just an example.), you could check for collisions by seeing if any object is within a certain distance (radius) of the collider.

I know you can create a script class that inherits from Collider, like so:

using UnityEngine; public class CustomCollider : Collider {

}

However, I'm not sure what methods to "override" to check for collisions or whatnot. Perhaps this is not possible at all.

I was able to successfully implement what I'm talking about in Bullet and I talk about it on my blog, if you're interested: http://recreationstudios.blogspot.com/2010/04/spherical-terrain-physics.html

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by skovacs1 · Dec 06, 2010 at 11:54 PM

Hmmm. Kind of an odd question. Perhaps it might be enlightening to have a better understanding what it is exactly you want from Unity's physics engine.

Unity's Physics engine has different collider types which implement known intersection algorithms for simple primitives Sphere, Box, Capsule and Plane. Mesh colliders are be more expensive as they would have to perform in the worst case per-triangle intersections. I believe that most if not all of this is done through PhysX, but that really doesn't matter since we cannot really change how these colliders and their collisions are implemented under the hood.

I'm sorry I don't have a better answer, but because of this closed implementation, I do not believe that you can easily override the base collider if at all. I am afraid you will likely have to make do with the MeshCollider. If you are trying to do something as you describe in the linked blog post (tessellate based on proximity to the camera or something according to some noise?) you would indeed likely have to modify the mesh where you are changing it, using the renderer's mesh interface. You wouldn't have to replace the entire mesh exactly, but you would have to find, change/add the appropriate vertices, triangles, uvs and normals and because of the automated recalculations when this happens, you had best store the arrays in temporary memory, change them and then re-assign them, meaning that for all that it matters, you really are replacing the mesh.

Comment
Add comment · Show 3 · 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 GlitchEnzo2 · Dec 07, 2010 at 07:50 PM 0
Share

The problem stems from the fact that I only use one shared mesh (and one shared material) in order have the best batching. The geometry is completely displaced on the GPU. On the CPU side, the geometry just makes up a cube, but on the GPU it makes up spherical terrain. I cannot use the meshes as they are in Unity for collision because they are not even close to the final render. I also cannot displacement them on the CPU because that would be far too costly.

avatar image GlitchEnzo2 · Dec 07, 2010 at 07:51 PM 0
Share

I've already begun work on a system that is similar to my blog post, and it seems very promising so far. I need to do some more experimentation with it, and then I'll probably post it here.

avatar image skovacs1 · Dec 07, 2010 at 09:29 PM 0
Share

Please do. I'd like to know how you got GPU displacement to affect CPU physics. Did you roll your own physics solution that does some sort of lookup to your displacement or something?

avatar image
1

Answer by GlitchEnzo2 · Dec 08, 2010 at 08:39 AM

I've implemented a system that seems to be working fairly well. It's very similar to the method I implemented in Bullet that I described in my blog. The one major difference is that the triangle processing in Bullet is double sided (collision is in both directions) whereas the MeshCollider in Unity is only single sided (collision is only in 1 direction). That required me to reverse some of my triangles, but that wasn't hard.

Here is my solution: I set up a trigger volume (a sphere) surrounding my spherical terrain, and then I overrode the trigger Enter, Stay, and Exit methods. The Enter and Stay methods are the same. They calculate the 8 corners of the bounding box and then project them down to the terrain. If any of the corners collide with the terrain, then a MeshCollider is created/updated using the 3 closest sides projected to the terrain (only 6 triangles are in each MeshCollider). So, all of the collision response is still handled by Unity/PhysX.

It is optimized slightly by storing the MeshCollider objects in a Dictionary that is keyed off of the original GameObject. That way, only the mesh needs to be updated, instead of destroying and recreating a new GameObject every frame. The Exit method is used to remove the entries from the Dictionary.

I want to add a further optimization where stationary GameObjects (ones just sitting on the terrain) don't have their meshes updated. This will require another Dictionary storing the previous positions, but that should be fairly simple.

As I said it works fairly well. I haven't seen any objects fall through the terrain, and it has no noticeable slow down with 6 objects colliding with the terrain at the same time. I'll plan on doing a test with a lot more objects to see what sort of performance hit there is.

I would post the code here, but it's kind of lengthy (over 150 lines). If anybody is interested in certain parts, I can post those.

Comment
Add comment · Show 8 · 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 Sriram · Dec 08, 2010 at 01:53 PM 0
Share

you could post it to the unity community wiki with a small description :)

avatar image Wulfeous · Sep 08, 2011 at 09:21 AM 0
Share

I'm working on a game that involves driving all around a perfect sphere. Problem is that my sphere is nowhere near perfect. I think you might have the expertise I need in working things out if you had a moment to look it over, please and thank you. Hopefully you still have some of the script you mention in this post, as I would love to learn from it. :) I started a post here

avatar image GlitchEnzo2 · Sep 21, 2011 at 05:39 PM 0
Share

If you want your collider to be a perfect sphere why not use the SphereCollider? $$anonymous$$y solution is useful only if you want spherical terrain (a bumpy, non-perfect sphere).

avatar image Wulfeous · Sep 21, 2011 at 05:44 PM 0
Share

ye. the sphere collider feels right, but the ground still isn't actually spherical. so you end up driving over empty air between edges.

avatar image GlitchEnzo2 · Sep 21, 2011 at 06:43 PM 0
Share

Oh, so the mesh you use for rendering the sphere has gaps? That's a different problem entirely. I'm guessing your sphere is actually a displaced cube made of 6 different sides? Those gaps are usually caused by floating point precision issues.

Show more comments
avatar image
1

Answer by spalmer · Mar 31, 2015 at 03:09 PM

I hate the builtin spheres so much I'm making perfect raytraced replacements. I already have the rendering working for an ellipsoid, now I want a custom collider that handles the ellipsoid properly, but Unity's builtin SphereCollider can't handle NU scale. I've got an idea how to use script to create and manage a unique custom SphereCollider for each potentially penetrating object, update a SphereCollider each frame to correspond to some approximate representation of the closest point on surface to each object as a sphere. What you guys are saying makes me think it might be possible. But if managing the different layers/masks gets to be a problem I can just use a finely tesselated MeshCollider I suppose... nasty. Yeah I realize this all has to run deep inside PhysX's pipeline, but hoped there was some way of implementing some kind of callback.

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
-3

Answer by 1o482013581451 · Apr 30, 2015 at 11:32 AM

Yes it is. All you have to do is go to "add component", "physics", and "mesh collider". It will create a collider in the exact shape of the mesh.

Comment
Add comment · Show 2 · 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 Pangamini · Apr 30, 2015 at 11:34 AM 2
Share

Did you actually read the question?

avatar image berk_can · Mar 21, 2021 at 06:43 PM 0
Share

this is biggest lie, it cant handle nonconvex meshes at all

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

9 People are following this question.

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

Related Questions

Trying to use colliders on trees to interact with player 1 Answer

How do i prevent an imported Mesh from falling through Terrain? 3 Answers

collision with terrain problem? 3 Answers

How to get OnCollisionEnter working with Terrain. JavaScript 2 Answers

Unity 4 - Sphere Falls through Terrain 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