Code-style consistency improvement:
Apply clang-format-all.sh using the _clang-format file through all the cpp/.h files. make sure not to apply it to certain serialization structures, since some parser expects the * as part of the name, instead of type. This commit contains no other changes aside from adding and applying clang-format-all.sh
This commit is contained in:
@@ -15,38 +15,40 @@
|
||||
|
||||
#include "NewtonsCradle.h"
|
||||
|
||||
#include <vector> // TODO: Should I use another data structure?
|
||||
#include <vector> // TODO: Should I use another data structure?
|
||||
#include <iterator>
|
||||
|
||||
#include "btBulletDynamicsCommon.h"
|
||||
#include "LinearMath/btVector3.h"
|
||||
#include "LinearMath/btAlignedObjectArray.h"
|
||||
#include "LinearMath/btAlignedObjectArray.h"
|
||||
#include "../CommonInterfaces/CommonRigidBodyBase.h"
|
||||
#include "../CommonInterfaces/CommonParameterInterface.h"
|
||||
|
||||
static btScalar gPendulaQty = 5; // Number of pendula in newton's cradle
|
||||
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
|
||||
static btScalar gDisplacedPendula = 1; // number of displaced pendula
|
||||
//TODO: This is an int as well
|
||||
|
||||
static btScalar gPendulaRestitution = 1; // pendula restitution 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
|
||||
|
||||
static btScalar gCurrentPendulumLength = 8; // current pendula length
|
||||
static btScalar gCurrentPendulumLength = 8; // current pendula length
|
||||
|
||||
static btScalar gInitialPendulumLength = 8; // default pendula length
|
||||
static btScalar gInitialPendulumLength = 8; // default pendula length
|
||||
|
||||
static btScalar gDisplacementForce = 30; // default force to displace the pendula
|
||||
static btScalar gDisplacementForce = 30; // default force to displace the pendula
|
||||
|
||||
static btScalar gForceScalar = 0; // default force scalar to apply a displacement
|
||||
static btScalar gForceScalar = 0; // default force scalar to apply a displacement
|
||||
|
||||
struct NewtonsCradleExample: public CommonRigidBodyBase {
|
||||
NewtonsCradleExample(struct GUIHelperInterface* helper) :
|
||||
CommonRigidBodyBase(helper) {
|
||||
struct NewtonsCradleExample : public CommonRigidBodyBase
|
||||
{
|
||||
NewtonsCradleExample(struct GUIHelperInterface* helper) : CommonRigidBodyBase(helper)
|
||||
{
|
||||
}
|
||||
virtual ~NewtonsCradleExample() {
|
||||
virtual ~NewtonsCradleExample()
|
||||
{
|
||||
}
|
||||
virtual void initPhysics();
|
||||
virtual void renderScene();
|
||||
@@ -56,48 +58,49 @@ struct NewtonsCradleExample: public CommonRigidBodyBase {
|
||||
virtual void stepSimulation(float deltaTime);
|
||||
virtual bool keyboardCallback(int key, int state);
|
||||
virtual void applyPendulumForce(btScalar pendulumForce);
|
||||
void resetCamera() {
|
||||
void resetCamera()
|
||||
{
|
||||
float dist = 41;
|
||||
float pitch = -35;
|
||||
float yaw = 52;
|
||||
float targetPos[3] = { 0, 0.46, 0 };
|
||||
float targetPos[3] = {0, 0.46, 0};
|
||||
m_guiHelper->resetCamera(dist, yaw, pitch, targetPos[0], targetPos[1],
|
||||
targetPos[2]);
|
||||
targetPos[2]);
|
||||
}
|
||||
|
||||
std::vector<btSliderConstraint*> constraints; // keep a handle to the slider constraints
|
||||
std::vector<btRigidBody*> pendula; // keep a handle to the pendula
|
||||
std::vector<btSliderConstraint*> constraints; // keep a handle to the slider constraints
|
||||
std::vector<btRigidBody*> pendula; // keep a handle to the pendula
|
||||
};
|
||||
|
||||
static NewtonsCradleExample* nex = NULL;
|
||||
|
||||
void onPendulaLengthChanged(float pendulaLength, void* userPtr); // Change the pendula length
|
||||
void onPendulaLengthChanged(float pendulaLength, void* userPtr); // Change the pendula length
|
||||
|
||||
void onPendulaRestitutionChanged(float pendulaRestitution, void* userPtr); // change the pendula restitution
|
||||
void onPendulaRestitutionChanged(float pendulaRestitution, void* userPtr); // change the pendula restitution
|
||||
|
||||
void applyForceWithForceScalar(float forceScalar);
|
||||
|
||||
void NewtonsCradleExample::initPhysics() {
|
||||
|
||||
{ // create a slider to change the number of pendula
|
||||
void NewtonsCradleExample::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_clampToIntegers = true;
|
||||
slider.m_clampToIntegers = true;
|
||||
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(
|
||||
slider);
|
||||
}
|
||||
|
||||
{ // create a slider to change the number of displaced pendula
|
||||
{ // 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_clampToIntegers = true;
|
||||
slider.m_clampToIntegers = true;
|
||||
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(
|
||||
slider);
|
||||
}
|
||||
|
||||
{ // create a slider to change the pendula restitution
|
||||
{ // create a slider to change the pendula restitution
|
||||
SliderParams slider("Pendula Restitution", &gPendulaRestitution);
|
||||
slider.m_minVal = 0;
|
||||
slider.m_maxVal = 1;
|
||||
@@ -107,7 +110,7 @@ void NewtonsCradleExample::initPhysics() {
|
||||
slider);
|
||||
}
|
||||
|
||||
{ // create a slider to change the pendulum length
|
||||
{ // create a slider to change the pendulum length
|
||||
SliderParams slider("Pendula Length", &gCurrentPendulumLength);
|
||||
slider.m_minVal = 0;
|
||||
slider.m_maxVal = 49;
|
||||
@@ -117,7 +120,7 @@ void NewtonsCradleExample::initPhysics() {
|
||||
slider);
|
||||
}
|
||||
|
||||
{ // create a slider to change the force to displace the lowest pendulum
|
||||
{ // create a slider to change the force to displace the lowest pendulum
|
||||
SliderParams slider("Displacement force", &gDisplacementForce);
|
||||
slider.m_minVal = 0.1;
|
||||
slider.m_maxVal = 200;
|
||||
@@ -126,7 +129,7 @@ void NewtonsCradleExample::initPhysics() {
|
||||
slider);
|
||||
}
|
||||
|
||||
{ // create a slider to apply the force by slider
|
||||
{ // create a slider to apply the force by slider
|
||||
SliderParams slider("Apply displacement force", &gForceScalar);
|
||||
slider.m_minVal = -1;
|
||||
slider.m_maxVal = 1;
|
||||
@@ -143,45 +146,43 @@ void NewtonsCradleExample::initPhysics() {
|
||||
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);
|
||||
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
|
||||
{ // create the pendula starting at the indicated position below and where each pendulum has the following mass
|
||||
btScalar pendulumMass(1.f);
|
||||
|
||||
btVector3 position(0.0f,15.0f,0.0f); // initial left-most pendulum position
|
||||
btQuaternion orientation(0,0,0,1); // orientation of the pendula
|
||||
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++) {
|
||||
|
||||
for (int i = 0; i < floor(gPendulaQty); i++)
|
||||
{
|
||||
// create pendulum
|
||||
createPendulum(pendulumShape, position, gInitialPendulumLength, 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);
|
||||
position.setX(position.x() - 2.1f * gSphereRadius);
|
||||
}
|
||||
}
|
||||
|
||||
m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);
|
||||
}
|
||||
|
||||
void NewtonsCradleExample::stepSimulation(float deltaTime) {
|
||||
void NewtonsCradleExample::stepSimulation(float deltaTime)
|
||||
{
|
||||
applyForceWithForceScalar(gForceScalar); // apply force defined by apply force slider
|
||||
|
||||
applyForceWithForceScalar(gForceScalar); // apply force defined by apply force slider
|
||||
|
||||
if (m_dynamicsWorld) {
|
||||
if (m_dynamicsWorld)
|
||||
{
|
||||
m_dynamicsWorld->stepSimulation(deltaTime);
|
||||
}
|
||||
}
|
||||
|
||||
void NewtonsCradleExample::createPendulum(btSphereShape* colShape, const btVector3& position, btScalar length, btScalar mass) {
|
||||
|
||||
void NewtonsCradleExample::createPendulum(btSphereShape* colShape, const btVector3& position, btScalar length, btScalar mass)
|
||||
{
|
||||
// The pendulum looks like this (names when built):
|
||||
// O topSphere
|
||||
// |
|
||||
@@ -193,32 +194,32 @@ void NewtonsCradleExample::createPendulum(btSphereShape* colShape, const btVecto
|
||||
|
||||
// position the top sphere above ground with a moving x position
|
||||
startTransform.setOrigin(position);
|
||||
startTransform.setRotation(btQuaternion(0, 0, 0, 1)); // zero rotation
|
||||
startTransform.setRotation(btQuaternion(0, 0, 0, 1)); // zero rotation
|
||||
btRigidBody* topSphere = createRigidBody(mass, startTransform, colShape);
|
||||
|
||||
// position the bottom sphere below the top sphere
|
||||
startTransform.setOrigin(
|
||||
btVector3(position.x(), btScalar(position.y() - length),
|
||||
position.z()));
|
||||
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);
|
||||
bottomSphere->setFriction(0); // we do not need friction here
|
||||
bottomSphere->setFriction(0); // we do not need friction here
|
||||
pendula.push_back(bottomSphere);
|
||||
|
||||
// disable the deactivation when objects do not move anymore
|
||||
topSphere->setActivationState(DISABLE_DEACTIVATION);
|
||||
bottomSphere->setActivationState(DISABLE_DEACTIVATION);
|
||||
|
||||
bottomSphere->setRestitution(gPendulaRestitution); // set pendula restitution
|
||||
bottomSphere->setRestitution(gPendulaRestitution); // set pendula restitution
|
||||
|
||||
//make the top sphere position "fixed" to the world by attaching with a point to point constraint
|
||||
// The pivot is defined in the reference frame of topSphere, so the attachment is exactly at the center of the topSphere
|
||||
btVector3 constraintPivot(btVector3(0.0f, 0.0f, 0.0f));
|
||||
btPoint2PointConstraint* p2pconst = new btPoint2PointConstraint(*topSphere,
|
||||
constraintPivot);
|
||||
constraintPivot);
|
||||
|
||||
p2pconst->setDbgDrawSize(btScalar(5.f)); // set the size of the debug drawing
|
||||
p2pconst->setDbgDrawSize(btScalar(5.f)); // set the size of the debug drawing
|
||||
|
||||
// add the constraint to the world
|
||||
m_dynamicsWorld->addConstraint(p2pconst, true);
|
||||
@@ -234,8 +235,8 @@ void NewtonsCradleExample::createPendulum(btSphereShape* colShape, const btVecto
|
||||
// the slider constraint is x aligned per default, but we want it to be y aligned, therefore we rotate it
|
||||
btQuaternion qt;
|
||||
qt.setEuler(0, 0, -SIMD_HALF_PI);
|
||||
constraintPivotInTopSphereRF.setRotation(qt); //we use Y like up Axis
|
||||
constraintPivotInBottomSphereRF.setRotation(qt); //we use Y like up Axis
|
||||
constraintPivotInTopSphereRF.setRotation(qt); //we use Y like up Axis
|
||||
constraintPivotInBottomSphereRF.setRotation(qt); //we use Y like up Axis
|
||||
|
||||
//Obtain the position of topSphere in local reference frame of bottomSphere (the pivot is therefore in the center of topSphere)
|
||||
btVector3 topSphereInBottomSphereRF =
|
||||
@@ -244,9 +245,9 @@ void NewtonsCradleExample::createPendulum(btSphereShape* colShape, const btVecto
|
||||
constraintPivotInBottomSphereRF.setOrigin(topSphereInBottomSphereRF);
|
||||
|
||||
btSliderConstraint* sliderConst = new btSliderConstraint(*topSphere,
|
||||
*bottomSphere, constraintPivotInTopSphereRF, constraintPivotInBottomSphereRF, true);
|
||||
*bottomSphere, constraintPivotInTopSphereRF, constraintPivotInBottomSphereRF, true);
|
||||
|
||||
sliderConst->setDbgDrawSize(btScalar(5.f)); // set the size of the debug drawing
|
||||
sliderConst->setDbgDrawSize(btScalar(5.f)); // set the size of the debug drawing
|
||||
|
||||
// set limits
|
||||
// the initial setup of the constraint defines the origins of the limit dimensions,
|
||||
@@ -261,76 +262,89 @@ void NewtonsCradleExample::createPendulum(btSphereShape* colShape, const btVecto
|
||||
m_dynamicsWorld->addConstraint(sliderConst, true);
|
||||
}
|
||||
|
||||
void NewtonsCradleExample::changePendulaLength(btScalar length) {
|
||||
void NewtonsCradleExample::changePendulaLength(btScalar length)
|
||||
{
|
||||
btScalar lowerLimit = -gInitialPendulumLength;
|
||||
for (std::vector<btSliderConstraint*>::iterator sit = constraints.begin();
|
||||
sit != constraints.end(); sit++) {
|
||||
sit != constraints.end(); sit++)
|
||||
{
|
||||
btAssert((*sit) && "Null constraint");
|
||||
|
||||
//if the pendulum is being shortened beyond it's own length, we don't let the lower sphere to go past the upper one
|
||||
if (lowerLimit <= length) {
|
||||
if (lowerLimit <= length)
|
||||
{
|
||||
(*sit)->setLowerLinLimit(length + lowerLimit);
|
||||
(*sit)->setUpperLinLimit(length + lowerLimit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NewtonsCradleExample::changePendulaRestitution(btScalar restitution) {
|
||||
void NewtonsCradleExample::changePendulaRestitution(btScalar restitution)
|
||||
{
|
||||
for (std::vector<btRigidBody*>::iterator rit = pendula.begin();
|
||||
rit != pendula.end(); rit++) {
|
||||
rit != pendula.end(); rit++)
|
||||
{
|
||||
btAssert((*rit) && "Null constraint");
|
||||
|
||||
(*rit)->setRestitution(restitution);
|
||||
}
|
||||
}
|
||||
|
||||
void NewtonsCradleExample::renderScene() {
|
||||
void NewtonsCradleExample::renderScene()
|
||||
{
|
||||
CommonRigidBodyBase::renderScene();
|
||||
}
|
||||
|
||||
bool NewtonsCradleExample::keyboardCallback(int key, int state) {
|
||||
bool NewtonsCradleExample::keyboardCallback(int key, int state)
|
||||
{
|
||||
//b3Printf("Key pressed: %d in state %d \n",key,state);
|
||||
|
||||
//key 1, key 2, key 3
|
||||
switch (key) {
|
||||
case '1' /*ASCII for 1*/: {
|
||||
switch (key)
|
||||
{
|
||||
case '1' /*ASCII for 1*/:
|
||||
{
|
||||
//assumption: Sphere are aligned in Z axis
|
||||
btScalar newLimit = btScalar(gCurrentPendulumLength + 0.1);
|
||||
|
||||
//assumption: Sphere are aligned in Z axis
|
||||
btScalar newLimit = btScalar(gCurrentPendulumLength + 0.1);
|
||||
|
||||
changePendulaLength(newLimit);
|
||||
gCurrentPendulumLength = newLimit;
|
||||
|
||||
b3Printf("Increase pendulum length to %f", gCurrentPendulumLength);
|
||||
return true;
|
||||
}
|
||||
case '2' /*ASCII for 2*/: {
|
||||
|
||||
//assumption: Sphere are aligned in Z axis
|
||||
btScalar newLimit = btScalar(gCurrentPendulumLength - 0.1);
|
||||
|
||||
//is being shortened beyond it's own length, we don't let the lower sphere to go over the upper one
|
||||
if (0 <= newLimit) {
|
||||
changePendulaLength(newLimit);
|
||||
gCurrentPendulumLength = newLimit;
|
||||
}
|
||||
|
||||
b3Printf("Decrease pendulum length to %f", gCurrentPendulumLength);
|
||||
return true;
|
||||
}
|
||||
case '3' /*ASCII for 3*/: {
|
||||
applyPendulumForce(gDisplacementForce);
|
||||
return true;
|
||||
}
|
||||
b3Printf("Increase pendulum length to %f", gCurrentPendulumLength);
|
||||
return true;
|
||||
}
|
||||
case '2' /*ASCII for 2*/:
|
||||
{
|
||||
//assumption: Sphere are aligned in Z axis
|
||||
btScalar newLimit = btScalar(gCurrentPendulumLength - 0.1);
|
||||
|
||||
//is being shortened beyond it's own length, we don't let the lower sphere to go over the upper one
|
||||
if (0 <= newLimit)
|
||||
{
|
||||
changePendulaLength(newLimit);
|
||||
gCurrentPendulumLength = newLimit;
|
||||
}
|
||||
|
||||
b3Printf("Decrease pendulum length to %f", gCurrentPendulumLength);
|
||||
return true;
|
||||
}
|
||||
case '3' /*ASCII for 3*/:
|
||||
{
|
||||
applyPendulumForce(gDisplacementForce);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void NewtonsCradleExample::applyPendulumForce(btScalar pendulumForce){
|
||||
if(pendulumForce != 0){
|
||||
b3Printf("Apply %f to pendulum",pendulumForce);
|
||||
for (int i = 0; i < gDisplacedPendula; i++) {
|
||||
void NewtonsCradleExample::applyPendulumForce(btScalar pendulumForce)
|
||||
{
|
||||
if (pendulumForce != 0)
|
||||
{
|
||||
b3Printf("Apply %f to pendulum", pendulumForce);
|
||||
for (int i = 0; i < gDisplacedPendula; i++)
|
||||
{
|
||||
if (gDisplacedPendula >= 0 && gDisplacedPendula <= gPendulaQty)
|
||||
pendula[i]->applyCentralForce(btVector3(pendulumForce, 0, 0));
|
||||
}
|
||||
@@ -339,24 +353,30 @@ void NewtonsCradleExample::applyPendulumForce(btScalar pendulumForce){
|
||||
|
||||
// GUI parameter modifiers
|
||||
|
||||
void onPendulaLengthChanged(float pendulaLength, void*) {
|
||||
if (nex){
|
||||
void onPendulaLengthChanged(float pendulaLength, void*)
|
||||
{
|
||||
if (nex)
|
||||
{
|
||||
nex->changePendulaLength(pendulaLength);
|
||||
//b3Printf("Pendula length changed to %f \n",sliderValue );
|
||||
}
|
||||
}
|
||||
|
||||
void onPendulaRestitutionChanged(float pendulaRestitution, void*) {
|
||||
if (nex){
|
||||
void onPendulaRestitutionChanged(float pendulaRestitution, void*)
|
||||
{
|
||||
if (nex)
|
||||
{
|
||||
nex->changePendulaRestitution(pendulaRestitution);
|
||||
}
|
||||
}
|
||||
|
||||
void applyForceWithForceScalar(float forceScalar) {
|
||||
if(nex){
|
||||
void applyForceWithForceScalar(float forceScalar)
|
||||
{
|
||||
if (nex)
|
||||
{
|
||||
btScalar appliedForce = forceScalar * gDisplacementForce;
|
||||
|
||||
if(fabs(gForceScalar) < 0.2f)
|
||||
if (fabs(gForceScalar) < 0.2f)
|
||||
gForceScalar = 0;
|
||||
|
||||
nex->applyPendulumForce(appliedForce);
|
||||
@@ -364,7 +384,8 @@ void applyForceWithForceScalar(float forceScalar) {
|
||||
}
|
||||
|
||||
CommonExampleInterface* ET_NewtonsCradleCreateFunc(
|
||||
CommonExampleOptions& options) {
|
||||
CommonExampleOptions& options)
|
||||
{
|
||||
nex = new NewtonsCradleExample(options.m_guiHelper);
|
||||
return nex;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user