Drawing soft bodies !

OSasuke
Posts: 47
Joined: Tue Dec 09, 2008 10:12 am

Drawing soft bodies !

Post by OSasuke »

Hi every one.

I started to integrate soft body to my application. The first problem that a had is the collision between soft and rigid boies. Now i have a problem with drawing soft bodies. What is the steps to draw soft bodies. I am working with Irrlicht.

Code: Select all

#include <iostream>
#include <cstdlib>
#include <wchar.h>
#include <irrlicht.h>
#include <btBulletDynamicsCommon.h>
#include <Bullet-C-Api.h>
#include <btBulletCollisionCommon.h>
#include <BulletSoftBody/btSoftBody.h>
#include <BulletSoftBody/btSoftRigidDynamicsWorld.h>
#include <BulletSoftBody/btSoftBodyRigidBodyCollisionConfiguration.h>
#include <BulletSoftBody/btSoftBodyHelpers.h>

#include "BtBox.h"

using namespace std;

using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;


int main(int argc, char** argv)
{
    IrrlichtDevice *device =createDevice(EDT_DIRECT3D9, dimension2d<u32>(512, 384), 16);
    device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo");
    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();

    btSoftBodyWorldInfo	softBodyWI;

    btVector3 worldAabbMin(-1000,-1000,-1000);
    btVector3 worldAabbMax(1000,1000,1000);
    int maxProxies = 1024;
    btAxisSweep3* broadphase = new btAxisSweep3(worldAabbMin,worldAabbMax,maxProxies);
    softBodyWI.m_broadphase = broadphase;

    btDefaultCollisionConfiguration* collisionConfiguration = new btSoftBodyRigidBodyCollisionConfiguration();
    btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
    softBodyWI.m_dispatcher = dispatcher;

    btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;

    btSoftRigidDynamicsWorld* world = new btSoftRigidDynamicsWorld(dispatcher,broadphase,solver,collisionConfiguration);
    world->setGravity(btVector3(0,-10,0));
    softBodyWI.m_sparsesdf.Initialize();
    softBodyWI.m_gravity.setValue(0,-10.0,0);
    softBodyWI.air_density      =   (btScalar)1.2;
    softBodyWI.water_density   =   0;
    softBodyWI.water_offset   =   0;
    softBodyWI.water_normal   =   btVector3(0,0,0);

    BtBox ground(btVector3(75,10,75),"wall.jpg");
          ground.irrInit(smgr,driver);
          ground.btInit(world,0,btVector3(0,0,0));
          ground.setPosition(0,-10,0);

    IMesh* smesh = smgr->getGeometryCreator()->createSphereMesh(8,16,16);
    IMeshSceneNode* snode = smgr->addMeshSceneNode( smesh );
    //snode->setMaterialFlag(EMF_WIREFRAME, true);

    snode->setMaterialTexture(0,driver->getTexture("water.jpg"));

    IMeshBuffer *mb = smesh->getMeshBuffer(0);

    video::S3DVertex* mb_vertices = (irr::video::S3DVertex*)mb->getVertices();
    btScalar* vertices = new btScalar[mb->getVertexCount()*3];
    for(int i=0; i<mb->getVertexCount(); i++)
    {
        vertices[i*3+0] = mb_vertices[i].Pos.X;
        vertices[i*3+1] = mb_vertices[i].Pos.Y;
        vertices[i*3+2] = mb_vertices[i].Pos.Z;
    }

    u16* mb_indices = mb->getIndices();
    int* indices = new int[mb->getIndexCount()];
    for(int i=0; i<mb->getIndexCount(); i++)
    {
        indices[i] = mb_indices[i];
    }

    btSoftBody*	psb=btSoftBodyHelpers::CreateFromTriMesh(softBodyWI,&vertices[0],
                                                    indices,mb->getVertexCount()/3);
    psb->m_materials[0]->m_kLST	=	0.1;
	psb->m_cfg.kDF				=	1;
	psb->m_cfg.kDP				=	0.001; // fun factor...
	psb->m_cfg.kPR				=	2500;
 	world->addSoftBody(psb);
	psb->translate(btVector3(0,20,0));

    ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS(0,20,0.03);
	cam->setPosition(vector3df(0,25,-45));
	cam->setTarget(vector3df(0,0,0));

    scene::ILightSceneNode* light1 =  smgr->addLightSceneNode();//, core::vector3df(0,0,0),

    SLight sl;
    sl.Type = ELT_DIRECTIONAL;
    sl.Position = vector3df(200,200,200);
    sl.Direction = vector3df(-1,-1,-1);
    sl.Attenuation = vector3df(2.0f,1.0f,2.0f);
    sl.AmbientColor = SColorf(0.15,0.15,0.15);
    sl.DiffuseColor = SColorf(0.6,0.6,0.6);
    sl.SpecularColor = SColorf(0.9,0.9,0.9);

    light1->setLightData(sl);

	while(device->run())
    {
        world->stepSimulation(1.0f/60.f,0);
        driver->beginScene(true, true, SColor(255,100,101,140));

        // Updating vertices of the soft body's irrlicht node
        for(int i=0; i<mb->getVertexCount(); i++)
        {
            mb_vertices[i].Pos.X = psb->m_nodes[i].m_x.x();
            mb_vertices[i].Pos.Y = psb->m_nodes[i].m_x.y();
            mb_vertices[i].Pos.Z = psb->m_nodes[i].m_x.z();
        }

        ground.update();
        smgr->drawAll();
        driver->endScene();
    }

    device->drop();

    delete vertices;
    delete indices;

    delete world;
    delete solver;
    delete dispatcher;
    delete collisionConfiguration;
    delete broadphase;

    return 0;

}
OSasuke
Posts: 47
Joined: Tue Dec 09, 2008 10:12 am

Re: Drawing soft bodies !

Post by OSasuke »

What represente nodes. Are they vertex. :?: :?: