Nehe OpenGL Tutorial Lesson 39th: Physical Simulation

Source: Internet
Author: User

Go from "translation" Nehe OpenGL tutorial

Objective

Statement, this Nehe OpenGL tutorial series of articles by 51 blog yarin Translation (2010-08-19), this blog for reprint and a little collation and modification. Thank you for compiling Nehe's OpenGL pipeline tutorial, as well as yarn translation finishing.

Nehe OpenGL Lesson 39th: Physical Simulations

Introduction to Physical simulations:

Remember high school physics, linear motion, free fall, spring. In this lesson, we will create all this.

Introduction to Physical simulation

If you are familiar with the laws of physics and want to implement it, this article is perfect for you.

In this tutorial, you will create a very simple physics engine, and we will create the following classes:

Content:

Location class
* Class Vector3D---> Classes used to record three-dimensional coordinates of an object

Force and movement
* Class Mass---> Represents the physical properties of an object

Simulation class
* Class Simulation---> Simulated physical laws

Analog Uniform Motion
* Class Constantvelocity:public Simulation---> Analog uniform Motion

Simulation of motion under the action of force
* Class Motionundergravitation:public Simulation---> Simulation motion under the action of gravity
* Class Massconnectedwithspring:public Simulation---> Simulation movement under the action of spring

Class Mass
{
Public
float m; Quality
Vector3D POS; Position
Vector3D vel; Speed
Vector3D Force; Force

Mass (float m)//constructor
{
This->m = m;
}

...

The following code adds a force to the object, which at the initial time is 0.

void Applyforce (Vector3D force)
{
This->force + = Force; Add a force
}

void init ()//initially set to 0
{
force.x = 0;
force.y = 0;
force.z = 0;
}

...

The following steps complete a simulation:

1. Set Force
2. Apply external forces
3. Calculate the position and velocity of the object according to the time of the force

void simulate (float DT)
{
Vel + = (force/m) * DT; Update speed

pos + = vel * DT; Update location

}

How the Simulation class works:

In a physical simulation, we simulate with the following rules, set the force, update the position and speed of the object, and simulate it again and again by time. Here is the implementation code for it:

Class Simulation
{
Public
int numofmasses; Number of objects
mass** masses; Pointer to object structure

Simulation (int numofmasses, float m)//constructor
{
this->numofmasses = numofmasses;

Masses = new mass*[numofmasses];

for (int a = 0; a < numofmasses; ++a)
Masses[a] = new Mass (m);
}

virtual void release ()//release of all objects
{
for (int a = 0; a < numofmasses; ++a)
{
Delete (Masses[a]);
Masses[a] = NULL;
}

Delete (masses);
masses = NULL;
}

mass* getmass (int index)
{
if (Index < 0 | | index >= numofmasses)//Return to the first object
return NULL;

return Masses[index];
}

...

(Class Simulation continued)

virtual void init ()//Initialize all objects
{
for (int a = 0; a < numofmasses; ++a)
Masses[a]->init ();
}

virtual void Solve ()//virtual function to set the individual forces applied to each object in a specific application
{

}

virtual void simulate (float dt)//Let all objects simulate one step
{
for (int a = 0; a < numofmasses; ++a)
Masses[a]->simulate (DT);
}

...

The entire simulated part is encapsulated in the following function

(Class Simulation continued)

virtual void operate (float DT)//Complete simulation process
{
Init (); Set Force to 0
Solve (); should be forced
Simulate (DT); Simulation
}
};

Now that we have a simple physics simulation engine that contains objects and simulates two classes, let's create three concrete mock objects based on them:

1. Objects with constant speed
2. Objects with constant acceleration
3. An object with a force inversely proportional to the distance

Control a mock object in the program:

Before we write a specific simulation class, let's look at how to simulate an object in the program, in this tutorial, the simulation engine and the operation simulation of the program in two files, in the program we use the following function, operation simulation:

void Update (DWORD milliseconds)//Perform impersonation

This function is updated at the beginning of each frame and the parameters are time intervals.

void Update (DWORD milliseconds)
{
...
...
...

float dt = milliseconds/1000.0f; Convert to Seconds

DT/= Slowmotionratio; Divided by the simulation factor

timeelapsed + = DT; Update the time lost

...

In the following code, we define a processing interval that is not so long, so that the physics engine simulates one time.

...

float Maxpossible_dt = 0.1f; Set the simulation interval

int numofiterations = (int) (DT/MAXPOSSIBLE_DT) + 1; Calculate the number of simulations in a lost time
if (numofiterations! = 0)
DT = dt/numofiterations;

for (int a = 0; a < numofiterations; ++a)//simulate them
{
Constantvelocity->operate (DT);
Motionundergravitation->operate (DT);
Massconnectedwithspring->operate (DT);
}
}

Let's write two specific simulation classes:

1. Objects with constant speed
* Class Constantvelocity:public Simulation---> Simulates a Uniform motion object

Class Constantvelocity:public Simulation
{
Public
Constantvelocity (): Simulation (1, 1.0f)
{
Masses[0]->pos = Vector3D (0.0f, 0.0f, 0.0f); Initial position is 0
Masses[0]->vel = Vector3D (1.0f, 0.0f, 0.0f); Move right
}
};

Let's create an object with constant acceleration:

Class Motionundergravitation:public Simulation
{
Vector3D gravitation; Acceleration

Motionundergravitation (Vector3D gravitation): Simulation (1, 1.0f)//constructors
{
This->gravitation = gravitation; Set acceleration
Masses[0]->pos = Vector3D ( -10.0f, 0.0f, 0.0f); Set the position to the left-10 place
Masses[0]->vel = Vector3D (10.0f, 15.0f, 0.0f); Set speed to top right
}

...

The following function sets the force applied to the object

virtual void Solve ()//Set Current force
{
for (int a = 0; a < numofmasses; ++a)
Masses[a]->applyforce (gravitation * masses[a]->m);
}

The following class creates an object that is subjected to a force proportional to the distance:

Class Massconnectedwithspring:public Simulation
{
Public
float springconstant; Elasticity coefficient
Vector3D Connectionpos; Connection direction

Massconnectedwithspring (float springconstant): Simulation (1, 1.0f)//constructors
{
This->springconstant = springconstant;

Connectionpos = Vector3D (0.0f, -5.0f, 0.0f);

Masses[0]->pos = Connectionpos + Vector3D (10.0f, 0.0f, 0.0f);
Masses[0]->vel = Vector3D (0.0f, 0.0f, 0.0f);
}

...

The following function sets the force that the current object is subjected to:

virtual void Solve ()//Set Current force
{
for (int a = 0; a < numofmasses; ++a)
{
Vector3D Springvector = masses[a]->pos-connectionpos;
Masses[a]->applyforce (-springvector * springconstant);
}
}
Original source code and version of the download:

http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=39

Nehe OpenGL Tutorial Lesson 39th: Physical Simulation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.