How to do physics in three.js + node game

Started by
4 comments, last by Euthyphro 4 years, 5 months ago

Hey guys.

Im doing a first person shooter (multiplayer) using three.js and node serverside. Now the question, that i have, is, how do I do physics. First of all, player movement, so the player can not walk through obstacles and jump on crates and not fall into the ground. I gotta do this client-side (for prediction) and server side (so players can not cheat). I dont know, whats the best way to go here, since it also has to be done server side, it shouldnt be too expensive. Do i use a physics engine like cannon and just let the engine handle everything, client and server side aswell ? That seems quite expensive if I do that serverside. Do i rather implement shape vs shape collision functions by myself ?

Advertisement

I recommend cylinder vs plane collision checks. However, once you get above ~100 plane checks per frame in a browser, you'd have to use start dividing the level into sections (quad-trees).

so players are cylinders and i splice the map into tons of planes ?

6 hours ago, tmothy_bentley said:

so players are cylinders and i splice the map into tons of planes ?

How complex and/or large are your environments? Do you have any screenshots or video?

Implementing this sort of movement from scratch can be nontrivial, partly due to numerical imprecision and various 'difficult' cases such as interacting with concavities where surfaces meet. There are some popular papers that have floated around over the years, but it's been mentioned that even those algorithms might suffer from some weaknesses, which may serve as evidence that it can be difficult to get right.

For a long time the approach taken by the Quake engines was popular (partly because those engines and their derivatives were popular). My understanding of the algorithm used there is that axis-aligned boxes were used for moving objects, with the environment preprocessed so as to expand (and bevel) the geometry, allowing for handling of objects as moving points, thereby somewhat simplifying things. (I'm not swearing by this - it's just my recollection.) I don't know if this approach is still in common use (I'm guessing it's now more common to use third-party physics solutions).

Cylinders can be harder to work with in general than some other shapes, but I've seen discussion of a sort of similar 'sliding plane' system involving cylinders. I think this requires that cylinders always remain aligned to a single axis, but that's of course reasonable in this setting. You could also consider capsules as an alternative to cylinders, as capsules are a bit easier to work with.

I read the Wikipedia article on Cannon. You may already know this, but the article specifically mentions network synchronization, so you might take a look at that if you haven't already.

If you can find a suitable third-party solution, that would obviously be easier than implementing it yourself. If you want to take the latter route, it might be useful to know more about what sort of geometry you'll be working with.

@tmothy_bentley, yes my idea is that players are upright cylinders and everything else is a rectangle or a rectangular prism (you decide).

You're right, Zakwayda. These are some tough problems. Axis-aligned boxes is the simplest solution but two players colliding will be strange.

This topic is closed to new replies.

Advertisement