Arcanorum

Simple, fast, independent, physics engine alternatives?

Recommended Posts

So here's the deal. I'm making an online multiplayer action game, currently using Phaser for the client (mainly for rendering and input), and I've hacked Phaser onto my Node.js server as well (http://www.html5gamedevs.com/topic/7393-running-phaser-on-nodejs-how-i-did-it-and-why-you-shouldnt-do-it/), so I can use the Arcade physics system to manage the interactions of player's entities with other entities in the game world. I get that some people puke at this idea, but it works, mostly.

The server entities use sprites with no texture, just so I can give them a physics body to use for collision/intersection checking, but I'm having some alignment issues related to how bodies relate to the scales and anchors of their respective sprites, and have been thinking that I probably should be using a proper system where physics bodies aren't tied to sprites (display objects).

What good alternative, standalone JS physics engines are there that offer similar features to Arcade that I can use on my server? Arcade seems ideal for what I'm doing, but lugging around the rest of Phaser with it without using it is just silly.

The game only requires simple rectangle AND circle body support. No complex polygons or shape rotating, and needs to be fast, as there may be many entities that could interact with each other at any time. I've thought about doing this myself, but want to save some time if I can.

Share this post


Link to post
Share on other sites

Hi @Arcanorum

There are two 2d physics libraries I can think of. You can include both of them on Node.js, and also run in the client, if necessary:

I believe you can simulate only the physics in the server (movements, collisions, etc), not including the whole engine (Phaser) in it.

Cheers!

Share this post


Link to post
Share on other sites

I'd suggest try make your own, reasons below:

  1. AABB collision is pretty simple with just a few lines.
  2. Regarding the physics simulation you just need to create a class with only the properties your entities need, which is just another few lines of code.

For box-vs-box collision detection:

// a vs b
aIsOverlapB = !(
  a.bottom <= b.top ||
  a.top >= b.bottom ||
  a.left >= b.right ||
  a.right <= b.left
);

For circle-vs-circle collision detection:

// AABB overlap first
aIsOverlapWithB = false;
if (!(
  a.bottom <= b.top ||
  a.top >= b.bottom ||
  a.left >= b.right ||
  a.right <= b.left)
) {
  // Now let's check the distance between center of the circles with radius sum
  // Squared distance is enough, so you can skip the expensive `sqrt` calculation
  aIsOverlapWithB = Vec2.squaredDistance(a.position, b.position) < (a.radius + b.radius) * (a.radius + b.radius);
}

For complete AABB based physics implementation, you can take a look at the physics module of LesserPanda, and use it as a reference for your own physics engine.

Share this post


Link to post
Share on other sites

Rect-rect and circle-circle collision is easy, but rect-circle collision and handling is a much bigger problem. I've also looked into PhysicsJS, but that has been inactive for a few years.

I'm already able to work out which other bodies a given moving body should check for collisions with, by giving each body a 'detection radius', and checking if the distance between the two bodies is less than their sum, which I believe is the fastest way of finding potential colliding bodies.

Bodies are also given a 'category', which is a way for a given body to tell if another body is something it is interested in. For example, player bodies are interested in other bodies that have a category of 'wall' and 'triggerZone', but bullets might only care about 'wall'.

Using these two properties, any bodies that are in range of a given body, and are of a category that the first body is interested in, are then checked if they are colliding, and if so, do some gameplay stuff, then separate them if needed. No need to do collision checks against everything in the world all the time.

I've looked more into P2, but I couldn't find a simple "check if a particular body is colliding with this one, and return true or false" function like Arcade has. That and the documentation is lacking.

I just hope that Arcade 2 isn't going to be hardwired into Phaser 3, so it can be used for other purposes.

Share this post


Link to post
Share on other sites

I think P2 is overkill for arcade physics-type uses, for sure -- much more of a "real physics" simulator.

You might want to investigate using a "spatial hash" as well (googleable term). It's a spatial partitioning algorithm where you say "I'm gonna make my entire world a series of 200x200 buckets." Each object can occupy, at most, four buckets. You only check each object against other objects in its bucket. WAY more efficient vs. "every object checks against every other object" type things.

Phaser's approach here is pretty standard. For the most part you want things that are colliding to separate. How far each things moves can be based on whatever, their mass, whether they are supposed to move at all, etc. Phaser even gives you an out and lets you provide custom collision handlers.

It's probably worth it to write your own or grab one of the ones already existant: https://www.ibm.com/developerworks/library/wa-build2dphysicsengine/

Share this post


Link to post
Share on other sites

I'd hella agree that most physics engines out there are tad bit bloated & overkill.

The route I went was to actually analyse implementations done by existing libraries and write my own implementations in a fitting manner towards my code. Ie: In a platformer game I only need to check collision on floors and floating tiles whenever the unit's downward velocity is greater that its upward velocity. Another example is checking only between a predefined set of objects, instead of checking against **ALL** objects - big performance boost right there so to speak.

Check these couple repo's anyways, these got great insights on collision handlings:

https://github.com/TuurDutoit/crash

https://github.com/jriecken/sat-js

edit: This event emitter is handy too in any 'handling' part. ;) 

https://github.com/TuurDutoit/EventEmitter

Share this post


Link to post
Share on other sites

To complement what other have said, all you need is to look at 

https://github.com/photonstorm/phaser-ce/tree/master/src/physics/arcade

which shows you how arcade physics are implemented in Phaser. The only other classes used are Phaser.Point, Phaser.Line, and Phaser.Signal.

I would personally:

1) In the server, shim those classes and other stuff that arcade physics calculations use.
 

2) use mapper and reducers to filter what to send and receive to the server. Make sure it's parsable by JSON.parse.
 

Just for the lulz I found out a way to get all the children (recursively) that have bodies. So you would call findBodies(game.world), get an array of bodies and send that to the server XD

const findBodies = (child) => (child.children.length > 0)?
  child.children
    .reduce( (prev, next) => prev
      // add the results of findBodies on the next child
      .concat(findBodies(next))
      // filter out those that are undefined or null
      .filter(t=>t)
    ,[]) :
  // no children, so we return the child's body
  child.body 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.