Sign in to follow this  
Marz Tierney

Need Better Physics Engine in Game

Recommended Posts

Okay, so this is my first Post, so please forgive me as I'm sure I won't be giving out enough information.

I've been working on a project for some time now, and I'm trying to create a better physics engine, however physics is not my best subject. My game is similar to minecraft, there are static chunk objects, and physics objects. My main problem is that it seems very laggy, compared to other games, even with high fps. So i'm wondering if I [b]should be putting the physics step on a thread and just using the position data etc for the rendering[/b] in order to get better movement? Another problem is, [b]how do I do rotational collisions[/b]? Here is some of my code for collisions.
[attachment=12348:townGen4.png]

// This is for when a physics object when it hits the bottom of a block (static position)
void collide(PVector loc_, float r_) {
...
if (checkTop(loc_,r_,r_,r_)){
vel.y*= -bounce;
loc.y = loc_.y + r_+h+teleport;
hasCollide = true;
} else if (checkBottom(loc_,r_,r_,r_)) {
vel.y *= -(bounce);
loc.y = loc_.y -r_-h-teleport;
hasCollide = true;
isOnGround = true;
if (vel.y > -0.7 && vel.y < 0.7 && inBlockID == 0) {
vel.y = 0;
}
...
}

boolean checkTop(PVector loc_, float w_, float h_, float d_) { // Check Y axis collision
if ( loc_.y + h_ > loc.y - h && loc_.y - h_ < loc.y - h && // Y top's edge is between Blocks borders
loc.x < loc_.x + w_ && loc.x > loc_.x - w_ &&
(loc.z < loc_.z + d_ && loc.z > loc_.z - d_) )
return true;
else return false;
}
boolean checkBottom(PVector loc_, float w_, float h_, float d_) {
if ( loc_.y - h_ < loc.y + h && loc_.y + h_ > loc.y + h &&
loc.x < loc_.x + w_ && loc.x > loc_.x - w_ &&
(loc.z < loc_.z + d_ && loc.z > loc_.z - d_) )
return true;
else return false;
}

Would I have to check all the points along a plane to see if it goes through another plane? as they would not be parralel. (The sides of an object)
Also is there a better way to make sure a physics object doesn't clip through the block, besides setting its location outside of it?
(loc.y = loc_.y + r_+h+teleport;)

Share this post


Link to post
Share on other sites
Yeah i've seen some of them libraries before, they look amazing, but i'de prefer to implement my own, for the experience mostly. I guess i'll have to search more for examples or explanations on how to implement a simple version of bullet physics.

Im going for something like this:
http://www.youtube.com/watch?v=eQMBGLMtdFE
I just need to get a better way to do collisions.

Share this post


Link to post
Share on other sites
My goal does not involve using a physics engine, it involves building one. Game development is a hobby of mine, the features of a physics engine I need are not so far out of my grasp. I will find the answer if I spend more time on it. I'm a uni student after all with much free time. I've got experience in robotics too, so a physics engine also interests me. However I will try out some of the recommended physics engines in the mean time.

Regarding your suggestion of getting my game out there, what is the best way to go about this? I'm guessing releasing a playable version online somewhere, have you done this before?

Share this post


Link to post
Share on other sites
Fair enough :)

Releasing and publishing a game is a completely different subject, and I can't say I'm really experienced in that matter, there are however systems such as steam greenlight which can help you with getting your game published for a very reasonable fee. I'm mainly a graphics and gameplay programmer (and CS and former engineering student) myself, things like publishing and legal matters I leave to my business partner who's qualified to handle those things.

Share this post


Link to post
Share on other sites
Hi,
I've been writing my own "physics engine" as well, though it's 2D. You might want to have a look at [url="http://www.euclideanspace.com/physics/index.htm"]euclideanspace.com[/url] as well as this [url="https://docs.google.com/file/d/0Bze6mKYvrpOKYjdkODVhMTAtM2Q4Zi00NzgyLWE2YzMtN2MwZmQ4NjA3OWMw/edit?pli=1&amp;hl=en"]paper[/url].
The paper is essentially an overview of everything you need in a physics engine. I hope his helps to get you started :)

Edit: And btw, as long as you're not very versed in the involved math and physics as well as general algorithm design it will be quite hard to get results like the stuff in the video you posted. It gets really hairy in the details. But even if you don't get to that point you'll learn a lot on the way and that seems to be what you're going for anyway. Edited by bonus.2113

Share this post


Link to post
Share on other sites
Wow great links, they seem like just what I was looking for. I am well in it for the learning experience as well. 2D physics are very similar to 3D, my first 3D physics simulations were based directly off 2D. There is a lot of maths involved with getting the camera to work in opengl, it's possible, just hard haha. I was mostly working on data structures, loading on threads and world generation techniques before, and my physics engine (which I neglected) was based on older version off a 2d version of Daniel shiffman's physics examples I got my hands on in first year uni, something similar too:
http://natureofcode.com/book/chapter-2-forces/

Ahh okay, I'll look into steam greenlight for publishing. Atm though I'm mainly interested in having my game on a site, so people could possibly beta test it, have you done this before? I also have a bunch of questions about graphics programming, I'm using VBO's for the cubes, trying to implement a simple light shader, or the reflective water like in Terasology!

Well it seems I have a long and interesting road ahead. If I have any more questions I'll post them :D thanks for all the help!!

Share this post


Link to post
Share on other sites
Another tip, if you want to learn how a physics engine works, is to look at the source code for any of the available ones. I find Bullets code reasonably easy-to-read.

I'd also like to point out that even if your final goal is to write your own, you will still learn a lot about physics engines and whats needed to make it stable, by using an available one first.

I think most people who set out on doing physics underestimate how much work (and stuff to learn) it is to just get a ready-made physics engine to do exactly what you want it to, unless you are just doing very simple destruction/explosion-like physics.

It's very far from a waste of time. You will need all those parts in your own engine too, and if you are versed in using them, it makes it a lot easier to debug your own later. Edited by Olof Hedman

Share this post


Link to post
Share on other sites
Do you need the blocks to rotate? For a voxel based game, i think non-rotating cubes would be better because:
1.They fit in holes, if you rotate a cube you cant fit it anywhere because its never aligned right
2.Simpler for you to implement
3.Faster physics?

Though if you want to have objects that are not 1-cube-sized, it might not work that well. But for non-rotating multi-voxel objects, you could make big sliding doors out of them or something, which might be buggy with rotation...

Share this post


Link to post
Share on other sites
I 2nd Radikalizim ... any time you spend writing your own physic "engine" or even just rigid body physics routines is a waste, just use bullet physics and get the job done. You can override any of the bullet callbacks/solvers and have your own custom implementations.

Share this post


Link to post
Share on other sites
I need rotational collisions for skeleton/body physics, and also it's alot better if the cubes rotate... And yes Olof, i've read alot so far, and I realize there is alot of work that goes into physics engines, but I find if I make my own engine, it is easier for me to debug and modify. I know how versatile they are to use, but I don't think I need to implement all the features they have. The example i've looked at uses points and tests them in a feild, i'm not sure exactly because the dependencies are a little confusing, but im guessing the way to check basically would be to check all the points on a plane go through another plane? This mathematically makes sense, but would take alot more processing power.

http://www.openprocessing.org/sketch/39230

Share this post


Link to post
Share on other sites
I wasn't saying anything about the work going into the physics engine, I just said there is a non-trivial amount of work _using_ any physics engine, and that you can learn a lot from it that is useful when you make your own. And most are very modular, so you can use for example the collision detection part of it, and implement your own physics, and thus take it one problem at a time.

There is a certain pride in feeling you have control over _all_ the code in your project, so I totally understand your wish to roll your own :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this