Remembering the past to build the future

" Remembering the past to build the future "

Wednesday, 16 July 2014

Physics : Cloth with particles

This demo shows how you can use the Verlet Integration to build a cloth with particles. For understand the theory behind the code, visit the site

Müller-Fischer et al.
Real-time physics

And thanks to the material from the site

The demo project has the following structure:

The main class : phys_sim.cpp. In this class you will find 2 methods, the first one

void BuildCloth(int nbParticlesX, int nbParticlesY);

makes a cloth, while the second one

void BuildRope(int nbParticles);

makes a rope. Use the method:

void initGL(int width, int height) 
glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse_0);
glLightfv (GL_LIGHT0, GL_POSITION, light_positio_0);



reshape(width, height);

glClearColor(0.126f, 0.126f, 0.128f, 1.0f);



for drawing a cloth or  a rope. The physics engine is realized through the following C++ sources:


Here it is a video demo:

Monday, 14 July 2014

Picking : Click and move an object

In this demo we have a cube in the scene. If you click on the cube, the cube changes its color to yellow:
the cube is now selected. Starting from a cube selected, a click on the plane moves the cube in the position where we have clicked. This video shows some examples:

The demo uses the unProject method as in the  Picking post . You will find in the code some modification for drawing the cube in a different position on the plane.

Download the source here:

C Sources

Wednesday, 2 July 2014

Object Picking

In this post I want to share an easy implementation of picking through unProject method. The demo is realized in C, so we have only the main.cpp file and vector3.h file. In the demo we have a cube in the center of the scene, when we click on the cube, the cube changes its color.

If you open the main.cpp source file you can note the following lines of code:

GLdouble posX, posY, posZ;
GLdouble cubeX=0,cubeY=0.5,cubeZ=0;
GLdouble cubeSize=1.f;
bool cubePicked;

The variables posX,posY and posZ give us the  unproject mouse click coordinates.
The variables cubeX,cubeY and cubeZ save the cube position.
The variable cubSize set the size of the cube;
The boolean variable cubePick is true when the cube is clicked;

When we click in the scene, the unProject method is called:

void unProject(){

GLint viewport[4]; // Where The Viewport Values Will Be Stored
glGetIntegerv(GL_VIEWPORT, viewport); // Retrieves The Viewport Values (X, Y, Width, Height)

GLdouble modelview[16]; // Where The 16 Doubles Of The Modelview Matrix Are To Be Stored
glGetDoublev(GL_MODELVIEW_MATRIX, modelview); // Retrieve The Modelview Matrix

GLdouble projection[16];  // Where The 16 Doubles Of The Projection Matrix Are To Be Stored
glGetDoublev(GL_PROJECTION_MATRIX, projection); // Retrieve The Projection Matrix

GLfloat winX, winY, winZ;               // Holds Our X, Y and Z Coordinates

winX = (float)mouse2D.x;                  // Holds The Mouse X Coordinate
winY = (float)mouse2D.y;                  // Holds The Mouse Y Coordinate

glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);

gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

        //ROUND 2 DECIMAL
posX= ((int)(posX * 10 + .5) / 10.0);
posY= ((int)(posY * 10+ .5) / 10.0);
posZ= ((int)(posZ * 10 + .5) / 10.0);


the method saves posX,posY and posZ variables. After the pickingCheck method is called:

void pickingCheck(){

if( posX>=cubeX-(cubeSize/2) && posX<=cubeX+(cubeSize/2)){
if( posY>=cubeY-(cubeSize/2) && posY<=cubeY+(cubeSize/2) ){
if( posZ>=cubeZ-(cubeSize/2) && posZ<=cubeZ+(cubeSize/2)){


the method checks the posX,posY,posZ range values. If this values are inside the cube, the cube is picked.

Download the code from here:


My Android Games, much are free