Hi,
I want to add Bullet Physics library (not a replacement of physx. Just for using some functions ). I added Bullet as plugin(created lib) and it looks ok.
But when i try to add some objects and step world simulation it gives me access violation error.
Bullet uses 16byte memory asignments. Can this be source of my problem ?
My test actor is here
// Fill out your copyright notice in the Description page of Project Settings.
#include "SharpSurgeon.h"
#include "TempActor.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;
}
// Sets default values
ATempActor::ATempActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void ATempActor::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);
//world->addSoftBody(softBody); //cloth
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);
}
// Called every frame
void ATempActor::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
world->stepSimulation(DeltaTime);
for (int i = 0; i<bodies.Num(); i++) {
UE_LOG(LogTemp, Warning, TEXT("BodyCOK %f"), i);
}
}
Best,
Korcan