Refactor Newton's Cradle. Implement Newton's Cradle with Softbody ropes.
This commit is contained in:
@@ -171,6 +171,8 @@ SET(BulletExampleBrowser_SRCS
|
|||||||
../ExtendedTutorials/SimpleJoint.h
|
../ExtendedTutorials/SimpleJoint.h
|
||||||
../ExtendedTutorials/NewtonsCradle.cpp
|
../ExtendedTutorials/NewtonsCradle.cpp
|
||||||
../ExtendedTutorials/NewtonsCradle.h
|
../ExtendedTutorials/NewtonsCradle.h
|
||||||
|
../ExtendedTutorials/NewtonsRopeCradle.cpp
|
||||||
|
../ExtendedTutorials/NewtonsRopeCradle.h
|
||||||
../ExtendedTutorials/InclinedPlane.cpp
|
../ExtendedTutorials/InclinedPlane.cpp
|
||||||
../ExtendedTutorials/InclinedPlane.h
|
../ExtendedTutorials/InclinedPlane.h
|
||||||
../ExtendedTutorials/MultiPendulum.cpp
|
../ExtendedTutorials/MultiPendulum.cpp
|
||||||
|
|||||||
@@ -66,6 +66,7 @@
|
|||||||
#include "../ExtendedTutorials/RigidBodyFromObj.h"
|
#include "../ExtendedTutorials/RigidBodyFromObj.h"
|
||||||
#include "../ExtendedTutorials/InclinedPlane.h"
|
#include "../ExtendedTutorials/InclinedPlane.h"
|
||||||
#include "../ExtendedTutorials/NewtonsCradle.h"
|
#include "../ExtendedTutorials/NewtonsCradle.h"
|
||||||
|
#include "../ExtendedTutorials/NewtonsRopeCradle.h"
|
||||||
#include "../ExtendedTutorials/MultiPendulum.h"
|
#include "../ExtendedTutorials/MultiPendulum.h"
|
||||||
|
|
||||||
struct ExampleEntry
|
struct ExampleEntry
|
||||||
@@ -279,6 +280,7 @@ static ExampleEntry gDefaultExamples[]=
|
|||||||
ExampleEntry(1,"Simple Bridge", "Create a simple bridge using a pair of point2point/distance constraints. You may click and drag any plank to see the bridge respond.", ET_BridgeCreateFunc),
|
ExampleEntry(1,"Simple Bridge", "Create a simple bridge using a pair of point2point/distance constraints. You may click and drag any plank to see the bridge respond.", ET_BridgeCreateFunc),
|
||||||
ExampleEntry(1,"Inclined Plane", "Create an inclined plane to show restitution and different types of friction. Use the sliders to vary restitution and friction and press space to reset the scene.", ET_InclinedPlaneCreateFunc),
|
ExampleEntry(1,"Inclined Plane", "Create an inclined plane to show restitution and different types of friction. Use the sliders to vary restitution and friction and press space to reset the scene.", ET_InclinedPlaneCreateFunc),
|
||||||
ExampleEntry(1,"Newton's Cradle", "Create a Newton's Cradle using a pair of point2point/slider constraints. Press 1/2 to lengthen/shorten the pendula, press 3 to displace pendula. Use the sliders to select the number of pendula in total (reset simulation), the number of displaced pendula and other options.", ET_NewtonsCradleCreateFunc),
|
ExampleEntry(1,"Newton's Cradle", "Create a Newton's Cradle using a pair of point2point/slider constraints. Press 1/2 to lengthen/shorten the pendula, press 3 to displace pendula. Use the sliders to select the number of pendula in total (reset simulation), the number of displaced pendula and other options.", ET_NewtonsCradleCreateFunc),
|
||||||
|
ExampleEntry(1,"Newton's Rope Cradle", "Create a Newton's Cradle using ropes. Press 3 to displace pendula. Use the sliders to select the number of pendula in total (reset simulation) and the number of displaced pendula.",ET_NewtonsRopeCradleCreateFunc),
|
||||||
ExampleEntry(1,"Multi-Pendulum", "Create a Multi-Pendulum using point2point/slider constraints. Press 1/2 to lengthen/shorten the pendula, press 3 to displace pendula. Use the sliders to select the number of pendula in total (reset simulation), the number of displaced pendula and other options.",ET_MultiPendulumCreateFunc),
|
ExampleEntry(1,"Multi-Pendulum", "Create a Multi-Pendulum using point2point/slider constraints. Press 1/2 to lengthen/shorten the pendula, press 3 to displace pendula. Use the sliders to select the number of pendula in total (reset simulation), the number of displaced pendula and other options.",ET_MultiPendulumCreateFunc),
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ static btScalar gPendulaQty = 5; // Number of pendula in newton's cradle
|
|||||||
static btScalar gDisplacedPendula = 1; // number of displaced pendula
|
static btScalar gDisplacedPendula = 1; // number of displaced pendula
|
||||||
//TODO: This is an int as well
|
//TODO: This is an int as well
|
||||||
|
|
||||||
static btScalar gPendulaRestitution = 1; // pendula restition when hitting against each other
|
static btScalar gPendulaRestitution = 1; // pendula restitution when hitting against each other
|
||||||
|
|
||||||
static btScalar gSphereRadius = 1; // pendula radius
|
static btScalar gSphereRadius = 1; // pendula radius
|
||||||
|
|
||||||
@@ -48,8 +48,7 @@ struct NewtonsCradleExample: public CommonRigidBodyBase {
|
|||||||
}
|
}
|
||||||
virtual void initPhysics();
|
virtual void initPhysics();
|
||||||
virtual void renderScene();
|
virtual void renderScene();
|
||||||
virtual void createPendulum(btSphereShape* colShape, btScalar xPosition,
|
virtual void createPendulum(btSphereShape* colShape, btVector3 position, btScalar length, btScalar mass);
|
||||||
btScalar yPosition, btScalar zPosition, btScalar length, btScalar mass);
|
|
||||||
virtual void changePendulaLength(btScalar length);
|
virtual void changePendulaLength(btScalar length);
|
||||||
virtual void changePendulaRestitution(btScalar restitution);
|
virtual void changePendulaRestitution(btScalar restitution);
|
||||||
virtual void stepSimulation(float deltaTime);
|
virtual void stepSimulation(float deltaTime);
|
||||||
@@ -139,12 +138,11 @@ void NewtonsCradleExample::initPhysics() {
|
|||||||
+ btIDebugDraw::DBG_DrawConstraints
|
+ btIDebugDraw::DBG_DrawConstraints
|
||||||
+ btIDebugDraw::DBG_DrawConstraintLimits);
|
+ btIDebugDraw::DBG_DrawConstraintLimits);
|
||||||
|
|
||||||
{ // create the pendulum starting at the indicated position below and where each pendulum has the following mass
|
{ // create the pendula starting at the indicated position below and where each pendulum has the following mass
|
||||||
btScalar pendulumMass(1.f);
|
btScalar pendulumMass(1.f);
|
||||||
|
|
||||||
btScalar xPosition(0.0f); // initial left-most pendulum position
|
btVector3 position(0.0f,15.0f,0.0f); // initial left-most pendulum position
|
||||||
btScalar yPosition(15.0f);
|
btQuaternion orientation(0,0,0,1); // orientation of the pendula
|
||||||
btScalar zPosition(0.0f);
|
|
||||||
|
|
||||||
// Re-using the same collision is better for memory usage and performance
|
// Re-using the same collision is better for memory usage and performance
|
||||||
btSphereShape* pendulumShape = new btSphereShape(gSphereRadius);
|
btSphereShape* pendulumShape = new btSphereShape(gSphereRadius);
|
||||||
@@ -153,11 +151,10 @@ void NewtonsCradleExample::initPhysics() {
|
|||||||
for (int i = 0; i < floor(gPendulaQty); i++) {
|
for (int i = 0; i < floor(gPendulaQty); i++) {
|
||||||
|
|
||||||
// create pendulum
|
// create pendulum
|
||||||
createPendulum(pendulumShape, xPosition, yPosition, zPosition,
|
createPendulum(pendulumShape, position, gInitialPendulumLength, pendulumMass);
|
||||||
gInitialPendulumLength, pendulumMass);
|
|
||||||
|
|
||||||
// displace the pendula 1.05 sphere size, so that they all nearly touch (small spacings in between
|
// displace the pendula 1.05 sphere size, so that they all nearly touch (small spacings in between
|
||||||
xPosition -= 2.1f * gSphereRadius;
|
position.setX(position.x()-2.1f * gSphereRadius);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -171,8 +168,7 @@ void NewtonsCradleExample::stepSimulation(float deltaTime) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NewtonsCradleExample::createPendulum(btSphereShape* colShape,
|
void NewtonsCradleExample::createPendulum(btSphereShape* colShape,btVector3 position, btScalar length, btScalar mass) {
|
||||||
btScalar xPosition, btScalar yPosition, btScalar zPosition, btScalar length, btScalar mass) {
|
|
||||||
|
|
||||||
// The pendulum looks like this (names when built):
|
// The pendulum looks like this (names when built):
|
||||||
// O topSphere
|
// O topSphere
|
||||||
@@ -184,15 +180,14 @@ void NewtonsCradleExample::createPendulum(btSphereShape* colShape,
|
|||||||
startTransform.setIdentity();
|
startTransform.setIdentity();
|
||||||
|
|
||||||
// position the top sphere above ground with a moving x position
|
// position the top sphere above ground with a moving x position
|
||||||
startTransform.setOrigin(
|
startTransform.setOrigin(position);
|
||||||
btVector3(btScalar(xPosition), btScalar(yPosition), btScalar(zPosition)));
|
|
||||||
startTransform.setRotation(btQuaternion(0, 0, 0, 1)); // zero rotation
|
startTransform.setRotation(btQuaternion(0, 0, 0, 1)); // zero rotation
|
||||||
btRigidBody* topSphere = createRigidBody(mass, startTransform, colShape);
|
btRigidBody* topSphere = createRigidBody(mass, startTransform, colShape);
|
||||||
|
|
||||||
// position the bottom sphere below the top sphere
|
// position the bottom sphere below the top sphere
|
||||||
startTransform.setOrigin(
|
startTransform.setOrigin(
|
||||||
btVector3(btScalar(xPosition), btScalar(yPosition - length),
|
btVector3(position.x(), btScalar(position.y() - length),
|
||||||
btScalar(zPosition)));
|
position.z()));
|
||||||
|
|
||||||
startTransform.setRotation(btQuaternion(0, 0, 0, 1)); // zero rotation
|
startTransform.setRotation(btQuaternion(0, 0, 0, 1)); // zero rotation
|
||||||
btRigidBody* bottomSphere = createRigidBody(mass, startTransform, colShape);
|
btRigidBody* bottomSphere = createRigidBody(mass, startTransform, colShape);
|
||||||
|
|||||||
@@ -13,10 +13,10 @@ subject to the following restrictions:
|
|||||||
3. This notice may not be removed or altered from any source distribution.
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef ET_NEWTONIAN_PENDULUM_EXAMPLE_H
|
#ifndef ET_NEWTONS_CRADLE_EXAMPLE_H
|
||||||
#define ET_NEWTONIAN_PENDULUM_EXAMPLE_H
|
#define ET_NEWTONS_CRADLE_EXAMPLE_H
|
||||||
|
|
||||||
class CommonExampleInterface* ET_NewtonsCradleCreateFunc(struct CommonExampleOptions& options);
|
class CommonExampleInterface* ET_NewtonsCradleCreateFunc(struct CommonExampleOptions& options);
|
||||||
|
|
||||||
|
|
||||||
#endif //ET_NEWTONIAN_PENDULUM_EXAMPLE_H
|
#endif //ET_NEWTONS_CRADLE_EXAMPLE_H
|
||||||
|
|||||||
345
examples/ExtendedTutorials/NewtonsRopeCradle.cpp
Normal file
345
examples/ExtendedTutorials/NewtonsRopeCradle.cpp
Normal file
@@ -0,0 +1,345 @@
|
|||||||
|
/*
|
||||||
|
Bullet Continuous Collision Detection and Physics Library
|
||||||
|
Copyright (c) 2015 Google Inc. http://bulletphysics.org
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied warranty.
|
||||||
|
In no event will the authors be held liable for any damages arising from the use of this software.
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it freely,
|
||||||
|
subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "NewtonsRopeCradle.h"
|
||||||
|
|
||||||
|
#include <vector> // TODO: Should I use another data structure?
|
||||||
|
#include <iterator>
|
||||||
|
|
||||||
|
#include "btBulletDynamicsCommon.h"
|
||||||
|
#include "LinearMath/btVector3.h"
|
||||||
|
#include "LinearMath/btAlignedObjectArray.h"
|
||||||
|
#include "../CommonInterfaces/CommonRigidBodyBase.h"
|
||||||
|
|
||||||
|
#include "BulletSoftBody/btSoftRigidDynamicsWorld.h"
|
||||||
|
#include "BulletSoftBody/btSoftBodyHelpers.h"
|
||||||
|
#include "BulletSoftBody/btSoftBodyRigidBodyCollisionConfiguration.h"
|
||||||
|
#include "../CommonInterfaces/CommonParameterInterface.h"
|
||||||
|
|
||||||
|
static btScalar gPendulaQty = 5; // Number of pendula in newton's cradle
|
||||||
|
//TODO: This would actually be an Integer, but the Slider does not like integers, so I floor it when changed
|
||||||
|
|
||||||
|
static btScalar gDisplacedPendula = 1; // number of displaced pendula
|
||||||
|
//TODO: This is an int as well
|
||||||
|
|
||||||
|
static btScalar gPendulaRestitution = 1; // pendula restition when hitting against each other
|
||||||
|
|
||||||
|
static btScalar gSphereRadius = 1; // pendula radius
|
||||||
|
|
||||||
|
static btScalar gInitialPendulumWidth = 4; // default pendula width
|
||||||
|
|
||||||
|
static btScalar gInitialPendulumHeight = 8; // default pendula height
|
||||||
|
|
||||||
|
static btScalar gRopeResolution = 1; // default rope resolution (number of links as in a chain)
|
||||||
|
|
||||||
|
static btScalar gForcingForce = 30; // default force to displace the pendula
|
||||||
|
|
||||||
|
struct NewtonsRopeCradleExample : public CommonRigidBodyBase {
|
||||||
|
NewtonsRopeCradleExample(struct GUIHelperInterface* helper) :
|
||||||
|
CommonRigidBodyBase(helper) {
|
||||||
|
}
|
||||||
|
virtual ~NewtonsRopeCradleExample(){}
|
||||||
|
virtual void initPhysics();
|
||||||
|
virtual void renderScene();
|
||||||
|
void createEmptyDynamicsWorld()
|
||||||
|
{
|
||||||
|
m_collisionConfiguration = new btSoftBodyRigidBodyCollisionConfiguration();
|
||||||
|
m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
|
||||||
|
|
||||||
|
m_broadphase = new btDbvtBroadphase();
|
||||||
|
|
||||||
|
m_solver = new btSequentialImpulseConstraintSolver;
|
||||||
|
|
||||||
|
m_dynamicsWorld = new btSoftRigidDynamicsWorld(m_dispatcher, m_broadphase, m_solver, m_collisionConfiguration);
|
||||||
|
m_dynamicsWorld->setGravity(btVector3(0, -10, 0));
|
||||||
|
|
||||||
|
softBodyWorldInfo.m_broadphase = m_broadphase;
|
||||||
|
softBodyWorldInfo.m_dispatcher = m_dispatcher;
|
||||||
|
softBodyWorldInfo.m_gravity = m_dynamicsWorld->getGravity();
|
||||||
|
softBodyWorldInfo.m_sparsesdf.Initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void createRopePendulum(btSphereShape* colShape,
|
||||||
|
btVector3 position, btQuaternion pendulumOrientation, btScalar width, btScalar height, btScalar mass);
|
||||||
|
virtual void changePendulaRestitution(btScalar restitution);
|
||||||
|
virtual void connectWithRope(btRigidBody* body1, btRigidBody* body2);
|
||||||
|
virtual bool keyboardCallback(int key, int state);
|
||||||
|
|
||||||
|
virtual btSoftRigidDynamicsWorld* getSoftDynamicsWorld()
|
||||||
|
{
|
||||||
|
///just make it a btSoftRigidDynamicsWorld please
|
||||||
|
///or we will add type checking
|
||||||
|
return (btSoftRigidDynamicsWorld*) m_dynamicsWorld;
|
||||||
|
}
|
||||||
|
void resetCamera()
|
||||||
|
{
|
||||||
|
float dist = 41;
|
||||||
|
float pitch = 52;
|
||||||
|
float yaw = 35;
|
||||||
|
float targetPos[3]={0,0.46,0};
|
||||||
|
m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<btSliderConstraint*> constraints;
|
||||||
|
std::vector<btRigidBody*> pendula;
|
||||||
|
|
||||||
|
btSoftBodyWorldInfo softBodyWorldInfo;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
static NewtonsRopeCradleExample* nex = NULL;
|
||||||
|
|
||||||
|
void onRopePendulaRestitutionChanged(float pendulaRestitution);
|
||||||
|
|
||||||
|
void floorRSliderValue(float notUsed);
|
||||||
|
|
||||||
|
void NewtonsRopeCradleExample::initPhysics()
|
||||||
|
{
|
||||||
|
|
||||||
|
{ // create a slider to change the number of pendula
|
||||||
|
SliderParams slider("Number of Pendula", &gPendulaQty);
|
||||||
|
slider.m_minVal = 1;
|
||||||
|
slider.m_maxVal = 50;
|
||||||
|
slider.m_callback = floorRSliderValue; // hack to get integer values
|
||||||
|
slider.m_clampToNotches = false;
|
||||||
|
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(
|
||||||
|
slider);
|
||||||
|
}
|
||||||
|
|
||||||
|
{ // create a slider to change the number of displaced pendula
|
||||||
|
SliderParams slider("Number of Displaced Pendula", &gDisplacedPendula);
|
||||||
|
slider.m_minVal = 0;
|
||||||
|
slider.m_maxVal = 49;
|
||||||
|
slider.m_callback = floorRSliderValue; // hack to get integer values
|
||||||
|
slider.m_clampToNotches = false;
|
||||||
|
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(
|
||||||
|
slider);
|
||||||
|
}
|
||||||
|
|
||||||
|
{ // create a slider to change the pendula restitution
|
||||||
|
SliderParams slider("Pendula Restitution", &gPendulaRestitution);
|
||||||
|
slider.m_minVal = 0;
|
||||||
|
slider.m_maxVal = 1;
|
||||||
|
slider.m_clampToNotches = false;
|
||||||
|
slider.m_callback = onRopePendulaRestitutionChanged;
|
||||||
|
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(
|
||||||
|
slider);
|
||||||
|
}
|
||||||
|
|
||||||
|
{ // create a slider to change the rope resolution
|
||||||
|
SliderParams slider("Rope Resolution", &gRopeResolution);
|
||||||
|
slider.m_minVal = 1;
|
||||||
|
slider.m_maxVal = 20;
|
||||||
|
slider.m_clampToNotches = false;
|
||||||
|
slider.m_callback = floorRSliderValue;
|
||||||
|
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(
|
||||||
|
slider);
|
||||||
|
}
|
||||||
|
|
||||||
|
{ // create a slider to change the pendulum width
|
||||||
|
SliderParams slider("Pendulum Width", &gInitialPendulumWidth);
|
||||||
|
slider.m_minVal = 0;
|
||||||
|
slider.m_maxVal = 40;
|
||||||
|
slider.m_clampToNotches = false;
|
||||||
|
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(
|
||||||
|
slider);
|
||||||
|
}
|
||||||
|
|
||||||
|
{ // create a slider to change the pendulum height
|
||||||
|
SliderParams slider("Pendulum Height", &gInitialPendulumHeight);
|
||||||
|
slider.m_minVal = 0;
|
||||||
|
slider.m_maxVal = 40;
|
||||||
|
slider.m_clampToNotches = false;
|
||||||
|
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(
|
||||||
|
slider);
|
||||||
|
}
|
||||||
|
|
||||||
|
{ // create a slider to change the force to displace the lowest pendulum
|
||||||
|
SliderParams slider("Displacement force", &gForcingForce);
|
||||||
|
slider.m_minVal = 0.1;
|
||||||
|
slider.m_maxVal = 200;
|
||||||
|
slider.m_clampToNotches = false;
|
||||||
|
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(
|
||||||
|
slider);
|
||||||
|
}
|
||||||
|
|
||||||
|
m_guiHelper->setUpAxis(1);
|
||||||
|
|
||||||
|
createEmptyDynamicsWorld();
|
||||||
|
|
||||||
|
// create a debug drawer
|
||||||
|
m_guiHelper->createPhysicsDebugDrawer(m_dynamicsWorld);
|
||||||
|
if (m_dynamicsWorld->getDebugDrawer())
|
||||||
|
m_dynamicsWorld->getDebugDrawer()->setDebugMode(
|
||||||
|
btIDebugDraw::DBG_DrawWireframe
|
||||||
|
+ btIDebugDraw::DBG_DrawContactPoints
|
||||||
|
+ btIDebugDraw::DBG_DrawConstraints
|
||||||
|
+ btIDebugDraw::DBG_DrawConstraintLimits);
|
||||||
|
|
||||||
|
{ // create the pendula starting at the indicated position below and where each pendulum has the following mass
|
||||||
|
btScalar pendulumMass(1.0f);
|
||||||
|
|
||||||
|
btVector3 position(0.0f,15.0f,0.0f); // initial left-most pendulum position
|
||||||
|
btQuaternion orientation(0,0,0,1); // orientation of the pendula
|
||||||
|
|
||||||
|
// Re-using the same collision is better for memory usage and performance
|
||||||
|
btSphereShape* pendulumShape = new btSphereShape(gSphereRadius);
|
||||||
|
m_collisionShapes.push_back(pendulumShape);
|
||||||
|
|
||||||
|
for (int i = 0; i < floor(gPendulaQty); i++) {
|
||||||
|
|
||||||
|
// create pendulum
|
||||||
|
createRopePendulum(pendulumShape, position, orientation,gInitialPendulumWidth,
|
||||||
|
gInitialPendulumHeight, pendulumMass);
|
||||||
|
|
||||||
|
// displace the pendula 1.05 sphere size, so that they all nearly touch (small spacings in between)
|
||||||
|
position.setX(position.x()-2.1f * gSphereRadius);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);
|
||||||
|
}
|
||||||
|
|
||||||
|
void NewtonsRopeCradleExample::connectWithRope(btRigidBody* body1, btRigidBody* body2)
|
||||||
|
{
|
||||||
|
btSoftBody* softBodyRope0 = btSoftBodyHelpers::CreateRope(softBodyWorldInfo,body1->getWorldTransform().getOrigin(),body2->getWorldTransform().getOrigin(),gRopeResolution,0);
|
||||||
|
softBodyRope0->setTotalMass(0.1f);
|
||||||
|
|
||||||
|
softBodyRope0->appendAnchor(0,body1);
|
||||||
|
softBodyRope0->appendAnchor(softBodyRope0->m_nodes.size()-1,body2);
|
||||||
|
|
||||||
|
softBodyRope0->m_cfg.piterations = 5;
|
||||||
|
softBodyRope0->m_cfg.kDP = 0.005f;
|
||||||
|
softBodyRope0->m_cfg.kSHR = 1;
|
||||||
|
softBodyRope0->m_cfg.kCHR = 1;
|
||||||
|
softBodyRope0->m_cfg.kKHR = 1;
|
||||||
|
|
||||||
|
getSoftDynamicsWorld()->addSoftBody(softBodyRope0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void NewtonsRopeCradleExample::createRopePendulum(btSphereShape* colShape,
|
||||||
|
btVector3 position, btQuaternion pendulumOrientation, btScalar width, btScalar height, btScalar mass) {
|
||||||
|
|
||||||
|
// The pendulum looks like this (names when built):
|
||||||
|
// O O topSphere1 topSphere2
|
||||||
|
// \ /
|
||||||
|
// O bottomSphere
|
||||||
|
|
||||||
|
//create a dynamic pendulum
|
||||||
|
btTransform startTransform;
|
||||||
|
startTransform.setIdentity();
|
||||||
|
|
||||||
|
// calculate sphere positions
|
||||||
|
btVector3 topSphere1RelPosition(0,0,width);
|
||||||
|
btVector3 topSphere2RelPosition(0,0,-width);
|
||||||
|
btVector3 bottomSphereRelPosition(0,-height,0);
|
||||||
|
|
||||||
|
|
||||||
|
// position the top sphere above ground with appropriate orientation
|
||||||
|
startTransform.setOrigin(btVector3(0,0,0)); // no translation intitially
|
||||||
|
startTransform.setRotation(pendulumOrientation); // pendulum rotation
|
||||||
|
startTransform.setOrigin(startTransform * topSphere1RelPosition); // rotate this position
|
||||||
|
startTransform.setOrigin(position + startTransform.getOrigin()); // add non-rotated position to the relative position
|
||||||
|
btRigidBody* topSphere1 = createRigidBody(0, startTransform, colShape); // make top sphere static
|
||||||
|
|
||||||
|
// position the top sphere above ground with appropriate orientation
|
||||||
|
startTransform.setOrigin(btVector3(0,0,0)); // no translation intitially
|
||||||
|
startTransform.setRotation(pendulumOrientation); // pendulum rotation
|
||||||
|
startTransform.setOrigin(startTransform * topSphere2RelPosition); // rotate this position
|
||||||
|
startTransform.setOrigin(position + startTransform.getOrigin()); // add non-rotated position to the relative position
|
||||||
|
btRigidBody* topSphere2 = createRigidBody(0, startTransform, colShape); // make top sphere static
|
||||||
|
|
||||||
|
// position the bottom sphere below the top sphere
|
||||||
|
startTransform.setOrigin(btVector3(0,0,0)); // no translation intitially
|
||||||
|
startTransform.setRotation(pendulumOrientation); // pendulum rotation
|
||||||
|
startTransform.setOrigin(startTransform * bottomSphereRelPosition); // rotate this position
|
||||||
|
startTransform.setOrigin(position + startTransform.getOrigin()); // add non-rotated position to the relative position
|
||||||
|
btRigidBody* bottomSphere = createRigidBody(mass, startTransform, colShape);
|
||||||
|
bottomSphere->setFriction(0); // we do not need friction here
|
||||||
|
pendula.push_back(bottomSphere);
|
||||||
|
|
||||||
|
// disable the deactivation when objects do not move anymore
|
||||||
|
topSphere1->setActivationState(DISABLE_DEACTIVATION);
|
||||||
|
topSphere2->setActivationState(DISABLE_DEACTIVATION);
|
||||||
|
bottomSphere->setActivationState(DISABLE_DEACTIVATION);
|
||||||
|
|
||||||
|
bottomSphere->setRestitution(gPendulaRestitution); // set pendula restitution
|
||||||
|
|
||||||
|
// add ropes between spheres
|
||||||
|
connectWithRope(topSphere1, bottomSphere);
|
||||||
|
connectWithRope(topSphere2, bottomSphere);
|
||||||
|
}
|
||||||
|
|
||||||
|
void NewtonsRopeCradleExample::renderScene()
|
||||||
|
{
|
||||||
|
CommonRigidBodyBase::renderScene();
|
||||||
|
btSoftRigidDynamicsWorld* softWorld = getSoftDynamicsWorld();
|
||||||
|
|
||||||
|
for ( int i=0;i<softWorld->getSoftBodyArray().size();i++)
|
||||||
|
{
|
||||||
|
btSoftBody* psb=(btSoftBody*)softWorld->getSoftBodyArray()[i];
|
||||||
|
//if (softWorld->getDebugDrawer() && !(softWorld->getDebugDrawer()->getDebugMode() & (btIDebugDraw::DBG_DrawWireframe)))
|
||||||
|
{
|
||||||
|
btSoftBodyHelpers::DrawFrame(psb,softWorld->getDebugDrawer());
|
||||||
|
btSoftBodyHelpers::Draw(psb,softWorld->getDebugDrawer(),softWorld->getDrawFlags());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void NewtonsRopeCradleExample::changePendulaRestitution(btScalar restitution) {
|
||||||
|
for (std::vector<btRigidBody*>::iterator rit = pendula.begin();
|
||||||
|
rit != pendula.end(); rit++) {
|
||||||
|
btAssert((*rit) && "Null constraint");
|
||||||
|
|
||||||
|
(*rit)->setRestitution(restitution);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool NewtonsRopeCradleExample::keyboardCallback(int key, int state) {
|
||||||
|
//b3Printf("Key pressed: %d in state %d \n",key,state);
|
||||||
|
|
||||||
|
// key 3
|
||||||
|
switch (key) {
|
||||||
|
case 51 /*ASCII for 3*/: {
|
||||||
|
for (int i = 0; i < gDisplacedPendula; i++) {
|
||||||
|
if (gDisplacedPendula >= 0 && gDisplacedPendula <= gPendulaQty)
|
||||||
|
pendula[i]->applyCentralForce(btVector3(gForcingForce, 0, 0));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// GUI parameter modifiers
|
||||||
|
|
||||||
|
void onRopePendulaRestitutionChanged(float pendulaRestitution) {
|
||||||
|
if (nex){
|
||||||
|
nex->changePendulaRestitution(pendulaRestitution);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void floorRSliderValue(float notUsed) {
|
||||||
|
gPendulaQty = floor(gPendulaQty);
|
||||||
|
gDisplacedPendula = floor(gDisplacedPendula);
|
||||||
|
gRopeResolution = floor(gRopeResolution);
|
||||||
|
}
|
||||||
|
|
||||||
|
CommonExampleInterface* ET_NewtonsRopeCradleCreateFunc(
|
||||||
|
CommonExampleOptions& options) {
|
||||||
|
nex = new NewtonsRopeCradleExample(options.m_guiHelper);
|
||||||
|
return nex;
|
||||||
|
}
|
||||||
22
examples/ExtendedTutorials/NewtonsRopeCradle.h
Normal file
22
examples/ExtendedTutorials/NewtonsRopeCradle.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
/*
|
||||||
|
Bullet Continuous Collision Detection and Physics Library
|
||||||
|
Copyright (c) 2015 Google Inc. http://bulletphysics.org
|
||||||
|
|
||||||
|
This software is provided 'as-is', without any express or implied warranty.
|
||||||
|
In no event will the authors be held liable for any damages arising from the use of this software.
|
||||||
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
|
including commercial applications, and to alter it and redistribute it freely,
|
||||||
|
subject to the following restrictions:
|
||||||
|
|
||||||
|
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
||||||
|
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
||||||
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ET_NEWTONS_ROPE_CRADLE_EXAMPLE_H
|
||||||
|
#define ET_NEWTONS_ROPE_CRADLE_EXAMPLE_H
|
||||||
|
|
||||||
|
class CommonExampleInterface* ET_NewtonsRopeCradleCreateFunc(struct CommonExampleOptions& options);
|
||||||
|
|
||||||
|
|
||||||
|
#endif //ET_NEWTONS_ROPE_CRADLE_EXAMPLE_H
|
||||||
Reference in New Issue
Block a user