How to use Entity.PhysicsFromMesh?

I’m currently trying to setup the physics on a SENT using PhysicsFromMesh but I don’t have any clue about how this function actually works. I’ve looked at garry’s example mesh maker but that didn’t help a lot either.

This is the code I’m using right now:
[lua]
function ENT:Initialize()
self.Entity:SetModel(“models/dav0r/balloon/balloon.mdl”)
self.Entity:PhysicsInitBox(Vector(-16, -16, -16), Vector( 16, 16, 16))

self.Entity:GetPhysicsObject():SetMass(100)
self.Entity:GetPhysicsObject():Wake()
self.Entity:GetPhysicsObject():EnableGravity( false )

self.Entity:SetColor(math.random(0, 10) * 25.5, math.random(0, 10) * 25.5, math.random(0, 10) * 25.5, 200)

local MeshTable = {}
table.insert(MeshTable, MeshQuad(Vector(0, 0, 0), Vector(0, 0, 10), Vector(0, 10, 10), Vector(0, 0, 10), 0.1, 32))

self.Entity:PhysicsFromMesh(MeshTable)

end
[/lua]

However this crashes GMod as soon as the SENT Initializes. :frown:

Does anyone know how to properly setup a Physics Mesh for a scripted entity?

The physics object is not valid until you create it using PhysicsFromMesh. Move the stuff pertaining to the physics object to after you create the physics object.
Assuming garry isn’t checking if the physics object is null in those methods. Thus, you’re trying to call a function on a null pointer.

Advice, always check the validity of the physics object before using it.

Edit: Didn’t see that PhysicsInitBox. Don’t create a physics object only to create a different one later on. In any case, see below for another probable cause.

[lua]local phys = self:GetPhysicsObject();
if( IsValid( phys ) ) then

end[/lua]

http://luabin.foszor.com/code/lua/includes/extensions/mesh.lua#78
MeshQuad returns a table of vertices, which you’re then inserting into another table. You should be using table.Merge instead.
However, MeshQuad is only for the meshes you generate using BuildFromTriangles. Physics meshes are different entirely, just a table of vectors, every three represent a triangle.

[lua]local MeshTable = { Vector(0, 0, 0), Vector(0, 0, 10), Vector(0, 10, 10) };
self:PhysicsFromMesh( MeshTable );[/lua]

That just uses one triangle, you’d have to define the other half of the quad as well.

I haven’t tried, but if you made a box with this it can’t be turned around either just like with InitBox?

also check the validity of the entity it belongs to.
doing operations on a PhysObj belonging to a non-existent entity will crash the server, even if phys:IsValid() returned true