SlideShare una empresa de Scribd logo
1 de 65
Descargar para leer sin conexión
Hello




 Apollo

    Diederick Huijbers
diederick@apollomedia.nl
   www.apollomedia.nl
This presentation is part of the openFrameworks
freakDays meetings which are given by a group of
    Creative Coders and artists in Amsterdam.

    During these monthly meetings we give
presentation on innovative techniques and show
           projects we’re involved in.

For more information checkout www.roxlu.com or
          contact us at info@roxlu.com
c++     templates   Physics




Cloth
Basic C++
  structs
  classes
  pointers
templates<>
... use the keyword “struct”



  struct Particle {
    float x;
    float y;
    float mass;
  }



                      .. green means
                      “members are
                      accessible”..    Particle

                                         x
                                         y
                                        mass
... use the keyword “class”




  class Particle {
    float x;
    float y;
    float mass;
  }



                         .. red means
                         “members are        Particle
                         NOT accessible”..
                                               x
                                               y
                                              mass
Differences:
  Struct members are
public by default those of
       a class not.
Check how the keyword                    A struct doesn’t need this
              public makes the members                 public keyword. Their
              green ;) and so accessible!              members are public by
                                                       default! That’s nice!



class Particle {                   class Particle {    struct Particle {
  float x;                         public:               float x;
                                     float x;            float y;
  float y;                           float y;
  float mass;                        float mass;         float mass;
}                                  }                   }




   Particle                                 Particle           Particle

      x                                       x                    x
      y                                       y                    y
     mass                                    mass                 mass
When to use struct <-> class?
  It doesn’t really matter;
   it’s a personal thing....
...though, typically structs
  are used for simple data
  containers and classes
for more advanced things.
Classes for more advanced stuff

class Particle {
public:
  Particle();
  void setPosition(float x, float y);
  void update();
  void draw();
  void applyRepulsiveForce();
  void addForce(float x, float y);
private:
  float x;
  float y;
  float mass;
}
Structs for simple data containers


          struct User {
            string name;
            string password;
            string homepage;
            int age;
            float weight;
          };
          struct Tweet {
            uint16 userid;
            string homepage;
            string avatar_url;
            int timestamp;
            bool has_gps;
          };
Pointers !!!
Pointers are not that hard!
 Instead of pointing to a
      value hold a
   a memory address!
Pointers are not that hard
          Values   Address in memory
            f      #1
            r      #2
            e      #3
            a      #4
            k      #5
            d      #6
            a      #7
            y      #8
char* name = “freakday”



                                        Values            Address in memory
                                           f        #1                        name + 0
char *                                     r        #2                        name + 1
Check the char* the star (*)
tells the compiler we want a               e        #3                        name + 2
pointer a char. A normal char
is just one character. The 0 is           a        #4                        name + 3
automatically added and tells
the compiler it’s the end of the           k        #5                        name + 4
“string”
A pointer points hold an
                                           d        #6                        name + 5
memory address. We can
step through the memory                    a        #7                        name + 6
addresses using this type
of variable.                               y        #8                        name + 7

                                          0        #9                        name + 8

                                           ?        #10
                                           ?        #11
Values         Do not go into unauthorized
  f
  r
         #1
         #2
                  memory ....or else.....
  e      #3     “EXC_BAD_ACCESS” or
  a      #4
  k      #5     “UNDEFINED” behavior
  d      #6
  a      #7         ...which is ....
  y      #8
  0     #9
  ?      #10           name + 9

  ?      #11           name + 10
Check XCode project
“freakday_S03E02_pointers”
Memory Alignment + packing

                                                                  32 bit wide




                                                                  }
When the CPU reads from
memory it uses a natural       class Particle {                   enabled
alignment which must be          bool enabled; //       8 bits
respected to permit the CPU      float x;      //       32 bits      x
to read and write memory         float y;      //       32 bits      y
effectively. A compiler will     bool flying; //        8 bits
leave holes in memory when                                        flying
members/objects aren’t         }
aligned properly.




             = memory needed                                      WRONG!
                               = memory actually used              You should align
                                                                   your bytes nicely
Memory Alignment + packing

                                               32 bit wide




                                           }
  class Particle {
    float x;      //        32 bits                 x
    float y;      //        32 bits                 y
    bool flying; //         8 bits         f    e
    bool enabled; //        8 bits
  }




= memory needed                                         CORRECT!
                  = memory actually used                The members are
                                                        aligned nicely
Memory Alignment + packing

              Check XCode project
         “freakday_S03E02_packing”
Templates
 [TYPE_ADDRESS]: some value
[ADDRESS_ENTRY]: some value
   [TYPE_PHONE]: some value
Templates
Office address: some value
Billing address: some value
Mobile phone: some value
 Work phone: some value
Templates
Templates are a way to write more generic code: write
one special thing (algorithm) which you can use for
many kind of types.

Create one template for a particle for both 2D and 3D.
Particle<2D> and Particle<3D>
2D particles....
Particle

  class Particle2D {
    ofVec2f position;
    ofVec2f velocity;
    ofVec2f forces;
    void draw();
  }



Particle-Group

  class 2DParticlesGroup {
    vector<Particle2D*> particles;
    void addForce(ofVec2f f) {
       for(int i = 0; i < particles.size(); ++i) {
          particles.at(i)->addForce(f);
       }
    }
  }
3D particles....
Particle


  class Particle3D {
    ofVec3f position;
    ofVec3f velocity;
    ofVec3f forces;
    void draw();
  }


Particle-Group


  class 3DParticlesGroup {
    vector<Particle3D*> particles;
    void addForce(ofVec3f f) {
       for(int i = 0; i < particles.size(); ++i) {
          particles.at(i)->addForce(f);
       }
    }
  }
4D particles....
Particle

  class Particle4D {
    ofVec4f position;
    ofVec4f velocity;
    ofVec4f forces;
    void draw();
  }


Particle-Group

  class 4DParticlesGroup {
    vector<Particle4D*> particles;
    void addForce(ofVec4f f) {
       for(int i = 0; i < particles.size(); ++i) {
          particles.at(i)->addForce(f);
       }
    }
  }
5D, 6D, 7D ... or template
 Add this before your template class:
 “template<typename T>”.

The “T” is the type parameter: Particle2D, Particle3D, etc..
The name “T” can be anything you like, but T is convention.

Also correct:
template<typename SomeType>
template<class SomeType>
template<class T>

“Typename” and “class” can be used both, “typename”
is preferred.
Templated Particle


  template<typename T>
  class Particle {
  public:
    Particle(T oPosition, float nMass);
    void addForce(T oForce);
    void update();
    void draw();
  private:
    T position;
    T forces;
    T velocity;
    float mass;
  };
Templated Particle-Group


  template<typename P, typename V>
  class ParticleGroup {
  public:
    ParticleGroup();
    void addParticle(P oParticle);
    void addForce(V oForce);
    void update();
    void draw();
  private:
    vector<P> particles
  };
Example of templated particles


  template<typename P, typename V>
  class ParticleGroup {
  public:
    ParticleGroup();
    void addParticle(P oParticle);
    void addForce(V oForce);
    void update();
    void draw();
  private:
    vector<P> particles
  };



       Check XCode project
  “freakday_S03E02_templates”
Keyword Typename
Consider the following code:


   template<typename T>
   class SomeClass {
   public:
     typename T::SubType* ptr;
   };




 Makes sure that subtype is
 seen as a type, not a static
 member.
class WithStatic {                class WithType {
public:                           public:
   static int MyType;                class MyType {
}                                    }
                                  }

                                     Here T::MyType * ptr
    WRONG: Here T::MyType * ptr      would refer to the nested type
    would mean a multiplication      in the WithType class.




template<typename T>              template<typename T>
class SomeClass {                 class SomeClass {
public:                           public:
                                    typename T::MyType* ptr;
  T::MyType   * ptr;              };
};
When to use the keyword typename

  -- pseudo code --
  template<typename T>
  class MyContainer {
     vector<T> items;
     void draw() {
        typename vector<T>::iterator it = items.begin();
        while(it != items.end()) {
           (*it)->draw();
           ++it;
        }
     }
  };

  -- add some particles to container
  MyContainer<Particle2D*> particles;
  particles.items.push_back(new Particle2D());
  particles.items.push_back(new Particle2D());
  particles.draw();




  Here we make sure that
  iterator is a type, not a static
  member! Usually you use the
  keyword typename with STL
  iterators
Physics
Newtons first law
 An object continues with
constant velocity unless a
   force acts upon it[1]
Newtons first law
    Note that the red arrow stays the same size. The red arrow
     represents the velocity vector and showing the first law

                     Velocity vector(s):
                     ofxVec2f, ofxVec3f




time and position

1           2           3               4   5           6        7
            Check XCode project
    “freakday_S03E02_5_simple_motion”
When creating particle systems I often simulate
    drag by degreasing the velocity by a small percentage.
       Note how the velocity vector below get’s smaller.



// example faking drag                             The amount of drag, the 0.98
void Particle::update() {                          in this example, totally
 velocity *= 0.98;                                 depends on what you want.
                                                   It’s a case of tweaking to get
}                                                  the right behavior.




time and position

1          2           3           4          5          6               7
                  Check XCode project
     “freakday_S03E02_6_simple_motion_with_drag”
Newtons second law
A force acting on a object
produces acceleration that
   is proportional to the
      object’s mass.
                   [1]
Newtons second law

                                              this is the resulting force
this arrow represents the
                                              when mass is taken into
force acting on the object
                                              account




                             160-200 KG (?)
Newtons second law

                                        this is the resulting force
this arrow represents the
                                        when mass is taken into
force acting on the object
                                        account




                             75-80 KG
Newtons second law
     The math of how force,
acceleration and mass is related....
     “The law” ( F = ma )

          force = mass * acceleration

     ...to get the acceleration we do:


        acceleration = 1/mass * force
                  this is what I use in particle systems
Newtons second law
     This one is important!
The bigger the mass, the less the
          acceleration

             ration = 1/mass * force
       accele
                       This is what we call inverse mass.
                       We do not need to recalculate this
                       for each update! We store this in a
                       member of the Particle class and
                       can be used to represent “infinite”
                       mass when set to zero:
                       0 * force = 0
How does this look like in code?

  class Particle {
     ofxVec2f position;
     ofxVec2f velocity;
     ofxVec2f forces;
     float inverse_mass;
       Particle(float fMass) {
           if(mass <= 0) {
              inverse_mass = 0;
           }
           else {
              inverse_mass = 1/mass;
           }
       }
       void addForce(ofxVec2f oForce) {          D’Alembert’s Principle (simplified):
          forces += oForce;                      We can accumulate all forces acting
       }                                         on an object.

       void update() {
           forces *= inverse_mass;
           velocity += forces;
           velocity *= 0.99;
           position += velocity;
           forces.set(0,0);
       }
  };
                                                   Check XCode project
                                     “freakday_S03E02_7_simple_motion_inverse_mass”
Springs
 “Springs are structural
  elements that, when
connected between two
objects, apply equal and
opposite forces to each
         object.”
Springs




Object 1                                        Object 2




           The red arrows represent force
           vectors in opposite directions: so
           the two objects are pulled
           towards each other
Springs

      The rule related to springs
     forces is called “Hooks” law!
                 f = -k∆L

-k = spring strength; in code just a
“float strength” which often gets a
value like 0.1, 0.001


∆L = difference between springs
“compressed/rest length” and current
length.
∆L = (r - d)
                 difference between springs
                “compressed/rest length” and
                        current length.
Two springs not
stretched/compressed
and showing their rest
length.
                         }
                              r   = rest length
∆L = (r - d)
                                                  Two particles connected by a spring


                      (r) rest length




Spring force vector                     Spring force vector




                       (d) distance
Spring force vector           Rest length              Spring force vector




                                                 (d) distance




Spring template 2d/3d/..

  template<typename P, typename V>
  class Spring {
  public:
  ! Spring(P pParticleA, P pParticleB, float nStrength = 0.01)
  ! :a(pParticleA)
  ! ,b(pParticleB)
  ! ,k(nStrength)
  ! {                                                                           Rest length: distance
  ! !     rest_length = (b->position - a->position).length();                  between particles when
  ! }                                                                            creating the spring
  !   void   update() {
  !   !      V dir = b->position - a->position;
                                                            Current
  !   !      float dist = dir.length();                     distance
  !   !      if(dist == 0.0) {
  !   !      !   dist = 0.0001; // division by zero
  !   !      }
  !   !
  !   !      float f = (rest_length - dist) * k;            The force spring
  !   !      dir.normalize();
  !   !      a->addForce(dir * -f);
  !   !      b->addForce(dir * f);
  !   }
  !
  !   P a,b;
                       Force spring multiplied by normalized direction
                              vector and in opposite directions
Springs
Example: click mouse to add particle + spring...

                    Check XCode project
            “freakday_S03E02_8_spring_forces”




 Example: right click mouse to add particle +
  spring and use sliders to see the result of
       different “k” and “drag” values.

                  Check XCode project
      “freakday_S03E02_9_spring_forces_with_gui”
Cloth Physics
Cloth Physics
Simple cloth physics used
multiple springs connected
   to particles in a grid.
Cloth Physics

        Particles are positioned on
        a grid and then connected by 3
        kind of springs:

        - structural springs
        - shear springs
        - bending springs
Cloth Physics
Structural springs
Cloth Physics
Shear springs
Cloth Physics




Bending springs
Check XCode project “freakday_S03E02_10_cloth”
                     re )
                 s ul :
               or tif
                   a
             rr u
            E ea
             b
Creating the particle grid


 // lets create the grid.
 for(int i = 0; i < nCols; ++i) {
 ! for(int j = 0; j < nRows; ++j) {
 ! ! ofxVec3f position(i * nSize, j * nSize);
 ! ! Particle3D* particle = new Particle3D(position, 1.1);
 ! ! particles[j * cols + i] = particle;
 ! ! if(j == 0) {
 ! ! ! particle->inverse_mass = 0;
 ! ! }
 ! }
                             Calculating the index in the vector. We position the
 }                           particle just like normal image pixels are stored, by
                                         column and row. This is handy when we need to
                                         lookup (see getAt) a particle based on a column
                                                             and row.
Creating the structural springs


  // create springs.                                       For more info on
  Particle3D* a;                                           how to create the
  Particle3D* b;
  for(int i = 0; i < nCols; ++i) {
                                                           other springs see
  ! for(int j = 0; j < nRows; ++j) {                       the source; it’s
  ! !                                                      quite similar.
  ! !    // structural spring - right.
  ! !    if(i < nCols-1) {
  ! !    !   a = getAt(i,j);!
  ! !    !   b = getAt(i+1, j);
  ! !    !   springs.createSpring(a,b);
  ! !    }
  ! !
  ! !    // structural spring - down
  ! !    if(j < nRows-1) {
  ! !    !   a = getAt(i,j);
  ! !    !   b = getAt(i, j+1);
  ! !    !   springs.createSpring(a,b);
  ! !    }
  ! }
  }




       Check XCode project “freakday_S03E02_11_with_gui”
Next time C++/...:

Initializer lists
Virtual functions
Inheritance
More templates
.....
Debugging linker errors (file, nm)
Operator overloading (=,[],*,etc.)
Const, static
Next time particles/3d

  Integrators
  (euler, verlet, runge kutta)

  Fast particle systems
  (buckets, sorting)

  Billboarding
  Matrices / 3D
  Shaders
Other suggestions...

....
References
1. Ian Millington, Game Physics Engine Development, 2007

Más contenido relacionado

La actualidad más candente

Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007Guillaume Laforge
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++Mohamad Al_hsan
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with pythonArslan Arshad
 
Object-Oriented Programming with C#
Object-Oriented Programming with C#Object-Oriented Programming with C#
Object-Oriented Programming with C#Svetlin Nakov
 
Scala overview
Scala overviewScala overview
Scala overviewSteve Min
 
JavaScript OOPS Implimentation
JavaScript OOPS ImplimentationJavaScript OOPS Implimentation
JavaScript OOPS ImplimentationUsman Mehmood
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 
Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013rivierarb
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonSujith Kumar
 
Learn Ruby by Reading the Source
Learn Ruby by Reading the SourceLearn Ruby by Reading the Source
Learn Ruby by Reading the SourceBurke Libbey
 
Declarative Name Binding and Scope Rules
Declarative Name Binding and Scope RulesDeclarative Name Binding and Scope Rules
Declarative Name Binding and Scope RulesEelco Visser
 
Ios development
Ios developmentIos development
Ios developmentelnaqah
 
Java core - Detailed Overview
Java  core - Detailed OverviewJava  core - Detailed Overview
Java core - Detailed OverviewBuddha Tree
 

La actualidad más candente (20)

Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007
 
About Python
About PythonAbout Python
About Python
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++
 
OO in JavaScript
OO in JavaScriptOO in JavaScript
OO in JavaScript
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
 
Object-Oriented Programming with C#
Object-Oriented Programming with C#Object-Oriented Programming with C#
Object-Oriented Programming with C#
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 
Scala overview
Scala overviewScala overview
Scala overview
 
JavaScript OOPS Implimentation
JavaScript OOPS ImplimentationJavaScript OOPS Implimentation
JavaScript OOPS Implimentation
 
Classes1
Classes1Classes1
Classes1
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013Ruby object model at the Ruby drink-up of Sophia, January 2013
Ruby object model at the Ruby drink-up of Sophia, January 2013
 
Sonu wiziq
Sonu wiziqSonu wiziq
Sonu wiziq
 
Ruby Internals
Ruby InternalsRuby Internals
Ruby Internals
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
Learn Ruby by Reading the Source
Learn Ruby by Reading the SourceLearn Ruby by Reading the Source
Learn Ruby by Reading the Source
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Declarative Name Binding and Scope Rules
Declarative Name Binding and Scope RulesDeclarative Name Binding and Scope Rules
Declarative Name Binding and Scope Rules
 
Ios development
Ios developmentIos development
Ios development
 
Java core - Detailed Overview
Java  core - Detailed OverviewJava  core - Detailed Overview
Java core - Detailed Overview
 

Similar a openFrameworks freakDay S03E02 Diederick Huijbers - C++/Physics/Cloth Animation/Templates

Rust tutorial from Boston Meetup 2015-07-22
Rust tutorial from Boston Meetup 2015-07-22Rust tutorial from Boston Meetup 2015-07-22
Rust tutorial from Boston Meetup 2015-07-22nikomatsakis
 
Building DSLs with Xtext - Eclipse Modeling Day 2009
Building DSLs with Xtext - Eclipse Modeling Day 2009Building DSLs with Xtext - Eclipse Modeling Day 2009
Building DSLs with Xtext - Eclipse Modeling Day 2009Heiko Behrens
 
Neurotech Solutions Ltd: Рекомендации по Stage3D: выбор наиболее подходящего ...
Neurotech Solutions Ltd: Рекомендации по Stage3D: выбор наиболее подходящего ...Neurotech Solutions Ltd: Рекомендации по Stage3D: выбор наиболее подходящего ...
Neurotech Solutions Ltd: Рекомендации по Stage3D: выбор наиболее подходящего ...DevGAMM Conference
 
Back to the Future with TypeScript
Back to the Future with TypeScriptBack to the Future with TypeScript
Back to the Future with TypeScriptAleš Najmann
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developementfrwebhelp
 
Review constdestr
Review constdestrReview constdestr
Review constdestrrajudasraju
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogramprincepavan
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogramprincepavan
 
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...Tudor Dragan
 
Mysterious c++
Mysterious c++Mysterious c++
Mysterious c++xprayc
 
Why we cannot ignore Functional Programming
Why we cannot ignore Functional ProgrammingWhy we cannot ignore Functional Programming
Why we cannot ignore Functional ProgrammingMario Fusco
 
Pharo, an innovative and open-source Smalltalk
Pharo, an innovative and open-source SmalltalkPharo, an innovative and open-source Smalltalk
Pharo, an innovative and open-source SmalltalkSerge Stinckwich
 

Similar a openFrameworks freakDay S03E02 Diederick Huijbers - C++/Physics/Cloth Animation/Templates (20)

Rust tutorial from Boston Meetup 2015-07-22
Rust tutorial from Boston Meetup 2015-07-22Rust tutorial from Boston Meetup 2015-07-22
Rust tutorial from Boston Meetup 2015-07-22
 
Javascript
JavascriptJavascript
Javascript
 
P1
P1P1
P1
 
Building DSLs with Xtext - Eclipse Modeling Day 2009
Building DSLs with Xtext - Eclipse Modeling Day 2009Building DSLs with Xtext - Eclipse Modeling Day 2009
Building DSLs with Xtext - Eclipse Modeling Day 2009
 
Neurotech Solutions Ltd: Рекомендации по Stage3D: выбор наиболее подходящего ...
Neurotech Solutions Ltd: Рекомендации по Stage3D: выбор наиболее подходящего ...Neurotech Solutions Ltd: Рекомендации по Stage3D: выбор наиболее подходящего ...
Neurotech Solutions Ltd: Рекомендации по Stage3D: выбор наиболее подходящего ...
 
Back to the Future with TypeScript
Back to the Future with TypeScriptBack to the Future with TypeScript
Back to the Future with TypeScript
 
C# for beginners
C# for beginnersC# for beginners
C# for beginners
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developement
 
core java
core javacore java
core java
 
Review constdestr
Review constdestrReview constdestr
Review constdestr
 
C++primer
C++primerC++primer
C++primer
 
Structured Languages
Structured LanguagesStructured Languages
Structured Languages
 
java tutorial 2
 java tutorial 2 java tutorial 2
java tutorial 2
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
 
T02 a firstcprogram
T02 a firstcprogramT02 a firstcprogram
T02 a firstcprogram
 
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
Kotlin - The Swiss army knife of programming languages - Visma Mobile Meet-up...
 
Mysterious c++
Mysterious c++Mysterious c++
Mysterious c++
 
Net framework
Net frameworkNet framework
Net framework
 
Why we cannot ignore Functional Programming
Why we cannot ignore Functional ProgrammingWhy we cannot ignore Functional Programming
Why we cannot ignore Functional Programming
 
Pharo, an innovative and open-source Smalltalk
Pharo, an innovative and open-source SmalltalkPharo, an innovative and open-source Smalltalk
Pharo, an innovative and open-source Smalltalk
 

Más de roxlu

openFrameworks 007 - sound
openFrameworks 007 - soundopenFrameworks 007 - sound
openFrameworks 007 - soundroxlu
 
openFrameworks 007 - video
openFrameworks 007 - videoopenFrameworks 007 - video
openFrameworks 007 - videoroxlu
 
openFrameworks 007 - graphics
openFrameworks 007 - graphicsopenFrameworks 007 - graphics
openFrameworks 007 - graphicsroxlu
 
openFrameworks 007 - events
openFrameworks 007 - eventsopenFrameworks 007 - events
openFrameworks 007 - eventsroxlu
 
openFrameworks 007 - 3D
openFrameworks 007 - 3DopenFrameworks 007 - 3D
openFrameworks 007 - 3Droxlu
 
openFrameworks 007 - GL
openFrameworks 007 - GL openFrameworks 007 - GL
openFrameworks 007 - GL roxlu
 
openFrameworks 007 - utils
openFrameworks 007 - utilsopenFrameworks 007 - utils
openFrameworks 007 - utilsroxlu
 
ofxFlashCommunication
ofxFlashCommunicationofxFlashCommunication
ofxFlashCommunicationroxlu
 
openFrameworks freakDays S03E02 Tim Olden - Computational Candles
openFrameworks freakDays S03E02 Tim Olden - Computational CandlesopenFrameworks freakDays S03E02 Tim Olden - Computational Candles
openFrameworks freakDays S03E02 Tim Olden - Computational Candlesroxlu
 

Más de roxlu (9)

openFrameworks 007 - sound
openFrameworks 007 - soundopenFrameworks 007 - sound
openFrameworks 007 - sound
 
openFrameworks 007 - video
openFrameworks 007 - videoopenFrameworks 007 - video
openFrameworks 007 - video
 
openFrameworks 007 - graphics
openFrameworks 007 - graphicsopenFrameworks 007 - graphics
openFrameworks 007 - graphics
 
openFrameworks 007 - events
openFrameworks 007 - eventsopenFrameworks 007 - events
openFrameworks 007 - events
 
openFrameworks 007 - 3D
openFrameworks 007 - 3DopenFrameworks 007 - 3D
openFrameworks 007 - 3D
 
openFrameworks 007 - GL
openFrameworks 007 - GL openFrameworks 007 - GL
openFrameworks 007 - GL
 
openFrameworks 007 - utils
openFrameworks 007 - utilsopenFrameworks 007 - utils
openFrameworks 007 - utils
 
ofxFlashCommunication
ofxFlashCommunicationofxFlashCommunication
ofxFlashCommunication
 
openFrameworks freakDays S03E02 Tim Olden - Computational Candles
openFrameworks freakDays S03E02 Tim Olden - Computational CandlesopenFrameworks freakDays S03E02 Tim Olden - Computational Candles
openFrameworks freakDays S03E02 Tim Olden - Computational Candles
 

Último

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 

Último (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

openFrameworks freakDay S03E02 Diederick Huijbers - C++/Physics/Cloth Animation/Templates

  • 1. Hello Apollo Diederick Huijbers diederick@apollomedia.nl www.apollomedia.nl
  • 2. This presentation is part of the openFrameworks freakDays meetings which are given by a group of Creative Coders and artists in Amsterdam. During these monthly meetings we give presentation on innovative techniques and show projects we’re involved in. For more information checkout www.roxlu.com or contact us at info@roxlu.com
  • 3.
  • 4. c++ templates Physics Cloth
  • 5. Basic C++ structs classes pointers templates<>
  • 6. ... use the keyword “struct” struct Particle { float x; float y; float mass; } .. green means “members are accessible”.. Particle x y mass
  • 7. ... use the keyword “class” class Particle { float x; float y; float mass; } .. red means “members are Particle NOT accessible”.. x y mass
  • 8. Differences: Struct members are public by default those of a class not.
  • 9. Check how the keyword A struct doesn’t need this public makes the members public keyword. Their green ;) and so accessible! members are public by default! That’s nice! class Particle { class Particle { struct Particle { float x; public: float x; float x; float y; float y; float y; float mass; float mass; float mass; } } } Particle Particle Particle x x x y y y mass mass mass
  • 10. When to use struct <-> class? It doesn’t really matter; it’s a personal thing....
  • 11. ...though, typically structs are used for simple data containers and classes for more advanced things.
  • 12. Classes for more advanced stuff class Particle { public: Particle(); void setPosition(float x, float y); void update(); void draw(); void applyRepulsiveForce(); void addForce(float x, float y); private: float x; float y; float mass; }
  • 13. Structs for simple data containers struct User { string name; string password; string homepage; int age; float weight; }; struct Tweet { uint16 userid; string homepage; string avatar_url; int timestamp; bool has_gps; };
  • 15. Pointers are not that hard! Instead of pointing to a value hold a a memory address!
  • 16. Pointers are not that hard Values Address in memory f #1 r #2 e #3 a #4 k #5 d #6 a #7 y #8
  • 17. char* name = “freakday” Values Address in memory f #1 name + 0 char * r #2 name + 1 Check the char* the star (*) tells the compiler we want a e #3 name + 2 pointer a char. A normal char is just one character. The 0 is a #4 name + 3 automatically added and tells the compiler it’s the end of the k #5 name + 4 “string” A pointer points hold an d #6 name + 5 memory address. We can step through the memory a #7 name + 6 addresses using this type of variable. y #8 name + 7 0 #9 name + 8 ? #10 ? #11
  • 18. Values Do not go into unauthorized f r #1 #2 memory ....or else..... e #3 “EXC_BAD_ACCESS” or a #4 k #5 “UNDEFINED” behavior d #6 a #7 ...which is .... y #8 0 #9 ? #10 name + 9 ? #11 name + 10
  • 20. Memory Alignment + packing 32 bit wide } When the CPU reads from memory it uses a natural class Particle { enabled alignment which must be bool enabled; // 8 bits respected to permit the CPU float x; // 32 bits x to read and write memory float y; // 32 bits y effectively. A compiler will bool flying; // 8 bits leave holes in memory when flying members/objects aren’t } aligned properly. = memory needed WRONG! = memory actually used You should align your bytes nicely
  • 21. Memory Alignment + packing 32 bit wide } class Particle { float x; // 32 bits x float y; // 32 bits y bool flying; // 8 bits f e bool enabled; // 8 bits } = memory needed CORRECT! = memory actually used The members are aligned nicely
  • 22. Memory Alignment + packing Check XCode project “freakday_S03E02_packing”
  • 23. Templates [TYPE_ADDRESS]: some value [ADDRESS_ENTRY]: some value [TYPE_PHONE]: some value
  • 24. Templates Office address: some value Billing address: some value Mobile phone: some value Work phone: some value
  • 25. Templates Templates are a way to write more generic code: write one special thing (algorithm) which you can use for many kind of types. Create one template for a particle for both 2D and 3D. Particle<2D> and Particle<3D>
  • 26. 2D particles.... Particle class Particle2D { ofVec2f position; ofVec2f velocity; ofVec2f forces; void draw(); } Particle-Group class 2DParticlesGroup { vector<Particle2D*> particles; void addForce(ofVec2f f) { for(int i = 0; i < particles.size(); ++i) { particles.at(i)->addForce(f); } } }
  • 27. 3D particles.... Particle class Particle3D { ofVec3f position; ofVec3f velocity; ofVec3f forces; void draw(); } Particle-Group class 3DParticlesGroup { vector<Particle3D*> particles; void addForce(ofVec3f f) { for(int i = 0; i < particles.size(); ++i) { particles.at(i)->addForce(f); } } }
  • 28. 4D particles.... Particle class Particle4D { ofVec4f position; ofVec4f velocity; ofVec4f forces; void draw(); } Particle-Group class 4DParticlesGroup { vector<Particle4D*> particles; void addForce(ofVec4f f) { for(int i = 0; i < particles.size(); ++i) { particles.at(i)->addForce(f); } } }
  • 29. 5D, 6D, 7D ... or template Add this before your template class: “template<typename T>”. The “T” is the type parameter: Particle2D, Particle3D, etc.. The name “T” can be anything you like, but T is convention. Also correct: template<typename SomeType> template<class SomeType> template<class T> “Typename” and “class” can be used both, “typename” is preferred.
  • 30. Templated Particle template<typename T> class Particle { public: Particle(T oPosition, float nMass); void addForce(T oForce); void update(); void draw(); private: T position; T forces; T velocity; float mass; };
  • 31. Templated Particle-Group template<typename P, typename V> class ParticleGroup { public: ParticleGroup(); void addParticle(P oParticle); void addForce(V oForce); void update(); void draw(); private: vector<P> particles };
  • 32. Example of templated particles template<typename P, typename V> class ParticleGroup { public: ParticleGroup(); void addParticle(P oParticle); void addForce(V oForce); void update(); void draw(); private: vector<P> particles }; Check XCode project “freakday_S03E02_templates”
  • 33. Keyword Typename Consider the following code: template<typename T> class SomeClass { public: typename T::SubType* ptr; }; Makes sure that subtype is seen as a type, not a static member.
  • 34. class WithStatic { class WithType { public: public: static int MyType; class MyType { } } } Here T::MyType * ptr WRONG: Here T::MyType * ptr would refer to the nested type would mean a multiplication in the WithType class. template<typename T> template<typename T> class SomeClass { class SomeClass { public: public: typename T::MyType* ptr; T::MyType * ptr; }; };
  • 35. When to use the keyword typename -- pseudo code -- template<typename T> class MyContainer { vector<T> items; void draw() { typename vector<T>::iterator it = items.begin(); while(it != items.end()) { (*it)->draw(); ++it; } } }; -- add some particles to container MyContainer<Particle2D*> particles; particles.items.push_back(new Particle2D()); particles.items.push_back(new Particle2D()); particles.draw(); Here we make sure that iterator is a type, not a static member! Usually you use the keyword typename with STL iterators
  • 37. Newtons first law An object continues with constant velocity unless a force acts upon it[1]
  • 38. Newtons first law Note that the red arrow stays the same size. The red arrow represents the velocity vector and showing the first law Velocity vector(s): ofxVec2f, ofxVec3f time and position 1 2 3 4 5 6 7 Check XCode project “freakday_S03E02_5_simple_motion”
  • 39. When creating particle systems I often simulate drag by degreasing the velocity by a small percentage. Note how the velocity vector below get’s smaller. // example faking drag The amount of drag, the 0.98 void Particle::update() { in this example, totally velocity *= 0.98; depends on what you want. It’s a case of tweaking to get } the right behavior. time and position 1 2 3 4 5 6 7 Check XCode project “freakday_S03E02_6_simple_motion_with_drag”
  • 40. Newtons second law A force acting on a object produces acceleration that is proportional to the object’s mass. [1]
  • 41. Newtons second law this is the resulting force this arrow represents the when mass is taken into force acting on the object account 160-200 KG (?)
  • 42. Newtons second law this is the resulting force this arrow represents the when mass is taken into force acting on the object account 75-80 KG
  • 43. Newtons second law The math of how force, acceleration and mass is related.... “The law” ( F = ma ) force = mass * acceleration ...to get the acceleration we do: acceleration = 1/mass * force this is what I use in particle systems
  • 44. Newtons second law This one is important! The bigger the mass, the less the acceleration ration = 1/mass * force accele This is what we call inverse mass. We do not need to recalculate this for each update! We store this in a member of the Particle class and can be used to represent “infinite” mass when set to zero: 0 * force = 0
  • 45. How does this look like in code? class Particle { ofxVec2f position; ofxVec2f velocity; ofxVec2f forces; float inverse_mass; Particle(float fMass) { if(mass <= 0) { inverse_mass = 0; } else { inverse_mass = 1/mass; } } void addForce(ofxVec2f oForce) { D’Alembert’s Principle (simplified): forces += oForce; We can accumulate all forces acting } on an object. void update() { forces *= inverse_mass; velocity += forces; velocity *= 0.99; position += velocity; forces.set(0,0); } }; Check XCode project “freakday_S03E02_7_simple_motion_inverse_mass”
  • 46. Springs “Springs are structural elements that, when connected between two objects, apply equal and opposite forces to each object.”
  • 47. Springs Object 1 Object 2 The red arrows represent force vectors in opposite directions: so the two objects are pulled towards each other
  • 48. Springs The rule related to springs forces is called “Hooks” law! f = -k∆L -k = spring strength; in code just a “float strength” which often gets a value like 0.1, 0.001 ∆L = difference between springs “compressed/rest length” and current length.
  • 49. ∆L = (r - d) difference between springs “compressed/rest length” and current length. Two springs not stretched/compressed and showing their rest length. } r = rest length
  • 50. ∆L = (r - d) Two particles connected by a spring (r) rest length Spring force vector Spring force vector (d) distance
  • 51. Spring force vector Rest length Spring force vector (d) distance Spring template 2d/3d/.. template<typename P, typename V> class Spring { public: ! Spring(P pParticleA, P pParticleB, float nStrength = 0.01) ! :a(pParticleA) ! ,b(pParticleB) ! ,k(nStrength) ! { Rest length: distance ! ! rest_length = (b->position - a->position).length(); between particles when ! } creating the spring ! void update() { ! ! V dir = b->position - a->position; Current ! ! float dist = dir.length(); distance ! ! if(dist == 0.0) { ! ! ! dist = 0.0001; // division by zero ! ! } ! ! ! ! float f = (rest_length - dist) * k; The force spring ! ! dir.normalize(); ! ! a->addForce(dir * -f); ! ! b->addForce(dir * f); ! } ! ! P a,b; Force spring multiplied by normalized direction vector and in opposite directions
  • 52. Springs Example: click mouse to add particle + spring... Check XCode project “freakday_S03E02_8_spring_forces” Example: right click mouse to add particle + spring and use sliders to see the result of different “k” and “drag” values. Check XCode project “freakday_S03E02_9_spring_forces_with_gui”
  • 54. Cloth Physics Simple cloth physics used multiple springs connected to particles in a grid.
  • 55. Cloth Physics Particles are positioned on a grid and then connected by 3 kind of springs: - structural springs - shear springs - bending springs
  • 59. Check XCode project “freakday_S03E02_10_cloth” re ) s ul : or tif a rr u E ea b
  • 60. Creating the particle grid // lets create the grid. for(int i = 0; i < nCols; ++i) { ! for(int j = 0; j < nRows; ++j) { ! ! ofxVec3f position(i * nSize, j * nSize); ! ! Particle3D* particle = new Particle3D(position, 1.1); ! ! particles[j * cols + i] = particle; ! ! if(j == 0) { ! ! ! particle->inverse_mass = 0; ! ! } ! } Calculating the index in the vector. We position the } particle just like normal image pixels are stored, by column and row. This is handy when we need to lookup (see getAt) a particle based on a column and row.
  • 61. Creating the structural springs // create springs. For more info on Particle3D* a; how to create the Particle3D* b; for(int i = 0; i < nCols; ++i) { other springs see ! for(int j = 0; j < nRows; ++j) { the source; it’s ! ! quite similar. ! ! // structural spring - right. ! ! if(i < nCols-1) { ! ! ! a = getAt(i,j);! ! ! ! b = getAt(i+1, j); ! ! ! springs.createSpring(a,b); ! ! } ! ! ! ! // structural spring - down ! ! if(j < nRows-1) { ! ! ! a = getAt(i,j); ! ! ! b = getAt(i, j+1); ! ! ! springs.createSpring(a,b); ! ! } ! } } Check XCode project “freakday_S03E02_11_with_gui”
  • 62. Next time C++/...: Initializer lists Virtual functions Inheritance More templates ..... Debugging linker errors (file, nm) Operator overloading (=,[],*,etc.) Const, static
  • 63. Next time particles/3d Integrators (euler, verlet, runge kutta) Fast particle systems (buckets, sorting) Billboarding Matrices / 3D Shaders
  • 65. References 1. Ian Millington, Game Physics Engine Development, 2007