Bullet Physics Library switching rigidbody between static_object and dynamic_object - bulletphysics

I am fairly new to Bullet and my goal here is to be able to switch a btRigidBody between static and dynamic. To initialize my rigidbody I start out by doing this:
btGImpactMeshShape* triMesh=new btGImpactMeshShape(mTriMesh);
triMesh->setLocalScaling(btVector3(1,1,1));
triMesh->updateBound();
meshInfos[currentMesh].shape=triMesh;
meshInfos[currentMesh].motionState=new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(position.x,position.y,position.z)));
meshInfos[currentMesh].mass=mass;
btVector3 inertia;
meshInfos[currentMesh].shape->calculateLocalInertia(mass, inertia);
btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(0, meshInfos[currentMesh].motionState, meshInfos[currentMesh].shape, inertia);
meshInfos[currentMesh].rigidBody=new btRigidBody(rigidBodyCI);
that sets it up as static_object because the "mass" variable I have is 0 to start. Later on I have a function that checks if a boolean was switched on and it switches the rigidbody to a dynamic object like this:
if(gravityOn && !addedToWorld)
{
if(mass>0)
{
world->removeRigidBody(body);
btVector3 inertia;
body->getCollisionShape()->calculateLocalInertia(mass, inertia);
body->setMassProps(mass, inertia);
body->setLinearFactor(btVector3(1,1,1));
body->setAngularFactor(btVector3(1,1,1));
body->updateInertiaTensor();
world->addRigidBody(body);
addedToWorld=true;
}
else
{
std::cout << "Mass must be set to a number greater than 0" << std::endl;
}
}
else if(!gravityOn && addedToWorld)
{
world->removeRigidBody(body);
body->setMassProps(0, btVector3(0,0,0));
body->setLinearFactor(btVector3(0,0,0));
body->setAngularFactor(btVector3(0,0,0));
body->updateInertiaTensor();
world->addRigidBody(body);
addedToWorld=false;
}
the addedToWorld boolean just makes sure that the if statement doesn't keep running through the code block every update.
From what I have researched this should work but instead does nothing. Am I missing something? From what I've seen the best practice is to remove the rigidbody first before you do any changes to it. And then setMassProps to change inertia, setLinearFactor and setAngularFactor allows the object to not move or move depending on the vector you pass into it when collided into, updateInertiaTensor allows the inertia to update properly, and then I add the rigidbody back. I might have misunderstood some of this, any help would be greatly appreciated.

After a long night I figured it out. First of all for an unknown reason at this time using a triangle mesh (the mesh of the object) will have the possibility of crashing the application. So instead I used a convex collision shape. In addition, you will need to call some flags in the switch to properly switch between static and dynamic. The code is as such:
btConvexShape* tmpConvexShape=new btConvexTriangleMeshShape(mTriMesh);
btShapeHull* hull=new btShapeHull(tmpConvexShape);
btScalar margin=tmpConvexShape->getMargin();
hull->buildHull(margin);
tmpConvexShape->setUserPointer(hull);
btConvexHullShape* convexShape=new btConvexHullShape();
bool updateLocalAabb=false;
for(int i=0; i<hull->numVertices(); i++)
{
convexShape->addPoint(hull->getVertexPointer()[i],updateLocalAabb);
}
convexShape->recalcLocalAabb();
convexShape->setMargin(0.001f);
delete tmpConvexShape;
delete hull;
meshInfos[currentMesh].shape=convexShape;
meshInfos[currentMesh].motionState=new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(position.x,position.y,position.z)));
meshInfos[currentMesh].mass=mass;
btVector3 inertia;
meshInfos[currentMesh].shape->calculateLocalInertia(mass, inertia);
btRigidBody::btRigidBodyConstructionInfo rigidBodyCI(0, meshInfos[currentMesh].motionState, meshInfos[currentMesh].shape, inertia);
meshInfos[currentMesh].rigidBody=new btRigidBody(rigidBodyCI);
and switching:
if(gravityOn && !addedToWorld)
{
if(mass>0)
{
world->removeRigidBody(body);
btVector3 inertia(0,0,0);
body->getCollisionShape()->calculateLocalInertia(mass, inertia);
body->setActivationState(DISABLE_DEACTIVATION);
body->setMassProps(mass, inertia);
body->setLinearFactor(btVector3(1,1,1));
body->setAngularFactor(btVector3(1,1,1));
body->updateInertiaTensor();
body->clearForces();
btTransform transform;
transform.setIdentity();
float x=position.x;
float y=position.y;
float z=position.z;
transform.setOrigin(btVector3(x, y,z));
body->getCollisionShape()->setLocalScaling(btVector3(1,1,1));
body->setWorldTransform(transform);
world->addRigidBody(body);
addedToWorld=true;
}
else
{
std::cout << "Mass must be set to a number greater than 0" << std::endl;
}
}
else if(!gravityOn && addedToWorld)
{
world->removeRigidBody(body);
btVector3 inertia(0,0,0);
body->getCollisionShape()->calculateLocalInertia(0, inertia);
body->setCollisionFlags(btCollisionObject::CF_STATIC_OBJECT);
body->setMassProps(0, inertia);
body->setLinearFactor(btVector3(0,0,0));
body->setAngularFactor(btVector3(0,0,0));
body->setGravity(btVector3(0,0,0));
body->updateInertiaTensor();
body->setAngularVelocity(btVector3(0,0,0));
body->setLinearVelocity(btVector3(0,0,0));
body->clearForces();
body->setActivationState(WANTS_DEACTIVATION);
btTransform transform;
transform.setIdentity();
float x=position.x;
float y=position.y;
float z=position.z;
transform.setOrigin(btVector3(x, y,z));
body->getCollisionShape()->setLocalScaling(btVector3(1,1,1));
body->setWorldTransform(transform);
world->addRigidBody(body);
addedToWorld=false;
}

Use a short function to change the body's mass (static if 0.0f, dynamic if otherwise):
void InceptionPhysics::changeMassObject(btRigidBody* rigidBody, float mass) {
logFileStderr(VERBOSE, "mass... %5.2f\n", mass);
m_dynamicsWorld->removeRigidBody(rigidBody);
btVector3 inertia;
rigidBody->getCollisionShape()->calculateLocalInertia(mass, inertia);
rigidBody->setMassProps(mass, inertia);
m_dynamicsWorld->addRigidBody(rigidBody);
}

Related

Esp32 and JsonArduino library reading analog Values in a Array

I'm trying to understand how to use Json with the ESP32 or Arduino.
In the following code example the idea is to read the values from a potentiometer and display it on the Serial Monitor. I was expecting to see something like this when I am turning the potentiometer.
"Reading: 0,54,140,175,480,782"
"Reading: 600,523,320,175,48,2"
But I get this
"Reading: 54,54,54,54,54,54"
"Reading: 140,140,140,140,140,140"
#include <ArduinoJson.h>
void setup() {
Serial.begin(9600);
}
void loop() {
StaticJsonDocument<500> doc;
JsonArray analogValues = doc.createNestedArray("analog");
for (int pin = 0; pin < 6; pin++) {
int value = analogRead(35);
analogValues.add(value);
}
Serial.print(F("Reading: "));
serializeJson(doc, Serial);
Serial.println();
}
Your code will take 7 samples from the input pin very quickly - faster than it's likely you'll be able to change the potentiometer. You need to add a delay between the samples to give the potentiometer time to change. So:
for (int pin = 0; pin < 6; pin++) {
int value = analogRead(35);
analogValues.add(value);
delay(200);
}
would wait 2 tenths of a second between taking samples.
To do some very basic debugging on this you could also confirm that the issues is the samples themselves and not the way you're handling JSON by outputting the sample values as you take them. In your original code this would be:
for (int pin = 0; pin < 6; pin++) {
int value = analogRead(35);
Serial.println(value);
analogValues.add(value);
}
It's also possible that the act of outputting the samples might slow things down enough that you might start to see variation.

Cannot invoke mult(float) on the primitive type float

I'm working on a simple gravity program in Processing. My program takes particles and attracts them to each other based on the formula for gravity. Unfortunately, once I try to multiply the force to the direction with PVector.mult(), I get the error in the title:
Cannot invoke mult(float) on the primitive type float.
Here is my code for the method. G is defined elsewhere.
public float distance(Particle other) {
return location.sub(other.location).mag();
}
public PVector direction(Particle other) {
return location.sub(other.location).normalize();
}
public void gravity(Particle other) {
float grav = (G*((mass * other.mass)/pow(distance(other), 2)));
if(distance(other) != 0) {
acceleration.add(distance(other).mult(grav));
}
Why am I not able to pass a float where a float is due?
Let's take this line apart and split it into multiple steps:
acceleration.add(distance(other).mult(grav));
Here's my attempt to split it into multiple lines:
float grav = 42;
float distanceFromOther = distance(other);
float multipliedValue = distanceFromOther.mult(grav);
acceleration.add(multipliedValue);
Hopefully this makes it more obvious what's going on: you're trying to call mult() on a float value, which won't work. You need to call mult on a PVector or another class that contains a mult() function.

Is there a race condition in the code of this Parallel Forall blogopost?

I've recently stumbled upon this blogpost in the NVIDIA devblogs:
https://devblogs.nvidia.com/parallelforall/accelerating-graph-betweenness-centrality-cuda/
I´ve implented the edge parallel code and it seems to work as intended, however it seems to me that the code works with a race condition "controlled" with __syncthreads.
This is the code (as shown in the blog):
__shared__ int current_depth;
__shared__ bool done;
if(idx == 0){
done = false;
current_depth = 0;
}
__syncthreads();
// Calculate the number of shortest paths and the
// distance from s (the root) to each vertex
while(!done){
__syncthreads();
done = true;
__syncthreads();
for(int k=idx; k<m; k+=blockDim.x) //For each edge...
{
int v = F[k];
// If the head is in the vertex frontier, look at the tail
if(d[v] == current_depth)
{
int w = C[k];
if(d[w] == INT_MAX){
d[w] = d[v] + 1;
done = false;
}
if(d[w] == (d[v] + 1)){
atomicAdd(&sigma[w],sigma[v]);
}
}
__syncthreads();
current_depth++;
}
}
I think there is a race condition just at the end:
__syncthreads();
current_depth++;
I think the program is relying on the race condition so the variable gets increased only by one, instead of by the number of threads. I don't feel like this is a good idea, but in my tests it seems to be reliable.
Is this really safe? Is there a better way to do it?
Thanks.
As the author of this blog post, I'd like to thank you for pointing out this error!
When I wrote this snippet I didn't use my verbatim edge-traversal code as that used explicit queuing to traverse the graph which makes the example more complicated without adding any pedagogical value. Instead I must have cargo-culted some old code and posted it incorrectly. It's been quite a while since I've touched this code or algorithm, but I believe the following snippet should work:
__shared__ int current_depth;
__shared__ bool done;
if(idx == 0){
done = false;
current_depth = 0;
}
__syncthreads();
// Calculate the number of shortest paths and the
// distance from s (the root) to each vertex
while(!done)
{
__syncthreads();
done = true;
__syncthreads();
for(int k=idx; k<m; k+=blockDim.x) //For each edge...
{
int v = F[k];
// If the head is in the vertex frontier, look at the tail
if(d[v] == current_depth)
{
int w = C[k];
if(d[w] == INT_MAX){
d[w] = d[v] + 1;
done = false;
}
if(d[w] == (d[v] + 1)){
atomicAdd(&sigma[w],sigma[v]);
}
}
}
__syncthreads(); //All threads reach here, no longer UB
if(idx == 0){ //Only one thread should increment this shared variable
current_depth++;
}
}
Notes:
Looks like a similar issue exists in the node parallel algorithm on the blog post
You could also use a register instead of a shared variable for current_depth, in which case every thread would have to increment it
So to answer your question, no, that method is not safe. If I'm not mistaken the blog snippet has the additional issue that current_depth should only be incremented once all vertices at the previous depth were handled, which is at the conclusion of the for loop.
Finally, if you'd like the final version of my code that has been tested and used by people in the community, you can access it here: https://github.com/Adam27X/hybrid_BC

LibGDX - given an arraylist of moving circles, how can i check for collision between them?

So I have 3 classes, MyGdxGame, Ball and DetectCollision. MyGdxGame initialises 4 instances of Ball (different colours and speed/direction, all bounce off the sides of the screen) and stores them as an arraylist.
This arraylist is passed through the constructor of DetectCollision:
public class DetectCollisions {
ArrayList<Ball> ball;
public DetectCollisions(ArrayList<Ball> ball) {
this.ball = ball;
start();
}
public void start() {
for(int i=0; i<ball.size();i++) {
...
}
}
Can anyone give me a hint as to where I should go with this? I just want to detect every time a ball collides with another (and eventually I'll have it print the number of collisions).
Any help highly appreciated :)
If you are going to add a lot of objects to the arraylist, use a 2D physics engine to do all the work for you. If you want to continue implementing your own functions, the very basic method to do is to implement a nested loop over the list to check every possible pair in the arraylist. The complexity is O(n^2)
for(int i=0; i<ball.size();i++) {
for(int j=i+1; j<ball.size();j++) {
check(ball.get(i), ball.get(j));
}
}
The method named check checks if given two circles collide or not. Checking two circles' collision is very easy. If the distance between the central points of the circles is smaller than the sum of the radiuses of the circles, then they collide. Check this page for further info about this.
This is very easy to accomplish if you let each Ball have bounds of type Circle.
You can make a circle as follows: Circle ballBounds = new Circle(float x, float y, float radius) This circle can act as the bounds of each ball ultimately allowing you to check if they collide using the Intersector class.
Having each Ball's boundary we can easily loop through the ArrayList<Ball> and check how many collide. For example:
ArrayList<Ball> ball = new ArrayList<Ball>(); // You need to add Balls to this ArrayList
Intersector intersector = new Intersector();
int counter = 0;
for(int i = 0; i < ball.size()-2; i++){
if(intersecor.overlaps(ball.get(i), ball.get(i+1)){
counter++;
}
i++;
}

Problem with constructor - initialization list (c++)

it's a weird problem that I have
I have a very simple constructor that's creates a matrix with no values:
RegMatrix::RegMatrix(const int numRow, const int numCol):
_numRow(numRow),_numCol(numCol),_matrix()
{
}
_matrix is a vector that holds 'Comlex', an object I've created
and VAL(i,j) is #define VAL(i,j) ((i * _numCol) + j)
Now, I call this constructor in function transpose:
RegMatrix RegMatrix::transpose()
{
RegMatrix newMatrix(_numCol,_numRow);
cout << "DIMENSIONS " << newMatrix._numRow << " " << newMatrix._numCol << endl;
for(int j=0; j<_numCol; j++)
{
for(int i=0; i<_numRow; i++)
{
newMatrix._matrix[VAL(i,j)] = _matrix[VAL(j,i)]; //<--SEGMENTATION FAULT
}
}
return newMatrix;
}
And here's my problem: I get a segmentation fault the very first time I enter the second loop. When I use the eclipse debugger I see that _nunRow and _numCol values of newMatrix seem to be garbage (one is '0' the other is -10000000 or something like that). What's even more weird is that I added the output line just to be sure and it gave me the right numbers!
So, any ideas as to what can be my problem?
Thanks!
You are indexing into an empty vector, which is doomed to fail. Use at instead of the subscript operator and you will get an exception.
My guess (based on what you show) is that there may be some problems with how you implement the copy constructor.