Memory Problem on Soft Bodies

Post Reply
Korcan
Posts: 7
Joined: Mon Sep 21, 2015 10:26 am

Memory Problem on Soft Bodies

Post by Korcan »

Hi,

I am adding Bullet in Unreal Engine 4 as a plugin (Not a replacement of PhysX). Everything looks well except soft bodies. When i created softbodies im getting memory read errors.

Do you have any idea why this happens ? Example code is below.

Code: Select all


// Fill out your copyright notice in the Description page of Project Settings.

#include "SharpSurgeon.h"
#include "SharpSurgeonGameMode.h"

#include "btBulletDynamicsCommon.h"
#include "BulletSoftBody/btSoftRigidDynamicsWorld.h"
#include "BulletSoftBody/btDefaultSoftBodySolver.h"
#include "BulletSoftBody/btSoftBodyHelpers.h"
#include "BulletSoftBody/btSoftBodyRigidBodyCollisionConfiguration.h"

struct bulletObject {
	int id;
	float r, g, b;
	bool hit;
	btRigidBody* body;
	bulletObject(btRigidBody* b, int i, float r0, float g0, float b0) : body(b), id(i), r(r0), g(g0), b(b0), hit(false) {}
};


btSoftRigidDynamicsWorld* world;
btDispatcher* dispatcher;
btCollisionConfiguration* collisionConfig;
btBroadphaseInterface* broadphase;
btConstraintSolver* solver;
btSoftBodySolver* softbodySolver;
TArray<bulletObject*> bodies;


btRigidBody* addSphere(float rad, float x, float y, float z, float mass)
{
	btTransform t;
	t.setIdentity();
	t.setOrigin(btVector3(x, y, z));
	btSphereShape* sphere = new btSphereShape(rad);
	btVector3 inertia(0, 0, 0);
	if (mass != 0.0)
		sphere->calculateLocalInertia(mass, inertia);

	btMotionState* motion = new btDefaultMotionState(t);
	btRigidBody::btRigidBodyConstructionInfo info(mass, motion, sphere, inertia);
	btRigidBody* body = new btRigidBody(info);
	world->addRigidBody(body);
	bodies.Add(new bulletObject(body, 0, 1.0, 0.0, 0.0));
	body->setUserPointer(bodies[bodies.Num() - 1]);
	return body;
}

btRigidBody* addCylinder(float d, float h, float x, float y, float z, float mass)
{
	btTransform t;
	t.setIdentity();
	t.setOrigin(btVector3(x, y, z));
	btCylinderShape* sphere = new btCylinderShape(btVector3(d / 2.0, h / 2.0, d / 2.0));
	btVector3 inertia(0, 0, 0);
	if (mass != 0.0)
		sphere->calculateLocalInertia(mass, inertia);

	btMotionState* motion = new btDefaultMotionState(t);
	btRigidBody::btRigidBodyConstructionInfo info(mass, motion, sphere, inertia);
	btRigidBody* body = new btRigidBody(info);
	world->addRigidBody(body);
	bodies.Add(new bulletObject(body, 1, 0.0, 1.0, 0.0));
	body->setUserPointer(bodies[bodies.Num() - 1]);
	return body;
}


btRigidBody* addCone(float d, float h, float x, float y, float z, float mass)
{
	btTransform t;
	t.setIdentity();
	t.setOrigin(btVector3(x, y, z));
	btConeShape* sphere = new btConeShape(d, h);
	btVector3 inertia(0, 0, 0);
	if (mass != 0.0)
		sphere->calculateLocalInertia(mass, inertia);

	btMotionState* motion = new btDefaultMotionState(t);
	btRigidBody::btRigidBodyConstructionInfo info(mass, motion, sphere, inertia);
	btRigidBody* body = new btRigidBody(info);
	world->addRigidBody(body);
	bodies.Add(new bulletObject(body, 2, 1.0, 0.0, 1.0));
	body->setUserPointer(bodies[bodies.Num() - 1]);
	return body;
}


btRigidBody* addBox(float width, float height, float depth, float x, float y, float z, float mass)
{
	btTransform t;
	t.setIdentity();
	t.setOrigin(btVector3(x, y, z));
	btBoxShape* sphere = new btBoxShape(btVector3(width / 2.0, height / 2.0, depth / 2.0));
	btVector3 inertia(0, 0, 0);
	if (mass != 0.0)
		sphere->calculateLocalInertia(mass, inertia);

	btMotionState* motion = new btDefaultMotionState(t);
	btRigidBody::btRigidBodyConstructionInfo info(mass, motion, sphere, inertia);
	btRigidBody* body = new btRigidBody(info);
	world->addRigidBody(body);
	bodies.Add(new bulletObject(body, 3, 1.0, 1.0, 0.0));
	body->setUserPointer(bodies[bodies.Num() - 1]);
	return body;
}

void ASharpSurgeonGameMode::BeginPlay()
{
	Super::BeginPlay();

	collisionConfig = new btSoftBodyRigidBodyCollisionConfiguration();
	dispatcher = new btCollisionDispatcher(collisionConfig);
	broadphase = new btDbvtBroadphase();
	solver = new btSequentialImpulseConstraintSolver();
	softbodySolver = new btDefaultSoftBodySolver();
	world = new btSoftRigidDynamicsWorld(dispatcher, broadphase, solver, collisionConfig, softbodySolver);
	world->setGravity(btVector3(0, -10, 0));

	btTransform t;
	t.setIdentity();
	t.setOrigin(btVector3(0, 0, 0));
	btStaticPlaneShape* plane = new btStaticPlaneShape(btVector3(0, 1, 0), 0);
	btMotionState* motion = new btDefaultMotionState(t);
	btRigidBody::btRigidBodyConstructionInfo info(0.0, motion, plane);
	btRigidBody* body = new btRigidBody(info);
	world->addRigidBody(body);
	bodies.Add(new bulletObject(body, 4, 0.8, 0.8, 0.8));
	body->setUserPointer(bodies[bodies.Num() - 1]);

	addSphere(1.0, 0, 20, 0, 1.0);
	float s = 4;
	float h = 20;

	/*
	btSoftBody* softBody = btSoftBodyHelpers::CreatePatch(
		world->getWorldInfo(), btVector3(-s, h, -s), btVector3(s, h, -s),
		btVector3(-s, h, s), btVector3(s, h, s), 50, 50, 4 + 8, true);
	softBody->m_cfg.viterations = 50;
	softBody->m_cfg.piterations = 50;
	softBody->setTotalMass(3.0);
	softBody->setMass(100, 100);

	softBody = btSoftBodyHelpers::CreateEllipsoid(world->getWorldInfo(),
		btVector3(10, 10, 10), btVector3(2, 2, 2), 1000);
	softBody->m_cfg.viterations = 50;
	softBody->m_cfg.piterations = 50;
	softBody->m_cfg.kPR = 1000;
	softBody->setTotalMass(3.0);
	softBody->setMass(0, 0);
	world->addSoftBody(softBody);
	*/

	addCylinder(2, 5, 0, 30, 0, 1.0);
	addCone(2, 5, 5, 30, 0, 1.0);
	addBox(10, 2, 3, 0, 40, 0, 1.0);

}

void ASharpSurgeonGameMode::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	world->stepSimulation(DeltaTime);
}

Best,
Korcan
Xcoder79
Posts: 42
Joined: Sun Aug 07, 2011 5:27 am

Re: Memory Problem on Soft Bodies

Post by Xcoder79 »

Hi,

Have you tried changing, softBody->m_cfg.viterations = 50;
softBody->m_cfg.piterations = 50;
To a lower value why so high?
Also cfg.kPR Softbody Pressure is not very stable.
Post Reply