Bullet Physics: 'btGeneric6DofSpringConstraint' vs. 'btGeneric6DofSpring2Constraint' to simulate a spring - c++

I am using Bullet Physics to simulate a box fixed two meters above the ground, and another one hanging down from it with a spring (or maybe better a rubberband of equilibrium length 0) simulated by a btGeneric6DofSpringConstraint.
btCollisionShape *shape = createBoxShape(0.2f, 0.2f, 0.2f);
btRigidBody *bodies[] = {
createStaticStatic(shape),
createDynamic(10 * getVolume(shape), shape),
};
bodies[0]->getWorldTransform().setOrigin({ 0.0f, 2.0f, 1.0f });
bodies[1]->getWorldTransform().setOrigin({ 0.0f, 1.0f, 1.0f });
// We can use 'btGeneric6DofSpring2Constraint' instead
btGeneric6DofSpringConstraint *spring = new btGeneric6DofSpringConstraint(
*bodies[0], *bodies[1],
{ 0.0f, -0.2f, 0.0f }, { 0.0f, 0.2f, 0.0f },
true
);
// Disable limits
spring->setLinearLowerLimit({ 1.0f, 1.0f, 1.0f });
spring->setLinearUpperLimit({ -1.0f, -1.0f, -1.0f });
// Enable spring behavior
for (int i = 0; i < 3; ++i)
{
spring->enableSpring(i, true);
spring->setStiffness(i, 35.0f);
spring->setDamping(i, 1.0f);
spring->setEquilibriumPoint(i, 0);
}
world->addConstraint(spring);
Now I enounter the problem that no damping is applied to the spring, no matter what value I use in setDamping. I read that this can be fixed by using btGeneric6DofSpring2Constraint instead (note the 2 after the Spring). But now the spring behaves unpredictable (generates energy, changes direction, etc.) whenever the spring is not attached to the bodies centers of mass (as in the example above).
Has anybody any idea what could be the problem? What is the difference between btGeneric6DofSpringConstraint and btGeneric6DofSpring2Constraint anyway?

Related

C++ GDI: I am seeking explanation for color matrix such that I can create any color manipulation mask

I am trying to implement an application that can manipulate the background screen color attributes through a transparent window. Basically trying to recreate Color Oracle. I am progressing here through C++:GDI+ resources. This GDI has a Color Matrix concept. I am able to create filters for greyscale(as shown in the example in the last hyperlink), brightness tweaking, saturation tweaking; However, as advanced use cases such as color blindness filter, blue light filter, contrast tweaks - I am using the hit-n-trial approach, It will be much efficient if anyone can take me in the right direction to learn fundamentals of this color matrix.
Example Matrix is shown below which boosts saturation by a small factor while restricting brightness component.
MAGCOLOREFFECT magEffectSaturationBoost =
{ { // MagEffectBright
{ 1.02f, 0.0f, 0.0f, 0.0f, 1.0f },
{ 0.0f, 1.02f, 0.0f, 0.0f, 1.0f },
{ 0.0f, 0.0f, 1.02f, 0.0f, 1.0f },
{ 0.0f, 0.0f, 0.0f, 1.0f, 0.0f },
{ -0.1f, -0.1f, -0.1f, 0.0f, 0.0f }
}
}
// Using the Matrix
ret = MagSetColorEffect(hwndMag, &magEffectSaturationBoost);
It will be much efficient if anyone can take me in the right direction to learn fundamentals of this color matrix.
Each color vector is multipled by 5x5 matrix, to make it possible color vector is 5 elements long - the fifth element is a dummy one, this allows to perform additional operations on colors (rotation, scaling, ...).
In your example each color component is multiplied by 1.02f making color brighter, after multiplication - from each color component 0.1 value is subtracted.
You can find full explanation here:
https://learn.microsoft.com/en-us/windows/win32/gdiplus/-gdiplus-using-a-color-matrix-to-transform-a-single-color-use
and:
https://learn.microsoft.com/en-us/windows/win32/gdiplus/-gdiplus-coordinate-systems-and-transformations-about
Few more links:
https://docs.rainmeter.net/tips/colormatrix-guide/
https://www.integral-domain.org/lwilliams/math150/labs/matrixcolorfilter.php

Constructor cannot Initialize one parameter when it exits the function

I am against one problem with my C++ code. I am using the Assimp library, and I have one class that has one attribute of type const aiScene*. In the constructor of the class, I assign this value and all works fine, but when I exit the constructor code, the parameter const aiScene* does not have any data assigned
I paste you here the code.
Model.h
class Model
{
public:
Model(std::string objectLoc, std::string vertexLoc, std::string fragmentLoc,
glm::vec3 position, glm::vec3 scale, glm::vec3 rotation,
Camera* camera);
void render();
~Model();
private:
std::string objectLoc, vertexLoc, fragmentLoc;
Shader* shader;
glm::mat4 model;
glm::vec3 position, scale, rotation;
Camera* camera;
std::vector<Mesh*> meshes;
std::vector<Texture*> textures;
std::vector<unsigned int> meshToTex;
std::unordered_map<std::string, GLuint> boneMapping;
std::vector<glm::mat4> bonesTransformations;
const aiScene* scene;
glm::mat4 globalInverseTransform;
GLuint boneCount;
};
Model.cpp
Model::Model(std::string objectLoc, std::string vertexLoc, std::string fragmentLoc, glm::vec3
position, glm::vec3 scale, glm::vec3 rotation, Camera* camera)
: objectLoc(objectLoc), vertexLoc(vertexLoc), fragmentLoc(fragmentLoc),
position(position), scale(scale), rotation(rotation),
camera(camera),
boneCount(0)
{
shader = new Shader(vertexLoc, fragmentLoc,
DirectionalLight(glm::vec3(1.0f, 1.0f, 1.0f), 0.4f, 0.1f, glm::vec3(0.0f, 0.0f, -1.0f)),
SpecularLight(1.0f, 250.0f),
SpotLight(glm::vec3(1.0f, 1.0f, 1.0f), 0.0f, 1.0f, glm::vec3(0.0f, 0.0f, 0.0f), 1.0f, 0.0f, 0.0f,
glm::vec3(0.0f, 0.0f, -1.0f), glm::radians(10.0f)));
shader->addPointLight(PointLight(glm::vec3(1.0f, 0.0f, 0.0f), 0.2f, 0.2f, glm::vec3(0.0f, 0.0f,
-15.0f), 0.3f, 0.2f, 0.1f));
shader->addPointLight(PointLight(glm::vec3(0.0f, 1.0f, 0.0f), 0.2f, 0.2f, glm::vec3(10.0f, 0.0f,
-15.0f), 0.3f, 0.2f, 0.1f));
shader->addPointLight(PointLight(glm::vec3(0.0f, 0.0f, 1.0f), 0.2f, 1.2f, glm::vec3(-10.0f, 0.0f,
-15.0f), 0.8f, 0.2f, 0.1f));
model = glm::translate(glm::mat4(1.0f), position)
* glm::scale(glm::mat4(1.0f), scale)
* glm::rotate(glm::mat4(1.0f), glm::radians(rotation.x), glm::vec3(1, 0, 0))
* glm::rotate(glm::mat4(1.0f), glm::radians(rotation.y), glm::vec3(0, 1, 0))
* glm::rotate(glm::mat4(1.0f), glm::radians(rotation.z), glm::vec3(0, 0, 1));
Assimp::Importer importer;
scene = importer.ReadFile(objectLoc,
aiProcess_Triangulate |
aiProcess_FlipUVs |
aiProcess_GenSmoothNormals |
aiProcess_JoinIdenticalVertices);
if (!scene)
{
printf("Cannot load model %s: %s\n", objectLoc.c_str(), importer.GetErrorString());
return;
}
aiNode* rootNode = scene->mRootNode;
globalInverseTransform = aiMatrix4x4ToGlm(rootNode->mTransformation.Inverse());
loadNode(rootNode);
loadTextures();
}
I have tried everything, from copy one by one the attributes to the object scene, to make a clone method in all classes involved, but nothing. Even, I have thought that the problem could be the const identifier in the scene attribute, but if I make the object not constant, it works wrongly the same.
I am not modifying that attribute in any of the methods of the Model class.
I paste you here some photos of the problem.
Good result in the constructor
Bad result out of the constructor
If you could help me it would be great.
The documentation says
The returned data is intended to be read-only, the importer object keeps ownership of the data and will destroy it upon destruction.
[Assimp::Importer::ReadFile]
When the constructor is left Assimp::Importer importer; and the object scene is pointing to are destroyed.
You have to make a copy of it before you leave the constructor or
Use GetOrphanedScene() to take ownership of it.
[Assimp::Importer::ReadFile]
aiScene* Assimp::Importer::GetOrphanedScene ( )
Returns the scene loaded by the last successful call to ReadFile() and releases the scene from the ownership of the Importer instance.
The application is now responsible for deleting the scene. Any further calls to GetScene() or GetOrphanedScene() will return NULL - until a new scene has been loaded via ReadFile().
Returns:
Current scene or NULL if there is currently no scene loaded
Note:
Use this method with maximal caution, and only if you have to. By design, aiScene's are exclusively maintained, allocated and deallocated by Assimp and no one else. The reasoning behind this is the golden rule that deallocations should always be done by the module that did the original allocation because heaps are not necessarily shared. GetOrphanedScene() enforces you to delete the returned scene by yourself, but this will only be fine if and only if you're using the same heap as assimp. On Windows, it's typically fine provided everything is linked against the multithreaded-dll version of the runtime library. It will work as well for static linkage with Assimp.
[Assimp::Importer::GetOrphanedScene]

How to make a spring constraint with Bullet Physics?

I want to test the spring contraint of Bullet Physics. So I created a static box hovering above the ground and a second dynamic box hanging down from it. But activating the spring behavior does nothing! The box is indeed hanging freely. I know it because it rotates freely. But it does not oscillate or anything.
btCollisionShape *boxShape = createBoxShape(0.2f, 0.2f, 0.2f);
btRigidBody *box1 = createStatic(boxShape);
btRigidBody *box2 = createDynamic(1.0f /*mass*/, boxShape);
box1->setWorldTransform(btTransform(btQuaternion::getIdentity(), { 0.0f, 2.0f, 1.0f }));
box2->setWorldTransform(btTransform(btQuaternion::getIdentity(), { 0.0f, 1.0f, 1.0f }));
btGeneric6DofSpring2Constraint *spring = new btGeneric6DofSpring2Constraint(
*box1, *box2,
btTransform(btQuaternion::getIdentity(), { 0.0f, -1.0f, 0.0f }),
btTransform(btQuaternion::getIdentity(), { 0.0f, 0.0f, 0.0f })
);
// I thought maybe the linear movement is locked, but even using these lines do not help.
// spring->setLinearUpperLimit(btVector3(0.0f, 0.1, 0.0f));
// spring->setLinearLowerLimit(btVector3(0.0f, -0.1, 0.0f));
// Enabling the spring behavior for they y-coordinate (index = 1)
spring->enableSpring(1, true);
spring->setStiffness(1, 0.01f);
spring->setDamping (1, 0.00f);
spring->setEquilibriumPoint();
What is wrong? I played a lot with the the Stiffness and Damping parameters. But it changed nothing. Setting linear lower and upper limits makes the box movable in the y-direction, but it still not oscillates. And yes, gravity is activated.
Ok, I found a solution by checking out Bullet's provided example projects (could have come up with the idea earlier). Three things I have learned:
The spring constraint will not violate the linear limits. The problem with my former approach was that the linear movement was either locked, or limited to a too small range for the assigned spring stiffness. Now there are no more limits (by setting the lower limit above the upper one).
The stiffness was far too small, so the joined objects were acting as if they were freely movable inside the linear limits. You can check out the values in my code below, I got them from the example project.
There is a small difference in the behavior between btGeneric6DofSpringConstraint and btGeneric6DofSpring2Constraint. The former one seems to violet the non-spring-axes less (x- and z-axes in my case). The latter one seems to apply a stronger damping. But these are just first observations.
btGeneric6DofSpringConstraint *spring = new btGeneric6DofSpringConstraint(
*box1, *box2,
btTransform(btQuaternion::getIdentity(), { 0.0f, -1.0f, 0.0f }),
btTransform(btQuaternion::getIdentity(), { 0.0f, 0.0f, 0.0f }),
true
);
// Removing any restrictions on the y-coordinate of the hanging box
// by setting the lower limit above the upper one.
spring->setLinearLowerLimit(btVector3(0.0f, 1.0f, 0.0f));
spring->setLinearUpperLimit(btVector3(0.0f, 0.0f, 0.0f));
// Enabling the spring behavior for they y-coordinate (index = 1)
spring->enableSpring(1, true);
spring->setStiffness(1, 35.0f);
spring->setDamping (1, 0.5f);
spring->setEquilibriumPoint();

Begun learning OpenGL, need help figuring out this problem

So I have begun learning OpenGL, reading from the book "OpenGL Super Bible 5 ed.". It's explains things really well, and I have been able to create my first gl program myself! Just something simple, a rotating 3d pyramid.
Now for some reason one of the faces are not rendering. I checked the vertecies (plotted it on paper first) and it seemed to be right. Found out if I changed the shader to draw a line loop, it would render. However it would not render a triangle. Can anyone explain why?
void setupRC()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
shaderManager.InitializeStockShaders();
M3DVector3f vVerts1[] = {-0.5f,0.0f,-0.5f,0.0f,0.5f,0.0f,0.5f,0.0f,-0.5f};
M3DVector3f vVerts2[] = {-0.5f,0.0f,-0.5f,0.0f,0.5f,0.0f,-0.5f,0.0f,0.5f};
M3DVector3f vVerts3[] = {-0.5f,0.0f,0.5f,0.0f,0.5f,0.0f,0.5f,0.0f,0.5f};
M3DVector3f vVerts4[] = {0.5f,0.0f,0.5f,0.0f,0.5f,0.0f,0.5f,0.0f,-0.5f};
triangleBatch1.Begin(GL_LINE_LOOP, 3);
triangleBatch1.CopyVertexData3f(vVerts1);
triangleBatch1.End();
triangleBatch2.Begin(GL_TRIANGLES, 3);
triangleBatch2.CopyVertexData3f(vVerts2);
triangleBatch2.End();
triangleBatch3.Begin(GL_TRIANGLES, 3);
triangleBatch3.CopyVertexData3f(vVerts3);
triangleBatch3.End();
triangleBatch4.Begin(GL_TRIANGLES, 3);
triangleBatch4.CopyVertexData3f(vVerts4);
triangleBatch4.End();
glEnable(GL_CULL_FACE);
}
float rot = 1;
void renderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
GLfloat vRed[] = {1.0f, 0.0f, 0.0f, 0.5f};
GLfloat vBlue[] = {0.0f, 1.0f, 0.0f, 0.5f};
GLfloat vGreen[] = {0.0f, 0.0f, 1.0f, 0.5f};
GLfloat vWhite[] = {1.0f, 1.0f, 1.0f, 0.5f};
M3DMatrix44f transformMatrix;
if (rot >= 360)
rot = 0;
else
rot = rot + 1;
m3dRotationMatrix44(transformMatrix,m3dDegToRad(rot),0.0f,1.0f,0.0f);
shaderManager.UseStockShader(GLT_SHADER_FLAT, transformMatrix, vRed);
triangleBatch1.Draw();
shaderManager.UseStockShader(GLT_SHADER_FLAT, transformMatrix, vBlue);
triangleBatch2.Draw();
shaderManager.UseStockShader(GLT_SHADER_FLAT, transformMatrix, vGreen);
triangleBatch3.Draw();
shaderManager.UseStockShader(GLT_SHADER_FLAT, transformMatrix, vWhite);
triangleBatch4.Draw();
glutSwapBuffers();
glutPostRedisplay();
Sleep(10);
}
You've most likely defined the vertices in clockwise order for the triangle that isn't showing, and in counterclockwise order (normally the default) for those that are. Clockwise winding essentially creates an inward facing normal and thus OpenGL won't bother to render it when culling is enabled.
The easiest way to check this is to set glCullFace(GL_FRONT)--that should toggle it so you see the missing triangle and no longer see the other three.
The only thing I see that affects polygons here is glEnable(GL_CULL_FACE);.
You shouldn't have that, because if you plot your vertices backwards, the polygon won't render.
Remove it or actually call glDisable(GL_CULL_FACE); to be sure.
In your case, it's not likely that you want to draw a polygon that you can see from one side only.

Lighting does not work with gluSphere

This is a simple issue that I'm somewhat ashamed to ask for help on.
I'm making a simple call to gluSphere to render a sphere, however, it does not light properly even though I'm pretty sure I added the normals and lighting correctly. If, however, I add a texture, the model lights normally, except it seems to be always SMOOTH, and I cannot change it to flat.
This is the lighting code in my init() function:
gl.glLightfv( GL.GL_LIGHT0, GL.GL_AMBIENT , AMBIENT_LIGHT, 0 );
gl.glLightfv( GL.GL_LIGHT0, GL.GL_DIFFUSE , DIFFUSE_LIGHT, 0 );
gl.glLightfv( GL.GL_LIGHT0, GL.GL_POSITION, light_pos , 0 );
gl.glEnable ( GL.GL_LIGHT0 );
gl.glEnable ( GL.GL_LIGHTING );
this is my sphere code in my display() function:
gl.glColor3d(1.0, 1.0, 1.0);
glu.gluQuadricDrawStyle (quad, GLU.GLU_FILL);
glu.gluQuadricNormals (quad, GLU.GLU_FLAT);
glu.gluQuadricOrientation(quad, GLU.GLU_OUTSIDE);
glu.gluSphere(quad, 1.0, lat, lon);
Please advise.
EDIT:
light values:
public final static float[] DIFFUSE_LIGHT = { 1.0f, 1.0f, 1.0f, 1.0f };
public final static float[] AMBIENT_LIGHT = { 0.3f, 0.3f, 0.3f, 1.0f };
public float[] light_pos = { -2.0f, 2.0f, 10.0f, 0.0f };
added materials, no change:
gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT , new float[]{0.5f, 0.5f, 0.5f, 1.0f}, 0);
gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE , new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 0);
gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, new float[]{0.7f, 0.7f, 0.7f, 1.0f}, 0);
gl.glMaterialf (GL.GL_FRONT, GL.GL_SHININESS, 0.5f);
gl.glMaterialfv(GL.GL_FRONT, GL.GL_EMISSION, new float[]{0.3f, 0.3f, 0.3f, 0.0f}, 0);
EDIT2:
Blah, I figured i had a:
gl.glEnable(GL.GL_TEXTURE_2D);
active somewhere and it was causing my model not to have shading if there was no texture associated with it. -_- carry on good people, carry on.
I encountered lighting problem too and figured out that i need to add glEnable(GL_NORMALIZE).
What kind of lighting are you expecting? The third parameter to glLightfv is supposed to be the values of the light you are setting.
Are there any translations/rotations you do that may affect the position of the light?
Is GL_COLOR_MATERIAL enabled or disabled? It may overwrite your material settings if it is.
Even if your code is not enabling GL_TEXTURE_2D, you should try disabling it manually right before the line is reached, just in case.
If you wish to ignore gluSphere completely, there's some code in this thread that you can use.