Difference between revisions of "Getting started with Newton"

From Newton Wiki
Jump to: navigation, search
 
m (1 revision imported)
 
(No difference)

Latest revision as of 08:02, 10 June 2019

Template:Languages
This is a traduction of my tutorial made in French. It'll take a little bit time to translate all the text, so be patient :)... And of course please correct my mistakes, my English isn't so good. Original version can be read here : [1]

In this first tutorial about Newton Game Dynamics, you'll learn how to use this powerful library in order to create a small application with cubes and spheres. (made with Newton 1.53)


INTRODUCTION

Physics in today's games take more and more importance, because it adds realism and can make new gameplay. But making a physics engine is a hard stuff because it demands strong skills in programming and physics. That's why a lot of physics engine were born these times ! The most well-known is certainly Havok, because it's used in a lot of games made by professionals. But it's really expensive and it's reserved to developpers. But don't worry ! There are a lot of free engines that you can use. For instance, there are ODE, Tokamak, PhysX, and of course Newton Game Dynamics. The most powerful of free physics engine is PhysX by Ageia, but it is more complicated than Newton, and the forum is much less sympathic :). In this tutorial, I have tried to make it as simple as possible. The language used is C++. At the end of that tutorial, you'll obtain one static floor, with cubes and spheres that fall realistically. Don't worry, it's really easy :).


GETTING STARTED

   I) Installation :

The installation of Newton Game Dynamics is really easy. First download the SDK here [1] (http://newtondynamics.com/downloads.html). Install the SDK. You'll find then trois folders : doc (you HAVE TO read it, it contains all the functions of NGD), samples and sdk. If you're using Code::Blocks, copy Newton.h in your include folder, and Newton.dll in C:/WINDOWS/system32 (you'll have to put this dll with ALL of your projects !), and Newton.lib in your lib folder. Now, you just have to linked everything : on Code::Blocks : Settings > Compiler And Debugger > Linker. In Link Librairies, click on Add > and add your Newton.lib. That's made, you can now use Newton !

   ça c'est toi : II) Pré-requis :

Vous devez avoir les bases du C++ et de l'OpenGL (j'utiliserai également GLU pour les sphères). Pour le fenêtrage, j'ai utilisé la SDL, mais rien ne vous empêche d'utiliser GLUT ou l'API Win32. Bien sûr, j'ai utilisé OpenGL, mais vous pouvez utiliser DirectX, NGD n'est pas spécifique à une API. J'ai également fait usage de la bibliothèque SDL_gfx pour gérer efficacement le frame-rate. En effet, cela permet de "bloquer" le frame-rate, ainsi le programme marche de la même façon quelque soit l'ordinateur (sauf si évidemment l'ordinateur est trop peu puissant pour atteindre ce frame-rate). Pour plus d'informations sur comment utiliser cette bibliothèque (c'est très facile et très utile !), ou pour passer par une autre méthode, je vous renvoie vers l'article de fearyourself : fr voir ici

Concernant la physique, vous n'avez pas besoin d'être un crack, mais avoir déjà entendu parler des forces est bien entendu un plus. Pour ceux qui ignoreraient totalement ce concept ou qui auraient oublié leurs cours de seconde, sachez qu'une force est un phénomène physique. Pour rester simple, chaque corps subit plusieurs forces qui attirent ce corps, ce qui provoque une modification de la vitesse de l'objet (par exemple, le simple fait de lâcher un stylo fait intervenir plusieurs forces, la force gravitationnelle de la Terre attire le stylo vers le sol). Pour plus d'informations sur les forces, voici un article très complet et bien fait : fr http://fr.wikipedia.org/wiki/Force_(physique)


WRITING THE CLASSES

CVECTOR Class 1) CVector.h


#ifndef CVECTOR_H
#define CVECTOR_H
#include <GL/GL.h> // I include GL.h in order to use GLfloat
class CVector
{
 public:
    CVector ();
    CVector (const GLfloat fX, const GLfloat fY, const GLfloat fZ);
    virtual ~CVector();
    void SetCoordinates (const GLfloat fX, const GLfloat fY, const GLfloat fZ);
    GLfloat x, y, z;
};
#endif // CVECTOR_H

Here is the header of our class CVector. It's a very simple vector class. Of course, if you create a real application, you'll have to make a better class. But it'll be good enough for us.

Our first CVector's constructor doesn't take any parameters, whereas the second takes three coordinates (x, y, z !). SetCoordinates is simply used to change the coordinates of a vector. So it allows to use the same CVector object without create another one.

And finally, the three variables, that are declared public, just for the simplicity ^^.


2) CVector.cpp

#include "CVector.h"
CVector::CVector ()
 : x (0.0f), y (0.0f), z (0.0f)
{
 // Ne fait rien
}
CVector::CVector (const GLfloat fX, const GLfloat fY, const GLfloat fZ)
 : x (fX), y (fY), z (fZ)
{
 // Ne fait rien
}
CVector::~CVector()
{
 // Ne fait rien
}
void CVector::ReglerCoordonnees (const GLfloat fX, const GLfloat fY, const GLfloat fZ)
{
 x = fX;
 y = fY;
 z = fZ;
}

Okej, nothing complicated here :)/


OBJECT Class 1) Object.h Here is the class called Object. This is a pure virtual class. First we include some headers :

#include <iostream>
#include <GL/GL.h>
#include <GL/glu.h>
#include <Newton/Newton.h> // Newton Engine :)
#include "CVector.h"

Note that if your Newton's header is not located in the same place as me, you'll have to change it :).

We also need a small matrix structure, that we define just after that :

// Structure matrix
struct matrix
{
   GLfloat matrice [4][4];
};

We'll be using OpenGL matrices, so 4*4. Matrices with physics will be very usual, to store translations, rotations, scaling,... So, each object will have his own matrice, which will be modified by Newton each time his position will change.


Now we're gonna to define our Object class. Here it is :

class Objet
{
// A friend function that'll be used by Newton Game Dynamics
friend void ApplyForceAndTorqueCallback (const NewtonBody * nBody);
  public:
     Objet (); // Constructeur
     virtual ~Objet (); // Destructeur
     virtual void Initialize () {};
     virtual void Draw () = 0; // Classe pure
     virtual void SetColor (const CVector &) = 0;
  protected:
     NewtonBody * m_pBody; // A pointer to a NewtonBody
     GLfloat m_masse; // Mass of the object
     CVector m_color; // Color of the object
};

It's really not complicated, but maybe you are a little surprised. In fact, to simplify the source code, we'll be using a friend function called ApplyForceAndTorqueCallback, with only one parameter : a pointer to a NewtonObject object. Yes, Newton uses callbacks ! :). In fact, it's just a function that'll be called each time a body has moved, unlike the body is inactive or have reached a state of stable equilibrium.

We have then a constructor, and a virtual destructor. Then a virtual function, Initialize, a virtual function called Draw and another function, SetColor, that takes a reference to a CVector object. It'll be very usual because a vector can stores 3 values (x, y, z), but it'll be used here as three colors (Red, Green, Blue).

At least, we have three members variables (be careful to make them protected, not private !) : a pointer of a NewtonBody object : m_pBody. EACH object MUST have his own NewtonBody variable ! Then a float that represents the mass of the object, and finally an object call m_color;