2D physics engine (Verlet)

Advanced game related topics
Kelebrindae
Enthusiast
Enthusiast
Posts: 151
Joined: Tue Apr 01, 2008 3:23 pm

Re: 2D physics engine (Verlet)

Post by Kelebrindae »

You've done a great work here, idle! :D The "spatial map" idea is really interesting.

A hint for optimization:
I found that (on my PC, with PB 4.51; it may differ with other configs...) lists embedded in structure are slower than other solutions.
I have tested 3 solutions :
- embedded list: structure xxx:... List pointmass.pointmass_struct() ... endstructure
- embedded array: structure xxx:... pointmass.pointmass_struct[maxNbPoints] ... endstructure
- pointer to a memory zone where you store the objects' pointers: structure xxx:... *ptrPointList ... endstructure
=> with drawing disabled and the "F9" scenario, the latter is faster (but harder to implement/read) with 207 FPS, the second isn't far behind with 196 FPS, and the first (even if it's more elegant and easier to read) is significatively slower with 123 FPS.

That's why I originally used the pointers in my code, despite the fact they are a little harder to manipulate.
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: 2D physics engine (Verlet)

Post by idle »

Made some improvements to the spatial map, it appears to run marginally faster when the objects aren't clumped together, though it still takes a performance hit when the objects are clumped together. (may still be something to fix)

Added a quick sprite test on F1

should we be including texture coordinates into a body and if so how to transform them?

http://www.idlearts.com/verlet.zip
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
IceSoft
Addict
Addict
Posts: 1682
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: 2D physics engine (Verlet)

Post by IceSoft »

idle wrote: Added a quick sprite test on F1
should we be including texture coordinates into a body and if so how to transform them?
Use the MP3D Engine. It is much faster s the native DirectX9 solution.
Belive! C++ version of Puzzle of Mystralia
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: 2D physics engine (Verlet)

Post by idle »

Thanks I'll have a look.
I was just thinking aloud about how to go about supporting the mapping of textures, If you only wanted to use PB sprites for rendering objects you need to store texture coordinates in the engine and update them in the verlet integration as they are separate to the bodies vertices if the object isn't rectangular, but if you used opengl for instance, you may not need the additional data all of the time.
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: 2D physics engine (Verlet)

Post by Rook Zimbabwe »

MP3D is very interesting... if no can use maybe some of the ideas can eb salvaged into verlet?
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: 2D physics engine (Verlet)

Post by idle »

Gave up on the Spatial map couldn't get it to perform better than the original structure.
Fixed some bugs in the verlet.
Have started adding support for PB textures and changed the drawing routines to create n sided convex polygons

There's still some bugs with alignment, seems to be the odd numbered sided shapes and the mapping is currently only symmetric.

Added Attractor and Repel forces, will generalize it later to provide multiple sinks and repels with a variety of force functions
it may be possible to use them to create paths instead of interpolating a spline.

Image

Shape tests keys 3,4,5,6,8,9

http://www.idlearts.com/verlet.zip
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: 2D physics engine (Verlet)

Post by idle »

Generalized Attractors and Repellers so they can be bound to objects
Windows 11, Manjaro, Raspberry Pi OS
Image
marc_256
Addict
Addict
Posts: 835
Joined: Thu May 06, 2010 10:16 am
Location: Belgium
Contact:

Re: 2D physics engine (Verlet)

Post by marc_256 »

hi, Kelebrindae and idle,

Very nice, I love your work,
thanks for sharing it with us...

Marc,
- every professional was once an amateur - greetings from Pajottenland - Belgium -
PS: sorry for my english I speak flemish ...
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: 2D physics engine (Verlet)

Post by idle »

Thanks marc

I've added a Pin body and a Join body routine, the join body needs a bit more thought.
There is also an issue in the friction calculations in the collision response that needs to be addressed. When objects are colliding in free fall they loose momentum against gravity which isn't the right response.
I noticed it when I had joined bodies together and flung them around, so they're now given a GroupID which is ok as long as you add a single object to an existing group.

The new tests F3 and F4

http://www.idlearts.com/verlet.zip
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: 2D physics engine (Verlet)

Post by idle »

Converted code to run on linux and windows under opengl subsystem.
Added a dirty hull routine to Generate a body template from an image, this should be changed to compute a proper convex hull.
There are also issues to resolve generating constraints which may lead to some bodies being unstable.

Added Body Templates
It makes it easier to manage objects as you can simply refer to them by name
So you generate a body template from an image and then invoke the body from the template

Added a couple of mini game demos (incomplete)
Asteroid osmosis (become the biggest) left click to move
Pool Table (can't be arsed to finish it but the target coords are in the data section)

http://www.idlearts.com/verlet.tar.gz
Windows 11, Manjaro, Raspberry Pi OS
Image
auser
Enthusiast
Enthusiast
Posts: 195
Joined: Wed Sep 06, 2006 6:59 am

Re: 2D physics engine (Verlet)

Post by auser »

Looks interresting. :) But there seems to be a small bug in line 136 of verlet2D.pbi so the tgz does not work in windows.

CompilerIf #PB_OS_Linux Or #PB_OS_MacOS ; is always true

CompilerIf #PB_Compiler_OS & (#PB_OS_Linux|#PB_OS_MacOS) ; should work

Greetigns,
auser
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: 2D physics engine (Verlet)

Post by idle »

thanks
probably should be

Code: Select all

CompilerIf  (#PB_Compiler_OS = #PB_OS_Linux Or  #PB_Compiler_OS = #PB_OS_MacOS)
I'll update it later
Windows 11, Manjaro, Raspberry Pi OS
Image
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: 2D physics engine (Verlet)

Post by IdeasVacuum »

....why not just rename the structure and eliminate the CompilerIf?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: 2D physics engine (Verlet)

Post by idle »

this would be better

Code: Select all

CompilerIf Defined(point,#PB_Structure)=#False
 Structure point 
      x.i
      y.i
   EndStructure  
CompilerEndIf
Windows 11, Manjaro, Raspberry Pi OS
Image
Ruuttu
New User
New User
Posts: 8
Joined: Tue Jun 28, 2011 5:47 pm

Re: 2D physics engine (Verlet)

Post by Ruuttu »

Soo... hypotethically, if I wanted to actually use this in a game, how do I make the simulation speed independent of framerate?
~Rudolf Enberg
Post Reply