Need Better Physics Engine in Game

Started by
11 comments, last by alh420 11 years, 5 months ago
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 should be putting the physics step on a thread and just using the position data etc for the rendering in order to get better movement? Another problem is, how do I do rotational collisions? 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;)
Advertisement
Is there a specific reason you want to write the physics engine yourself?
It's quite difficult to write an efficient and fast physics implementation, so you might want to consider using a (free) proven solution.

Some popular solutions include: Havok (free for games with a retail price of $10 or lower), bullet physics, nvidia physx and ODE (haven't heard about this one for a while though)

I'm sure any of these can fulfill all of your physics needs, and more.

If you insist on writing it yourself I'm afraid I can't really help you out here.

I gets all your texture budgets!

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:

I just need to get a better way to do collisions.
I'd personally recommend against trying to build a full-featured physics engine on your own if your actual intention is to build a game. You can expect to dedicate quite some time to writing something which can qualify as a "simple version" of bullet, and I'd assume that time would be better spent on trying to get your game out there.

Bullet itself has been in development for years and its main author, Erwin Coumans, has been a senior physics engineer at multiple companies, including Havok if I remember correctly, so it has taken quite some time even for a very experienced engineer to develop something usable.


Now of course, if your main goal is to actually build a physics engine you should just go for it, but seeing your original post I don't think this is really what you want to accomplish in the end, right?

It's also interesting to note that a lot of major AAA studios who build their own game engines don't even bother with writing their own physics engines anymore, and just use existing solutions like the ones mentioned in my previous post.

I gets all your texture budgets!

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

I gets all your texture budgets!

Hi,
I've been writing my own "physics engine" as well, though it's 2D. You might want to have a look at euclideanspace.com as well as this paper.
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.
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!!
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.
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...

o3o

This topic is closed to new replies.

Advertisement