# Custom 3D collision shape

**URL:** <https://forum.defold.com/t/custom-3d-collision-shape/12505>\
**Category:** Questions\
**Created:** [October 13, 2017, 9:40am UTC](https://forum.defold.com/t/custom-3d-collision-shape/12505 "2017-10-13T09:40:57Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [October 13, 2017, 9:40am UTC](https://forum.defold.com/t/custom-3d-collision-shape/12505/1 "2017-10-13T09:40:57Z")

</div>

Anyone have an idea on how to prepare a custom 3d collision shape? @sven ?

When I try with a custom convexshape I sometimes get

```lua
WARNING:PHYSICS: Overflow in AABB, object removed from simulation
WARNING:PHYSICS: If you can reproduce this, please email bugs@continuousphysics.com

WARNING:PHYSICS: Please include above information, your Platform, version of OS.

WARNING:PHYSICS: Thanks.

```

Here is attempt at making a cube

```lua
shape_type: TYPE_HULL
data: -1
data: -1
data: -1
data: -1
data: -1
data: -1
data: -1
data: 1
data: -1
data: -1
data: 1
data: 1
data: 1
data: -1
data: -1
data: 1
data: -1
data: 1
data: 1
data: 1
data: -1
data: 1
data: 1
data: 1

```

But it looks like this

 ![2017-10-13 02_37_50-Defold Standard Geometry](https://forum-defold-uploads.b-cdn.net/original/2X/e/e23268c5631343027df51f59a5c903fed5bfe6b1.png)

What I’d like is to be able to define .dae files as collision shapes so objects can use their own geometry for collision, or be able to set a simplified version of their .dae for collision, or be able to have/make a tool which can convert .dae files to proper collision shape files.

---

<div class="post-metadata">

**Author:** ![Andreas\_Tadic](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@Andreas\_Tadic](https://forum.defold.com/u/Andreas_Tadic)\
**Post date:** [October 14, 2017, 12:14pm UTC](https://forum.defold.com/t/custom-3d-collision-shape/12505/2 "2017-10-14T12:14:42Z")

</div>

I’ve made a note to investigate what the current status is on this. Will check on Monday.  
If there isn’t already a ticket I’ll make sure to create one and bring it up asap as it’s certainly something we want, especially considering our current take on 3D and performance.

---

<div class="post-metadata">

**Author:** ![Andreas\_Tadic](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@Andreas\_Tadic](https://forum.defold.com/u/Andreas_Tadic)\
**Post date:** [October 16, 2017, 12:34pm UTC](https://forum.defold.com/t/custom-3d-collision-shape/12505/3 "2017-10-16T12:34:40Z")

</div>

Added ticket DEF-2927. I can’t give any ETA at the very moment though.

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [October 16, 2017, 1:27pm UTC](https://forum.defold.com/t/custom-3d-collision-shape/12505/4 "2017-10-16T13:27:18Z")

</div>

Can you post what the version of the bullet physics library being used is?

---

<div class="post-metadata">

**Author:** ![Andreas\_Tadic](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@Andreas\_Tadic](https://forum.defold.com/u/Andreas_Tadic)\
**Post date:** [October 16, 2017, 2:40pm UTC](https://forum.defold.com/t/custom-3d-collision-shape/12505/5 "2017-10-16T14:40:48Z")

</div>

Version 2.77

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [October 16, 2017, 3:00pm UTC](https://forum.defold.com/t/custom-3d-collision-shape/12505/6 "2017-10-16T15:00:35Z")

</div>

Download for anyone interested [https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/bullet/bullet-2.77.zip](https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/bullet/bullet-2.77.zip)

I’ll read the docs for this version.

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [March 22, 2020, 9:42pm UTC](https://forum.defold.com/t/custom-3d-collision-shape/12505/7 "2020-03-22T21:42:48Z")

</div>

Have custom 3D collision shapes been added since these posts?

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [March 22, 2020, 9:47pm UTC](https://forum.defold.com/t/custom-3d-collision-shape/12505/8 "2020-03-22T21:47:59Z")

</div>

Sadly not for meshes. Bullet physics should easily be able to support it too…

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [March 22, 2020, 10:54pm UTC](https://forum.defold.com/t/custom-3d-collision-shape/12505/9 "2020-03-22T22:54:01Z")

</div>

Okay, cheers. What about .convexshape in 3D?

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [March 22, 2020, 11:10pm UTC](https://forum.defold.com/t/custom-3d-collision-shape/12505/10 "2020-03-22T23:10:06Z")

</div>

You could try getting it to work, the original post in this thread was my last attempt at it.

If we could have static meshes for levels/terrain that could be enough for many 3d game use cases.

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [March 23, 2020, 8:45am UTC](https://forum.defold.com/t/custom-3d-collision-shape/12505/11 "2020-03-23T08:45:22Z")

</div>

> [@Pkeod](#):
>
> the original post in this thread was my last attempt at it.

Here is my attempt using @selimanac’s [editor](https://forum.defold.com/t/physics-body-editor-for-defold-polygon-editor/63705) with a slightly angled camera. It almost works, but I think the 0 depth makes it freak out a bit.

3D:

 ![28](https://forum-defold-uploads.b-cdn.net/original/3X/4/c/4cf01c32f6be5f2d59a49703732314166a4f6761.png)

2D:

 ![18](https://forum-defold-uploads.b-cdn.net/original/3X/7/f/7fa008ad76a51cc8d78275c8768bf50a7c973327.png)

---

<div class="post-metadata">

**Author:** ![astrochili](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/astrochili/32/37266_2.png) [@astrochili](https://forum.defold.com/u/astrochili)\
**Post date:** [August 15, 2020, 9:32pm UTC](https://forum.defold.com/t/custom-3d-collision-shape/12505/12 "2020-08-15T21:32:05Z")

</div>

I’ve also tried to use a custom convex shape in 3D. I made a simple pyramid.

> **pyramid.convexshape**
>
> ```
> shape_type: TYPE_HULL
> data: 0
> data: 1
> data: 0
> data: -1
> data: -1
> data: 1
> data: 1
> data: -1
> data: 1
> data: 0
> data: 1
> data: 0
> data: 1
> data: -1
> data: 1
> data: 1
> data: -1
> data: -1
> data: 0
> data: 1
> data: 0
> data: 1
> data: -1
> data: -1
> data: -1
> data: -1
> data: -1
> data: 0
> data: 1
> data: 0
> data: -1
> data: -1
> data: -1
> data: -1
> data: -1
> data: 1
> 
> ```

Sometimes it works, but with strange fractal geometry inside:

[https://forum-defold-uploads.b-cdn.net/original/3X/3/8/3867b09bbfd52345464268431e27a7f0308f162f.mov](https://forum-defold-uploads.b-cdn.net/original/3X/3/8/3867b09bbfd52345464268431e27a7f0308f162f.mov)

Sometimes it fails with the error “`Overflow in AABB, object removed from simulation`” and strange geometry outside:

 ![01](https://forum-defold-uploads.b-cdn.net/original/3X/5/f/5f6792481f2284a7379575603664b5d9153a60e6.png)

It actually looks like there is some [code](https://github.com/defold/defold/blob/168ed4826439e7c6b6299cc14c04891549006b7d/engine/physics/src/physics/physics_3d.cpp#L615) that does the job. If someone knows how to work with bullet physics and convert collada models to convex shapes, it would be great 🙂

The pitfall I found is that it is recommended to reduce the number of vertices. This is written in the [manual](http://www.cs.kent.edu/~ruttan/GameEngines/lectures/Bullet_User_Manual):

 ![09](https://forum-defold-uploads.b-cdn.net/original/3X/f/d/fd5e3cef42430fd6d1e544e7c4b11d77952a97cc.png)

I want to bump up this topic. This is actually one small but strongest step for making 3D games.

---

<div class="post-metadata">

**Author:** ![astrochili](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/astrochili/32/37266_2.png) [@astrochili](https://forum.defold.com/u/astrochili)\
**Post date:** [August 15, 2020, 11:35pm UTC](https://forum.defold.com/t/custom-3d-collision-shape/12505/13 "2020-08-15T23:35:45Z")

</div>

I saw some pattern in this bug.

After creating the necessary and correct vertices, the unnecessary glitch vertices are created in a random progression. If the progression generated vertexes into the object, then physics works fine and doesn’t need to process glitch vertexes because they are inside the object. If the progression goes beyond the object, then glitch vertices can be very far from the object and physics can’t calculate so huge collisions and turned off the object with the overflow error.

May be the random factor is a bad memory access or a floating point stuff? For example, it can be out of bounds in `vertices[i]` that gets an undefined behavior with random vertex data.

We pass `&convex_shape->m_Data[0]` to `NewConvexHullShape3D` that waiting for an array of floats , is it correct? May be we need to pass just `convex_shape->m_Data`?

A switch with some [2D convex shape preparing](https://github.com/defold/defold/blob/869a87718757bdb7bfd6ed078047fa7bfb451de9/engine/gamesys/src/gamesys/resources/res_convex_shape.cpp#L94):

```
if (context->m_3D)
    resource->m_Shape3D = dmPhysics::NewConvexHullShape3D(context->m_Context3D, &convex_shape->m_Data[0], convex_shape->m_Data.m_Count / 3);
else
{
    const uint32_t data_size = 2 * convex_shape->m_Data.m_Count / 3;
    float* data_2d = new float[2 * convex_shape->m_Data.m_Count / 3];
    for (uint32_t i = 0; i < data_size; ++i)
    {
        data_2d[i] = convex_shape->m_Data[i/2*3 + i%2];
    }
    resource->m_Shape2D = dmPhysics::NewPolygonShape2D(context->m_Context2D, data_2d, data_size/2);
    delete [] data_2d;
}

```

[3D shape creating](https://github.com/defold/defold/blob/168ed4826439e7c6b6299cc14c04891549006b7d/engine/physics/src/physics/physics_3d.cpp#L615):

```
HCollisionShape3D NewConvexHullShape3D(HContext3D context, const float* vertices, uint32_t vertex_count)
{
    assert(sizeof(btScalar) == sizeof(float));
    float scale = context->m_Scale;
    const uint32_t elem_count = vertex_count * 3;
    float* v = new float[elem_count];
    for (uint32_t i = 0; i < elem_count; ++i)
    {
        v[i] = vertices[i] * scale;
    }
    btConvexHullShape* hull = new btConvexHullShape(v, vertex_count, sizeof(float) * 3);
    delete [] v;
    return hull;
}

```

[2D shape creating](https://github.com/defold/defold/blob/a0c1b01f6d18a11e2d3c6a49c79b1b96baf7cffb/engine/physics/src/physics/physics_2d.cpp#L575):

```
HCollisionShape2D NewPolygonShape2D(HContext2D context, const float* vertices, uint32_t vertex_count)
{
    b2PolygonShape* shape = new b2PolygonShape();
    float scale = context->m_Scale;
    const uint32_t elem_count = vertex_count * 2;
    float* v = new float[elem_count];
    for (uint32_t i = 0; i < elem_count; ++i)
    {
        v[i] = vertices[i] * scale;
    }
    shape->Set((b2Vec2*)v, vertex_count);
    delete [] v;
    return shape;
}

```

I also found an interesting [recommendations post](https://www.gamedev.net/forums/topic/691208-build-a-convex-hull-from-a-given-mesh-in-bullet/5351743/) for working with btConvexHullShape:

- Use `.setMargin(0)` to remove the bug with extra padding.
- Use `btShapeHull()` to reduce the polygon count in btConvexHullShape.

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [August 16, 2020, 6:29am UTC](https://forum.defold.com/t/custom-3d-collision-shape/12505/14 "2020-08-16T06:29:17Z")

</div>

I wonder how painful it would be to update the bullet version. The current version is 2.77.

> **[Release Announcements - Real-Time Physics Simulation Forum](https://pybullet.org/Bullet/phpBB3/viewforum.php?f=18)**

Any of the 2 version will probably not be too bad and one of them might fix the issue among potential others.

One feature we need is to be able to use meshes as static physics objects. If we had that I could make most of the 3D games I want to make with Defold.

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [August 18, 2020, 6:42am UTC](https://forum.defold.com/t/custom-3d-collision-shape/12505/15 "2020-08-18T06:42:25Z")

</div>

Probably not very painful.  
Unpack the new package, and apply the patches we’ve made. Then rebuild the packages for each of the platforms/architectures.

The code is here: [defold/external/bullet3d at dev · defold/defold · GitHub](https://github.com/defold/defold/tree/dev/external/bullet3d)

For collision meshes, we have ticket [#3519](https://github.com/defold/defold/issues/3519) for this.

---

<div class="post-metadata">

**Author:** ![aglitchman](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/aglitchman/32/19432_2.png) [@aglitchman](https://forum.defold.com/u/aglitchman)\
**Post date:** [May 16, 2021, 9:43pm UTC](https://forum.defold.com/t/custom-3d-collision-shape/12505/16 "2021-05-16T21:43:17Z")

</div>

> [@astrochili](#):
>
> May be the random factor is a bad memory access or a floating point stuff? For example, it can be out of bounds in `vertices[i]` that gets an undefined behavior with random vertex data.

I managed to fix this issue, take a look:  
[https://github.com/defold/defold/pull/5817](https://github.com/defold/defold/pull/5817)

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [May 17, 2021, 8:49am UTC](https://forum.defold.com/t/custom-3d-collision-shape/12505/17 "2021-05-17T08:49:06Z")

</div>

This is great! For 2D .convexshapes PhysicsEditor works well. Is there a similar tool that can be used to generate 3D .convexshapes?

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [May 17, 2021, 8:53am UTC](https://forum.defold.com/t/custom-3d-collision-shape/12505/18 "2021-05-17T08:53:11Z")

</div>

Not sure there are.  
I’d recommend using Blender and having a script to output the vertices into a .convexshape file.

E.g. see `defold_mesh.py` in this example: [Mesh Component](https://forum.defold.com/t/mesh-component/65137)

---

<div class="post-metadata">

**Author:** ![totebo](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/totebo/32/36087_2.png) [@totebo](https://forum.defold.com/u/totebo)\
**Post date:** [May 17, 2021, 8:58am UTC](https://forum.defold.com/t/custom-3d-collision-shape/12505/19 "2021-05-17T08:58:01Z")

</div>

Regrettably defold\_mesh.py looks like “real code”, and therefore borderline impenetrable to me. 🙂 I was wondering how @aglitchman produced this one?

 ![118413451-118ac480-b6a8-11eb-9af8-cb08caf8d87a](https://forum-defold-uploads.b-cdn.net/original/3X/4/3/43205f4ed3a4d9ed5960286e792f6bccd2d18d9e.png)

---

<div class="post-metadata">

**Author:** ![aglitchman](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/aglitchman/32/19432_2.png) [@aglitchman](https://forum.defold.com/u/aglitchman)\
**Post date:** [May 17, 2021, 10:54am UTC](https://forum.defold.com/t/custom-3d-collision-shape/12505/20 "2021-05-17T10:54:51Z")

</div>

> [@totebo](#):
>
> Regrettably defold\_mesh.py looks like “real code”, and therefore borderline impenetrable to me. 🙂 I was wondering how @aglitchman produced this one?

The green lines are the physics debug view of a convex shape. I produced it using the [Convex Hull](https://docs.blender.org/manual/en/latest/modeling/meshes/editing/mesh/convex_hull.html) tool in Blender, then I made the `.convexshape` file by hands. Yes, this process can be automated with Python scripts!

[Next page](https://forum.defold.com/t/custom-3d-collision-shape/12505.md?page=2)
