My ball physics example program
Hi guys. I just created a simple particle/ball physics simulator and I just wanted to see what you guys think. I have programmed many games in java and *attempted* to write a 2d physics engine in java, but the structure of the language made it really hard to work with that type of math (like no operator overloading). I recently decided to migrate to C++ and I think that it is leaps and bounds better than Java (Java still has its portability uses though) for this type of programming. I decided to create a simple ball demo using C++. Here are some of the features:
-Euler integration (not the most accurate but for this example it works fine as long as there are double checks on collisions)
-Full collision response involving impulses
-Simulated gravity
-Variable elasticity, rubber band force, and collision tolerance (at compile-time)
-Random ball radius
-Each balls mass is based on the radius
Directions: Right click to add balls, left click on a ball to drag it around by an invisible "rubber band".
Anyways, here is the link:
http://www.andyknotts.com/programs/ballexample.zip
This is my first c++ "game" (or simulation to be correct). Please tell me what you think.
Very nice indeed! Physics simulators can be very complex to write, and seeing that this is your first C++ program means that you must be fairly skilled! I dare you to now add in rotational dynamics and rectangularly shaped bodies.
Thanks. I'm glad to hear some positive comments :).
Actually, I had started a major project on this in java (attempting to build 2d rigid body simulator that could later be implemented in games), and I nearly finished it, but lots of bugs and the nature of the language eventually drove me mad so now I have started coding in C++. I think I will take you up on that offer and give it another shot :)
Quote:
Very nice indeed! Physics simulators can be very complex to write, and seeing that this is your first C++ program means that you must be fairly skilled! I dare you to now add in rotational dynamics and rectangularly shaped bodies.
Actually, I had started a major project on this in java (attempting to build 2d rigid body simulator that could later be implemented in games), and I nearly finished it, but lots of bugs and the nature of the language eventually drove me mad so now I have started coding in C++. I think I will take you up on that offer and give it another shot :)
Great effort. I noticed I was able to break the simulation by adding about 30 balls. Energy was obviously being created, and things were getting stuck midair, presumably because of conflicting collisions. Fun to play with, though.
Later,
ZE.
Later,
ZE.
Quote:Original post by ZealousElixir
Great effort. I noticed I was able to break the simulation by adding about 30 balls. Energy was obviously being created, and things were getting stuck midair, presumably because of conflicting collisions. Fun to play with, though.
Later,
ZE.
Yeah, it will break if you add too many balls. I think a more accurate integration method would help remedy the problem, though. I may give it a shot tomorrow if I get the time.
Quote:Original post by Ark86
Yeah, it will break if you add too many balls. I think a more accurate integration method would help remedy the problem, though. I may give it a shot tomorrow if I get the time.
Yes, I agree. Especially the springs you use to drag the ball around with the mouse aren't especially friendly to explicit Euler integration (as I always point out, :) ).
Can we consolidate efforts?
I'm writing a 2d physics engine myself. I started with all polygonal collisions, round objects haven't been thought through particularly.
[edit]
The unique thing that I'm trying is to make particular effort to record and track collisions, so its not a stateless collision machine that uses epsilon to push things apart. Instead all contact points, and normals of contact, are recorded, and an object's next frame motion is limited by that contact. It seems to eliminate jittering, which i had very badly on my last attempt at physics. :)
[/edit]
I'm writing a 2d physics engine myself. I started with all polygonal collisions, round objects haven't been thought through particularly.
[edit]
The unique thing that I'm trying is to make particular effort to record and track collisions, so its not a stateless collision machine that uses epsilon to push things apart. Instead all contact points, and normals of contact, are recorded, and an object's next frame motion is limited by that contact. It seems to eliminate jittering, which i had very badly on my last attempt at physics. :)
[/edit]
Fantastic!! It's simple yet fun! and it's not even a game yet!
i for one is aiming on making a simple demo like this. and just like yours, a 2D will do. i have my collision detection algorithms working but i don't know how to integrate it with collision response. I hope you could share some thoughts on how to approach this.
i for one is aiming on making a simple demo like this. and just like yours, a 2D will do. i have my collision detection algorithms working but i don't know how to integrate it with collision response. I hope you could share some thoughts on how to approach this.
Quote:Original post by Ark86
Actually, I had started a major project on this in java (attempting to build 2d rigid body simulator that could later be implemented in games), and I nearly finished it, but lots of bugs and the nature of the language eventually drove me mad so now I have started coding in C++.
JAVA + Any kind of physics engine == HELL!!
I've just finished writng a really bad physics engine in JAVA too, i sympathise with this!!
Nice demo.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement