Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Concave mesh collider with dynamic rigidbody... Not possible?

Discussion in 'Physics' started by BenjaminApprill, Dec 15, 2023.

  1. BenjaminApprill

    BenjaminApprill

    Joined:
    Aug 1, 2022
    Posts:
    132
    Hello!

    I've been reading through 3D Physics in the Unity documentation for a solution to a development problem.

    I am trying to create a gun barrel that sits inside a turret mount. I would like the barrel to adhere to the physics of the mount, retain concave status so a bullet can pass through it, and be able to move back and forth as recoil and reset occur after firing the gun...

    After reading the docs, this seems to amount to the title of the question; Concave mesh collider with dynamic rigidbody. However, this combination is not allowed...

    Therefore, I am wondering how else I can achieve this result? The only kind of ideas I'm having is to make the barrel a combination of multiple convex meshes. This should achieve the concave nature of the barrel, without sacrificing the other physics properties. If there are any other obvious solutions however, I would be curious to hear them!
     
  2. codebiscuits

    codebiscuits

    Joined:
    Jun 16, 2020
    Posts:
    103
    Can you have a convex barrel, and tell it to ignore collisions with the bullet, either using the collision matrix, or in code?
    Apologies if this is too obvious a solution, or won't work out for what you need, not sure where you're at with physics:)
     
  3. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,532
    Yes, that's the only way if you want to retain the physics properties of the barrel.
     
  4. BenjaminApprill

    BenjaminApprill

    Joined:
    Aug 1, 2022
    Posts:
    132
    I tried that idea... It is still not working. The mesh isn't detecting collisions properly. I've tried all the collision types, but they don't work. I've tried all the combinations of static colliders and dynamic colliders... This really seems like the base physics systems is EXTREMELY archaic in application... I mean, what is it good for at this rate? Only primitive shape collisions? Why have a mesh collider in the first place then.

    This is very disappointing. This kind of thing is why Unity gets a bad reputation. I've stood with Unity for so many years. But when something this simple doesn't work correctly, it is hard to find optimism.

    I only have a few things left to try. Increasing the physics system steps, or whipping up some kind of scripted kinematic/static collider approach... It will be very disappointing if these don't work.
     
  5. Qleenie

    Qleenie

    Joined:
    Jan 27, 2019
    Posts:
    901
    If I understood correctly what you want to achieve, there are two solutions I can think of:

    - use contact modification to allow certain regions of your collider to pass bullets; the API for this is not really intuitive, but it works fine if you understood the principle
    - decompose the object into multiple convex collider. There are assets in the asset store which can do this, e.g. https://assetstore.unity.com/packages/tools/physics/non-convex-mesh-collider-84867#description
     
    Edy likes this.
  6. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,975
    There's very good reasons why dynamic rigidbodies + concave meshes aren't allowed: the combination is extremely brittle and extremely expensive.

    It's not cheap to calculate the intersection of arbitrary, possibly non-convex triangle soups: all individual triangles need to be tested for collision, and calculating a minimum escape vector is pretty much impossible so once an object is inside the collider due to tunneling, it cannot be pushed back out. However, there's a simple solution for convex shapes (the GJK algorithm) that's both robust and performant, so that's why most realtime physics engines only support convex shapes for dynamic objects. Additionally, convex objects work well with continuous collision detection.

    Convex meshes are not primitives. Only analytical shapes like boxes, capsules or spheres are.

    MeshColliders are intended to be used for static level collision shapes. Things like walls, floors, large rocks, etc.

    Tell me you've never used any other engine without telling me you've never used any other engine...;) Unreal doesn't support this, Godot doesn't either, GameMaker nope, can say the same for all engines I've ever worked with or heard about for that matter. Unity's physics engine is Nvidia's PhysX, which is probably the most widely used and battle-tested realtime physics engine in existence, along with Havok and Bullet. Not some janky / haphazardly put together system.

    You'd be hard pressed to find any physics engine out there which supports efficient, robust dynamic concave meshes -if at all- the reason being there's a simpler, cheaper, faster and overall better way to have dynamic concave shapes: multiple convex ones. This is the way most games do it, regardless of the engine they're built upon.
     
    Last edited: Dec 19, 2023
    strangeboltz, Edy and Qleenie like this.
  7. BenjaminApprill

    BenjaminApprill

    Joined:
    Aug 1, 2022
    Posts:
    132
    I've never used any other engine. I've opened Unreal once, and I used GameMaker Studio for like three days... All Unity engine otherwise...

    One thing I've considered is using the Animator for handling specific micro-adjustments of static objects. I will have to test it out some, but it handles complex shifts in object positions without scripts... It might work to fill in the gaps I have at the moment...
     
  8. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,092
    This is actually quite easy, you should not do concave collisions.
    Use Capsule collider for barrel. (There is no cylinder collider unfortunately).
    Barrel is separate rigidbody and mount is saparate, connected with configurablejoint.
    When firing spawn projectile from the end of a barrel, also disable collision between barrel and fired projectile.
     
  9. BenjaminApprill

    BenjaminApprill

    Joined:
    Aug 1, 2022
    Posts:
    132
    I will have to give this a shot... I am messing around with using the Animation of static objects now. But I suppose using a Capsule Collider like that would solve a lot of the problems.