Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (595) Games in Android Showcase (168) games submitted by our members Games in WIP (646) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 2D verlet physics tutorial  (Read 8225 times) 0 Members and 1 Guest are viewing this topic.
CommanderKeith
 « Posted 2009-11-24 18:03:03 »

I know some of you already know all about verlet physics (KevG, Riven),  but for the rest of us, there's an easy to understand 2D verlet tutorial on gamedev.net that was recently published:

http://www.gamedev.net/reference/programming/features/verletPhys/default.asp

Unfortunately the sample code is in C++ but I think I get the idea. Looks pretty neat.

Riven
« League of Dukes »

« JGO Overlord »

Medals: 1001
Projects: 4
Exp: 16 years

 « Reply #1 - Posted 2009-11-24 18:17:04 »

It's a real shame that acceleration is a vector in that code. Then you can just as well make velocity a vector.

Acceleration can be calculated, without time, by recording not 2 but 3 positions of each particle.

Acceleration = (Pnow-Pold) - (Pold-Polder)

If you have a body (set of particles with springs connecting them) acceleration will be a side-effect of movement (and collision). With 1 particle (not a body) it won't really work when colliding.

Example:

Pnow = (18, 101)
Pold = (15, 101)
Polder = (13, 101)

vold...now = (18, 101) - (15, 101) = (3, 0)
volder...old = (15, 101)  - (13, 101) = (2, 0)

aolder...now = (3, 0) - (2, 0) = (1, 0)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
CommanderKeith
 « Reply #2 - Posted 2009-11-24 18:26:15 »

Thanks for pointing that out. Would one method work better with a non-constant frame rate?

What about the limitation of only using convex polygons? Would it be worth making a different collision detection algorithm to do concave polygons, or would it be ok to just split the concave polygon into a few smaller convex polygons and connect them up somehow?

Cheers,
Keith

Riven
« League of Dukes »

« JGO Overlord »

Medals: 1001
Projects: 4
Exp: 16 years

 « Reply #3 - Posted 2009-11-24 18:42:49 »

The nice thing of Verlet is that you don't have to worry about ... anything.

Just make a body by connecting Particles with Springs.

If a particle is intersecting a triangle (just build your shapes out of triangles...) then push that single particle out of the triangle, and that's it. Due to the springs and other particles (that all have momentum) things will bounce off, getting angular velocity... you name it. The conversion from 2D to 3D physics is trivial: add a Z coordinate and you're done.

Think of it like atoms in real life. They don't know anything about cos/sin/sqrt, they don't even have collisions, only (electromagnetic) forces. They just act as independent particles with little springs (forces), and on a very high level, they behave as if they are performing very complex mathematical calculations, projecting themselves onto planes and whatnot, to figure out their next position, while in reality, they don't know squat.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
DzzD
 « Reply #4 - Posted 2009-11-24 20:44:30 »

Quote
Thanks for pointing that out. Would one method work better with a non-constant frame rate?
you should use a fixed time granularity, let say 10ms

if you have to simulate the physics for 30ms (because your last frame was 30ms ago) you just perform 3 physics loops : this is important for the correctness and stability of the physics simulation model.

this is important because using too bigs times steps will results in completly wrong result :
the fastest your object go (compared to its size) the smallest the timestep must be and more generally you should never move on atome more than half the lenght of the n constraints (springs length)  it is connected to.

EDIT : this is why it is pretty hard to push such object with a huge force (still in comparaison of its mass & size), because such force will result in a hight cceleration and so a too big move

DzzD
 « Reply #5 - Posted 2009-11-24 21:02:07 »

Quote
 1  2  3  4 `//Push both vertices apart by half of the difference respectively //so the distance between them equals the original lengthE.V1->Position += V1V2*Diff*0.5f; E.V2->Position -= V1V2*Diff*0.5f;`
this represent a Spring with an infinite k constant producing an infinite force for any displacement => such spring does not exist and this wont work in 3D or if an Atome have more than two connections in 2D.

because of the interaction of springs on each other I guess it is also better even in 2D to use a smaller value and iterate n times.

Riven
« League of Dukes »

« JGO Overlord »

Medals: 1001
Projects: 4
Exp: 16 years

 « Reply #6 - Posted 2009-11-24 21:16:29 »

Noticed that too...

This is my code:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45 `   public final float tick()   {      float ax = a.now.x;      float ay = a.now.y;      float az = a.now.z;      float bx = b.now.x;      float by = b.now.y;      float bz = b.now.z;      float dx = ax - bx;      float dy = ay - by;      float dz = az - bz;      float dist = (float) Math.sqrt(dx * dx + dy * dy + dz * dz);      if (how == MIN_LENGTH)      {         if (dist > len)            return 0.0f;      }      else if (how == MAX_LENGTH)      {         if (dist < len)            return 0.0f;      }      float tension = (this.len - dist) / dist;      float force = tension * this.stf;      float aw = a.invWeight;      float bw = b.invWeight;      float f1 = force * aw / (aw + bw);      float f2 = force * bw / (aw + bw);      a.now.x = ax + dx * f1;      a.now.y = ay + dy * f1;      a.now.z = az + dz * f1;      b.now.x = bx - dx * f2;      b.now.y = by - dy * f2;      b.now.z = bz - dz * f2;      return tension;   }`

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
DzzD
 « Reply #7 - Posted 2009-11-24 21:31:48 »

I also found better that before moving anything Atome/Particle using springs constraints it is a good idea to integrate/accumulate them for each particle into another vector and once all constraints force (spring force) has ben computed move each particles with its corresponding accumulation vector, this will make the order you proceed them independant of the resulting move and will result in a better simulation (EDIT : it also let the possibility to check the length of each accumulation vector before applying them)

Riven
« League of Dukes »

« JGO Overlord »

Medals: 1001
Projects: 4
Exp: 16 years

 « Reply #8 - Posted 2009-11-24 23:09:54 »

I also found better that before moving anything Atome/Particle using springs constraints it is a good idea to integrate/accumulate them for each particle into another vector and once all constraints force (spring force) has ben computed move each particles with its corresponding accumulation vector, this will make the order you proceed them independant of the resulting move and will result in a better simulation (EDIT : it also let the possibility to check the length of each accumulation vector before applying them)

Very true. I think the same can be accomplished by moving 'old xyz' in the opposite direction, so that the next step will push the particle along.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
CommanderKeith
 « Reply #9 - Posted 2009-11-25 03:38:04 »

Great advice DzzD and Riven, I'll have a go at making a demo using your ideas.

I'm interested in this because I want physics in a 2d look down bird's eye view style of game. Since there's no stacking of bodies in such a game and most of the time there's no contact between bodies (unlike in a platformer) verlet physics seems like the best candidate since it's simple and easy to implement/understand and it's weakness of stability won't be a big issue.

pjt33

« JGO Spiffy Duke »

Medals: 40
Projects: 4
Exp: 7 years

 « Reply #10 - Posted 2009-11-25 10:47:52 »

Shame it skips over loads of important stuff like particles having different mass, elastic collisions, external forces (e.g. gravity), etc. There's enough info there to make a basic quadrature testbed but people who didn't feel the article was overly dumbed down probably don't know enough physics to turn it into something useful for a game.
CommanderKeith
 « Reply #11 - Posted 2009-12-26 23:44:21 »

Another great tutorial (with references) : http://www.metanetsoftware.com/technique/tutorialA.html

It comes with source too, but it's in an swf and an fla file. I tried opening it in FlashDevelop (http://www.flashdevelop.org) but it came out garbled... Anyone know how to open this stuff and see the source?

Ranger
 « Reply #12 - Posted 2010-01-02 22:05:40 »

I know some of you already know all about verlet physics (KevG, Riven),  but for the rest of us, there's an easy to understand 2D verlet tutorial on gamedev.net that was recently published:

http://www.gamedev.net/reference/programming/features/verletPhys/default.asp

Unfortunately the sample code is in C++ but I think I get the idea. Looks pretty neat.

That tutorial is great!  Thanks for pointing it out.

Does anyone know of a simple Verlet code example with collision detection / reaction written in Java?  (So far I couldn't find any, so I'm thinking about converting the supplied C++ sample code to Java)

EDIT:  I ended up porting the C++ code to Java.  Here is the result as an Applet:  http://verlet.googlecode.com/
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 deepthought (48 views) 2015-06-30 15:39:44 deepthought (54 views) 2015-06-30 15:39:09 deepthought (63 views) 2015-06-30 15:36:52 Za\'Anzabar (39 views) 2015-06-29 05:44:54 TritonDreyja (47 views) 2015-06-24 17:10:40 CopyableCougar4 (46 views) 2015-06-23 00:34:45 BurntPizza (52 views) 2015-06-21 20:36:46 cookiecompiler (93 views) 2015-06-11 15:42:53 cookiecompiler (55 views) 2015-06-11 15:41:14 NegativeZero (80 views) 2015-06-11 09:49:18
 princec 29x wessles 23x BurntPizza 19x CopyableCougar4 19x opiop65 17x ags1 15x nsigma 15x orangepascal 14x EgonOlsen 14x Riven 13x KaiHH 12x Jesse 11x SauronWatchesYou 11x theagentd 11x KevinWorkman 11x Archive 11x
 How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org