Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  2D verlet physics tutorial  (Read 7378 times)
0 Members and 1 Guest are viewing this topic.
Offline 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.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« 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
Offline 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

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« 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
Offline 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

Offline 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 length
E.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.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« 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
Offline 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)

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« 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
Offline 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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pjt33
« 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.
Offline 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?

Offline 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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (41 views)
2014-10-17 03:59:02

Norakomi (33 views)
2014-10-16 15:22:06

Norakomi (26 views)
2014-10-16 15:20:20

lcass (30 views)
2014-10-15 16:18:58

TehJavaDev (59 views)
2014-10-14 00:39:48

TehJavaDev (60 views)
2014-10-14 00:35:47

TehJavaDev (50 views)
2014-10-14 00:32:37

BurntPizza (66 views)
2014-10-11 23:24:42

BurntPizza (38 views)
2014-10-11 23:10:45

BurntPizza (80 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!