PhysicsFromMesh, dynamic props? and teraforming in gmod?

Alright now that ive got your attention from the flashy thread name listen up.
Haven’t you ever thought: man gmod would sure be awesome if you could have true tera-forming or props that could dynamically change with lua…

Well over the past few days i started a short project to see if it was possible to create a tera-forming system in gmod using a dynamic mesh that would represent the terain. after a few long nights and alot of brain f**ks this is what i have as a proof of concept :

http://img402.imageshack.us/img402/835/meshworld.png

it looks perdy and all but while trying to make the collision for this mesh ive run into a problem.
it appears the PhysicsFromMesh is not creating a collision properly. when you make the physics from a mesh props collide with it perfectly, but players do not.
Players are pushed out of the mesh area just like when you create a physics from a box and don’t set collision bounds.

This is where i am stuck. I am not a c++ coder so i cant look at the source code and understand whats going on with it, but if any of you have any idea what could possible be wrong speak up so we can fix this problem and create jaw dropping things in gmod.

This is the physicsfrommesh source code Garry is using for it :



    LC_Function( Entity, PhysicsFromMesh )
    {
    	CBaseEntity* self = Get_Entity( 1 );

    	ILuaObject* pVertTable = g_Lua->GetObject( 2 );
    	if (!pVertTable || !pVertTable->isTable())
    	{
    		Msg("PhysicsFromMesh must be passed a mesh!
");
    		return 0;
    	}

    	Vector vMin(0,0,0), vMax(0,0,0);

    	int iMatIndex = 0;

    	if ( self->VPhysicsGetObject() )
    	{
    		iMatIndex = self->VPhysicsGetObject()->GetMaterialIndex();
    	}

    	CPhysPolysoup *pPolySoup = physcollision->PolysoupCreate();

    	Vector vVector[3];
    	int i=0;
    	while ( true )
    	{
    		i++;
    		ILuaObject* pVert = pVertTable->GetMember( i );
    		if (!pVert) break;

    		int index  = i - 1;
    		vVector[ index % 3 ] = *((Vector*)pVert->GetMemberUserData( "pos", (void*)&vec3_origin ));

    		vMin.x = min( vMin.x, vVector[ index % 3 ].x );
    		vMin.y = min( vMin.y, vVector[ index % 3 ].y );
    		vMin.z = min( vMin.z, vVector[ index % 3 ].z );

    		vMax.x = max( vMax.x, vVector[ index % 3 ].x );
    		vMax.y = max( vMax.y, vVector[ index % 3 ].y );
    		vMax.z = max( vMax.z, vVector[ index % 3 ].z );

    		if ( (index%3) == 2 )
    		{
    			physcollision->PolysoupAddTriangle( pPolySoup, vVector[0], vVector[1], vVector[2], iMatIndex );
    		}

    	}

    	CPhysCollide *pCollide = physcollision->ConvertPolysoupToCollide( pPolySoup, false );
    	physcollision->PolysoupDestroy( pPolySoup );

    	solid_t tmpSolid;
    	PhysModelParseSolid( tmpSolid, self, self->GetModelIndex() );

    	IPhysicsObject* pPhysObj = PhysModelCreateCustom( self, pCollide, self->GetAbsOrigin(), self->GetAbsAngles(), "ModelName", false, &tmpSolid );
    	pPhysObj->SetMass( 500 );

    	self->VPhysicsDestroyObject();
    	self->VPhysicsSetObject( pPhysObj );

    	self->SetCollisionBounds( vMin, vMax );
    	self->SetMoveType( MOVETYPE_VPHYSICS );
    	self->SetSolid( SOLID_VPHYSICS );

    	return 0;

    }



-------- new stuff vvvvvvv 3/16/2011 -----------------
Figured i would add a bit more to the original post explaining my mesh and what I’m working on at the moment.
Il put predy pictures in with text underneath them explaining what is going on, here we go :

http://img715.imageshack.us/img715/6002/gmflatgrass0001i.jpg

this is how each box of my mesh is made up, each part is 4 triangles that connect to 1 center point.
the outer green dots are the part that the player will be able to interact with and be able to move up or down.
the middle blue dot will always be lower then the lowest green dot, you’ll understand later why i do this but for now that’s how each box is setup.

http://img96.imageshack.us/img96/9175/gmflatgrass0002li.jpg

here you can see 4 boxes connected next to one another giving you a better look at whats going on.

http://img535.imageshack.us/img535/6733/gmflatgrass0000iq.jpg

this is my current work around for physicsfrommesh. its a bit more resource demanding and would only work if created on the fly where the player is.
the idea is that you create 4 entity that have physics from PhysicsInitBox and aim them at the center point, this will let your player stand on the mesh when done right.
since the middel point will always be lower then the lowest green point, the boxes should not mess up much.

the only slight problem you would have with doing this is when 1 corner of the mesh is on an extreme angle as seen below:

http://img856.imageshack.us/img856/3405/gmflatgrass0003.jpg

as you can see in the picture, when 1 point is alot higher then the other ones, the corners of your physicsinitbox will start to bleed through a little
you would have to test this to see how big an effect it has on game play, but to me it doesn’t seem like it would be that bad as the players would be dazed by the tera-forming and not notice.

Well that’s what i have for now hope this explain a bit more on how im trying to doing this.

Please post your reactions, ideas, or questions below.

I don’t see it set the collision group anywhere

Quick question that’s a brush displacement your terra forming correct?

If so you might be out of luck if its based on the env_terrainmorph entity

According to the VDC the collision hull data isn’t updated real time, which I’m guessing means you can’t actually get it to work without getting at the source code itself.

I’m using Mesh.BuildFromTriangles to create the ‘terrain’ you see in that picture, my knowledge base is pretty small, i didn’t even know there was such a thing as a “env_terrainmorph”

Edit: i did a test a few days ago with this, and I’m able to change the way the mesh looks in real time, along with changing the angle of the normal on the mesh witch could be used to mimic a sun going up and down on the world.

So is this an entity or does it return an entity? could you set the collision group after you create it?

At the moment ive been testing the physicsfrommesh on a small scale, a simple box.
I threw it into an addon so i could mess arround with it in sandbox, i uploaded it here if you want to see how i have it setup.
http://www.megaupload.com/?d=NS22TDEY

it reacts with props fine, but as soon as a player touches it, the entity just sends you flying.

Yea I’ll go ahead and take a look at that, I’m making a meteor game mode and it would be beyond awesome if I could leave craters, but only if players can walk on them. So I am very much for fix this too.

alright sounds good, pm your steam name if you want to chat about this stuff on there.

Sorry, PhysicsFromMesh has always been broken like that, and Garry isn’t even going to bother fixing it.

Well the c++ code is posted there in hopes that someone might be able to figure out what is wrong with it.
In the mean time, i have a few other methods that are more reasorce demanding im goning to try.

Post on the bug tracker if it needs fixing? Can’t hurt.

updated the original post to show my current ideas on how to go about making a world that you could terraform in gmod