Making a physics engine implementation

How would i go about making a custom implementation of a physics engine. the reason why i wanna do this is because i wanna build a 2D game and i wanna use JBox2D as my physics solution rather than bullet. how would you guys recommend me doing this? or would it even be possible to do this with JBox2D?

I don’t know how jbox2D works, but that’s usually a matter of “replicating” your scenegraph into the “physics space” of the physics engine, and to mirror the transforms from the physics space to the scene graph.
For that, you can make a physics control that you attach on each spatial and that sets the position of the spatial.
You could also have a more broad solution, with an AppState that updates the position of physics influenced spatials.

Though, several people on this forum use a 2D physics engine called Dyn4j, that has been successfully integrated with JME.

Yeah, dyn4j is a nice one.

Basically, you have to stop thinking of your JME spatials like game objects. They are just the visual representation of your game objects. Just like your physics objects will be the physical representation.

So once a frame you just have to make sure the visuals match the game state.

In this respect the beginners guide is a little missleading. With all that control and user data and savable stuff you think, that this is the perfect game object. At least that happen to me and I first had hack myself in a corner a couple of times until I started to get that advice serious.
And that alone was not enough I also had problems to make an archtecture where that separation takes place.
That’s why I started those blog articles to show one way out of this problem.


My reply is a bit off topic … but this answer is awesome, and it’s exactly what we need to say to people that create a network game. So plain and simple, so clear: spatials are NOT game objects.

And for the topic: just like other said: every frame pick the informations about the object (rotation, position … remember that they will likely be in world coordinates and if it’s so you’ll have to translate them in local coordinates, this can be trivial if every spatial are direct child of the world node (likely the rootNode, but maybe you could use the guiNode as it’s alreay ortho etc) but if it isn’t the case you can have a look at how this is done for jbullet (i think i can retrieve the code if you don’t find it).
Tips ? Be sure that you can display a 2D object somewhere, move it “by hand” to be sure that you can move it, pick a trivial-from-a-tutorial example that works with jbox2D, try to bind the position of the graphic object with the position of the object in the physic simulation. You can also display the position you got from the physic engine with a plain old System.out.println.

Off topic as well: That counts for every game.

Off topic too… that counts for every single application you are going to make in a dev career…
MVC is a very very common dev pattern, and maybe our mistake is that we just take it for granted that users will use it. And you’re right that the examples are not exemplary in this regard.

What makes a simple example does not always make for a good design in a real application.

1 Like

@nehon: Yes that counts for every software. Most often your “engine” or architecture leads you to the right direction. At least that is the case in my real life.
I would more say that the features of spatial can be misleading not the examples itself. In the examples you use those features which just cement the way toward the wrong direction. But those features as well can be very use full wisely used. So in the end the guy in front of the computer is to blame, like usual :wink:

So maybe I should correct it and say: “… could be misleading…” instead “… is a little misleading …”

Just that I said it, the beginners section is one of my favorite place in the wiki, those hello samples was the “thing” which told me “this is the game engine I want to use”! So keep the good work up everybody.

I think that the most misleading this is:
1 - controls on spatials. What are they ? (i know how to use them, how to implement them etc, that is not my question). Especially, what are they if a spatial is only the visual representation of you game objects.
2 - userdata stuff. They are quite yummy, of course, but they give the idea that you can put datas there (like hp etc).

maybe in a jme 4 or later we could try to give an implementation to the model (the model of “mvc”) but it sounds a bit like “maybe we can try to give an implementation to all-and-everything-possible”.
I don’t know how ut engine (or unity, or cryengine) handle this question.
It would also be a nice cement to implement a generic server.
But the question here is more about philosophy than about coding.

JME is nice because it doesn’t force you down a particular path… which is why some of us can successfully use an ES and other don’t have to.

1 Like

I am not talking about forcing anyone to anything. I don’t like nifty, i don’t use it. But i was happy to found it when i started jme.
I use the bulletengine but i can remove it easily. I love this.

Create something that both help people structuring their code and allow them to have a network based game in one click, will not oblige them to use it.

(and about es, as i don’t know where to ask the question: am i right if i say that the oop approach fails with the problem of the nominalism
( Nominalism - Wikipedia ) i.e. we want to say that a horse has 4 legs, a tail, X teeths etc, but what happen if a horse has only three legs ? we say that it’s a hors with three legs but it’s like a nonsense. And when we define the inheritence in our classes we create such category, like a horse, and we want to perform actions on any element of the equivalence class, like “walk”. Problem, if a horse is crippled it can’t walk. We could say that it’s not “horses” that walk but “horses with 4 legs” (so we remove the “a horse has 4 legs rule” from the horse definition) but then the question is: “what is a leg”. And the problem goes on and on, until we reach the level of atomos (literally: something we can’t cut) where we can define some properties without the risk of being wrong. Problem: at this point we could as well delete the horse requirement for the walk actions, as we are actually describing without any “meaning” what can walk (we can also remove the “leg” thing, we explained the semantic so much that we now have only syntax)
and the es system, instead of saying what objects are says what objects have (and a horse can has the ability to walk but he can also don’t have it).
I was thinking about that (philosophy about es) for days, but i didn’t wanted to create a topic only to talk philosophy, it’s like not related at all with jme.
Btw, are you the inventor of the entity system principle ?

That’s sort of it. ES is data-oriented and OOP tends to be method/encapsulation oriented.

Nope. I wish.

I created Zay-ES which is a ‘pure’ ES implementation (as opposed to some less pure Java ES implementations). But I basically learned about it like a lot of others… from articles on the web that someone pointed me to (thanks @normen). I went from skeptic to rewriting my whole engine in about a month. After a while, I refactored Mythruna to pull the ES out into its own library.

Now every game I start or make is always ES-based.

It is not incompatible with OOP. The OOP allow very good implementation of this architecure, but it is not a perfect fit in matter of performance (who cares?)

Entity system has been used for the first time in the game Thief: the dark project in 1998, as far as I know.

I would say the current form of ES systems is not that old actually,

before they were not that pure implementations, aka systems seperation of logic, entity is only id.

That’s a great reference, by the way. Thanks for the link.

It’s nice to see some things distilled that I had to piece together over several t-machine articles originally.

Edit: or maybe it’s exactly what I read originally and I just have lots of concrete examples to draw from this time… as thinking about it, that’s what this article really misses.

Edit2: yep, exact same articles I started with. It’s the examples in part 4 or 5 or whatever the drove it home. Sad that the originals are gone now, I guess.

Thanks for all the reply’s! im liking the idea of dyn4j thanks guys

I’ve just integrated Dyn4j to Zay-ES in the sample project of Alchemist, and it’s perfect :smile:

I can provide the code if you wish to have a look.

1 Like

Not to hijack this thread, but I guess you got your stuttering problem worked out?

After some debug I confirmed it was not in the game logic but in the multithreading, as you pointed out. I’m running the culprit logic into the rendering thread for the moment. I will implement your solution the day it becomes an issue.