Ogre + Bullet for Android Studio

Problems building or running the engine, queries about how to use features etc.
Post Reply
kmw4988
Gnoblar
Posts: 2
Joined: Wed Apr 10, 2019 10:41 am

Ogre + Bullet for Android Studio

Post by kmw4988 »

Ogre Version: : 1.11.5
Operating System: : MacOS Mojave 10.14.4
Render System: : in Android Studio

Hi, I need help from ogre master.
I'm poor in English. Please understand.

First, I was able to compile and build samplebrowserNDK. I attached the bullet engine to the samplebrowserNDK and wrote the following code.
But, unlike my expectations, nothing is happening, only the ogre head and the floor. How do you get gravity to fall down?

Code: Select all

#ifndef __BulletTest_H__
#define __BulletTest_H__

#include "SdkSample.h"
#include "OgreSceneNode.h"

#include "btBulletDynamicsCommon.h"

#include "LinearMath/btVector3.h"
#include "LinearMath/btAlignedObjectArray.h"
#include "BulletDynamics/Dynamics/btDynamicsWorld.h"
#include "BulletDynamics/Dynamics/btRigidBody.h"

#include "BtOgrePG.h"
#include "BtOgreGP.h"
#include "BtOgreExtras.h"

using namespace Ogre;
using namespace OgreBites;

class _OgreSampleClassExport Sample_BulletTest : public SdkSample
{
public:

    Sample_BulletTest()
    {
        mInfo["Title"] = "Bullet Test";
        mInfo["Description"] = "Shows Bullet Gravity Test (fixed-distance domes used for backgrounds).";
        mInfo["Thumbnail"] = "thumb_cubic.png";
        mInfo["Category"] = "Environment";
    }
    
protected:
    
    void setupContent()
    {
        // setup some basic lighting for our scene
        mSceneMgr->setAmbientLight(ColourValue(0.7, 0.7, 0.7));
        mSceneMgr->getRootSceneNode()
        ->createChildSceneNode(Vector3(20, 80, 50))
        ->attachObject(mSceneMgr->createLight());
        
        // set our camera to orbit around the origin and show cursor
        mCameraMan->setStyle(CS_ORBIT);
        mCameraMan->setYawPitchDist(Degree(0), Degree(0), 250);
        mTrayMgr->showCursor();

        ///-----initialization_start-----
        
        ///collision configuration contains default setup for memory, collision setup. Advanced users can create their own configuration.
        btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
        
        ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
        btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
        
        ///btDbvtBroadphase is a good general purpose broadphase. You can also try out btAxis3Sweep.
        btBroadphaseInterface* overlappingPairCache = new btDbvtBroadphase();
        
        ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
        btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
        
        btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration);
        
        dynamicsWorld->setGravity(btVector3(0, -10, 0));
        
        {
            
            // create a floor mesh resource
            MeshManager::getSingleton().createPlane("floor", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
                                                    Plane(Vector3::UNIT_Y, -50), 200, 200, 10, 10, true, 1, 8, 8, Vector3::UNIT_Z);
            
            // create a floor entity, give it a material, and place it at the origin
            Entity* mGroundEntity;
            btRigidBody *mGroundBody;
            btBvhTriangleMeshShape *mGroundShape;
            
            mGroundEntity = mSceneMgr->createEntity("Floor", "floor");
            mGroundEntity->setMaterialName("Examples/BumpyMetal");
            mSceneMgr->getRootSceneNode()->createChildSceneNode("groudNode")->attachObject(mGroundEntity);
            
            BtOgre::StaticMeshToShapeConverter converter2(mGroundEntity);
            mGroundShape = converter2.createTrimesh();
            
            //using motionstate is optional, it provides interpolation capabilities, and only synchronizes 'active' objects
            btDefaultMotionState* mGroundState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,0,0)));
            
            //Create the Body.
            mGroundBody = new btRigidBody(0, mGroundState, mGroundShape, btVector3(0,0,0));
            dynamicsWorld->addRigidBody(mGroundBody);
        }
        
        {
            Vector3 pos = Vector3(0,50,0);
            Quaternion rot = Quaternion::IDENTITY;
            
            SceneNode *mOgreNode;
            Entity *mOgreEntity;
            btRigidBody *mOgreBody;
            btCollisionShape *mOgreShape;

            mOgreEntity = mSceneMgr->createEntity("Head", "ogrehead.mesh");
            mOgreNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("OgreSceneNode", pos, rot);
            mOgreNode->attachObject(mOgreEntity);

            //Create shape.
            BtOgre::StaticMeshToShapeConverter converter(mOgreEntity);
            mOgreShape = converter.createSphere();
            
            //Calculate inertia.
            btScalar mass = 5;
            btVector3 inertia;
            mOgreShape->calculateLocalInertia(mass, inertia);
            
            //Create BtOgre MotionState (connects Ogre and Bullet).
            BtOgre::RigidBodyState *mOgreState = new BtOgre::RigidBodyState(mOgreNode);
            
            //Create the Body.
            mOgreBody = new btRigidBody(mass, mOgreState, mOgreShape, inertia);
            dynamicsWorld->addRigidBody(mOgreBody);
        }
    }
};

#endif
Owl53
Halfling
Posts: 92
Joined: Sat Jul 22, 2017 2:32 pm
x 4

Re: Ogre + Bullet for Android Studio

Post by Owl53 »

Are you calling dynamicsWorld->stepSimulation anywhere else in your code?
kmw4988
Gnoblar
Posts: 2
Joined: Wed Apr 10, 2019 10:41 am

Re: Ogre + Bullet for Android Studio

Post by kmw4988 »

Owl53 wrote: Mon May 20, 2019 11:56 am Are you calling dynamicsWorld->stepSimulation anywhere else in your code?
Hi, thanks.
With your help, the RigidBody solved.
Next, I'm trying to test on a SoftBody.
When I create an APK and run it on Android phone, the app dies like this:
Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 19599 (rg.ogre.browser)

I think, the problem is in the following code section.

Code: Select all

getSoftDynamicsWorld()->addSoftBody(psb);

Is there a solution?

I also attached a full source.

Code: Select all

/*
 -----------------------------------------------------------------------------
 This source file is part of OGRE
 (Object-oriented Graphics Rendering Engine)
 For the latest info, see http://www.ogre3d.org/
 Copyright (c) 2000-2013 Torus Knot Software Ltd
 Also see acknowledgements in Readme.html
 You may use this sample code for anything you like, it is not covered by the
 same license as the rest of the engine.
 -----------------------------------------------------------------------------
 */

#ifndef __BulletTest_H__
#define __BulletTest_H__

#include "SdkSample.h"
#include "OgreApplicationContext.h"

#include "btBulletDynamicsCommon.h"

#include "LinearMath/btVector3.h"
#include "LinearMath/btConvexHull.h"
#include "LinearMath/btScalar.h"
#include "LinearMath/btQuaternion.h"
#include "LinearMath/btMatrix3x3.h"

#include "BulletDynamics/Dynamics/btDynamicsWorld.h"
#include "BulletDynamics/Dynamics/btRigidBody.h"
#include "BulletDynamics/Featherstone/btMultiBody.h"

#include "BulletSoftBody/btSoftBody.h"
#include "BulletSoftBody/btSoftBodyRigidBodyCollisionConfiguration.h"
#include "BulletSoftBody/btSoftBodyHelpers.h"
#include "BulletSoftBody/btSoftRigidDynamicsWorld.h"

#include "BtOgrePG.h"
#include "BtOgreGP.h"
#include "BtOgreExtras.h"
#include "BtOgreSoftBody.h"
#include "MeshUtils.h"

using namespace Ogre;
using namespace OgreBites;

class _OgreSampleClassExport Sample_BulletTest : public SdkSample, public ApplicationContext
{
public:
    
    btDefaultCollisionConfiguration* m_collisionConfiguration = new btSoftBodyRigidBodyCollisionConfiguration();
    btCollisionDispatcher* m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
    btBroadphaseInterface* m_overlappingPairCache = new btDbvtBroadphase();
    btSequentialImpulseConstraintSolver* m_solver = new btSequentialImpulseConstraintSolver;
    
    btDiscreteDynamicsWorld* m_dynamicsWorld = new btSoftRigidDynamicsWorld(m_dispatcher, m_overlappingPairCache, m_solver, m_collisionConfiguration);
    
    btDynamicsWorld *phyWorld;
    btSoftBodyWorldInfo m_softBodyWorldInfo;
    BtOgre::BtOgreSoftBody *softBody;
    
    Ogre::Entity *mGroundEntity;
    btRigidBody *mGroundBody;
    btCollisionShape *mGroundShape;
    
    Sample_BulletHair()
    {
        mInfo["Title"] = "Bullet Test";
        mInfo["Description"] = "Shows Bullet Test";
        mInfo["Thumbnail"] = "thumb_test.png";
        mInfo["Category"] = "Environment";
    }
    
    bool frameStarted(const FrameEvent &evt)
    {
        ApplicationContext::frameStarted(evt);

        phyWorld->stepSimulation(evt.timeSinceLastFrame, 10);
        
        softBody->updateOgreMesh();
        
        return true;
    }
    
    btSoftRigidDynamicsWorld* getSoftDynamicsWorld()
    {
        
        return (btSoftRigidDynamicsWorld*) phyWorld;
    }
    
protected:
    
    void setupContent()
    {
        mSceneMgr->setAmbientLight(ColourValue(0.7, 0.7, 0.7));
        mSceneMgr->getRootSceneNode()
        ->createChildSceneNode(Vector3(20, 80, 50))
        ->attachObject(mSceneMgr->createLight());

        Light* spotLight = mSceneMgr->createLight("spotLight");
        spotLight->setType(Ogre::Light::LT_SPOTLIGHT);
        spotLight->setDirection(0, 0, -1);
        spotLight->setDiffuseColour(Ogre::ColourValue::White);
        spotLight->setSpecularColour(Ogre::ColourValue::White);
        SceneNode *lightNode =
        mSceneMgr->getRootSceneNode()->createChildSceneNode("lightNode");
        lightNode->setPosition(Ogre::Vector3(-10, 100, 10));
        lightNode->setOrientation(0.2, -1, 0.2,1);
        lightNode->attachObject(spotLight);
        lightNode->setPosition(mCamera->getDerivedPosition());
        lightNode->setOrientation(mCamera->getDerivedOrientation());
        
        Light* dirLight = mSceneMgr->createLight("dirLight");
        dirLight->setType(Ogre::Light::LT_DIRECTIONAL);
        dirLight->setDiffuseColour(Ogre::ColourValue::White);
        dirLight->setSpecularColour(Ogre::ColourValue::White);
        SceneNode *lightNode2 =
        mSceneMgr->getRootSceneNode()->createChildSceneNode();
        lightNode2->setOrientation(0.538657, -0.0805028, 0.829459, 0.123963);
        lightNode2->attachObject(dirLight);
        
        mCameraMan->setStyle(CS_ORBIT);
        mCameraMan->setYawPitchDist(Degree(0), Degree(10), 1000);
        mTrayMgr->showCursor();
        
        phyWorld = m_dynamicsWorld;
        phyWorld->getDispatchInfo().m_enableSPU = true;
        phyWorld->setGravity(btVector3(0, -9.8, 0));
        
        Ogre::Entity *ogreMeshEntity = mSceneMgr->createEntity("ogreMeshEntity", "Tuch.mesh");
        Vector3 pos2 = Vector3(0.2,12,0);
        Quaternion rot2 = Quaternion::IDENTITY;
        Ogre::SceneNode* node = mSceneMgr->getRootSceneNode()->createChildSceneNode("node", pos2, rot2);
        node->attachObject(ogreMeshEntity);
        node->roll(Ogre::Degree(-10));
        
        // Softbody
        m_softBodyWorldInfo.air_density = (btScalar)1.2;
        m_softBodyWorldInfo.m_gravity.setValue(0,-9.81,0);
        m_softBodyWorldInfo.m_dispatcher = m_dispatcher;
        m_softBodyWorldInfo.m_sparsesdf.Reset();
        m_softBodyWorldInfo.m_sparsesdf.Initialize();
        
        softBody = new BtOgre::BtOgreSoftBody(&m_softBodyWorldInfo);
        
        MeshPtr myMesh = ogreMeshEntity->getMesh();
        MeshData MyMeshData = *(MeshUtils::getMeshData(myMesh));
        
        unsigned long* indices = new unsigned long[MyMeshData.triangleCount*3];
        MeshUtils::meshBuffersToArrays(myMesh, MyMeshData.vertices, indices);
        btSoftBody* psb = softBody->create(ogreMeshEntity,
                                           MyMeshData.vertexCount,
                                           MyMeshData.vertices,
                                           MyMeshData.triangleCount*3,
                                           (unsigned int*)indices);
        
        btSoftBody::Material* pm=psb->appendMaterial();
        pm->m_kLST = 0.1;
        psb->m_cfg.piterations = 2;
        psb->m_cfg.kDF = 0.5;
        psb->m_cfg.collisions |= btSoftBody::fCollision::VF_SS;
        psb->generateClusters(0);
        psb->randomizeConstraints();
        psb->setTotalMass(100,true);
        
        getSoftDynamicsWorld()->addSoftBody(psb);
        
        mGroundEntity = mSceneMgr->createEntity("groundEntity", "TestLevel_b0.mesh");
        mSceneMgr->getRootSceneNode()->createChildSceneNode("groundNode")->attachObject(mGroundEntity);
        
        BtOgre::StaticMeshToShapeConverter converter(mGroundEntity);
        mGroundShape = converter.createTrimesh();
        
        btDefaultMotionState* groundState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,0,0)));
        
        mGroundBody = new btRigidBody(0, groundState, mGroundShape, btVector3(0,0,0));
        phyWorld->addRigidBody(mGroundBody);
        
        btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),0);
        
        btScalar mass2(0.);
        
        btVector3 localInertia(0,0,0);
        btTransform groundTransform;
        groundTransform.setIdentity();
        groundTransform.setOrigin(btVector3(0,-3,0));
        
        btDefaultMotionState* myMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,-100,0)));
        btRigidBody::btRigidBodyConstructionInfo rbInfo(mass2,myMotionState,groundShape,localInertia);
        btRigidBody* body = new btRigidBody(rbInfo);
        phyWorld->addRigidBody(body);
    }
};

#endif
Post Reply