I'm building a 2D MORPG using JavaScript, Node JS and socket.io
To prevent cheating, I have to run all collisions for all players on my server.
I'm currently doing fairly simple square collisions like this:
for (var i = 1 ; i < collidables.length ; i++) {
if (y < collidables[i].y + collidables[i].z &&
x < collidables[i].x + collidables[i].z &&
x + z > collidables[i].x &&
y + z > collidables[i].y) {
// Handle collision
}
}
When a player collides with a wall I halve their speed and bounce them back.
This is not working optimally, but it's fine for now.
The same logic is used to check bullet collision and possibly collision with monsters in the future.
I'm currently using 3 different loops for the follwing:
- Player vs Terrain collisions
- Bullet vs Terrain collisions
- Bullet vs Monster collisions
Since all of them have slightly different logic.
Should I put them all in the same loop or is using multiple loops fine? I feel like using multiple loops will slow down my server a lot once there will be some more terrain / players / monsters.
I chose square collisions because I think they are a lot faster than any other type of collision checking, and my game will have a LOT of collision checking.
I think I will allow between 50-100 players on a single server, with most likely hundreds of monsters and thousands of bullets flying around.
There will also be a rather big map that has to stay loaded on the server. But since the server doesn't actually have to draw anything this should be fine? Client-side the map is split up into smaller parts and only loads the parts near the player.
My game is working fine for now, but there's no real way to test with 50 players until people actually start playing.
I'm afraid I will find out way too late that my collision checking is taking up way too much server memory and/or cpu.
How can I improve the efficiency of my collision checks?
...
Here's a preview of my game in pre-alpha, it should make my question less abstract.
If I need to post any more info / code, please let me know and I will do so!
I'm currently running this game on a very small test-server with 512MB RAM and 1 VCPU (can't find the specifications)
This is scalable to up to 16GB RAM and 8 VCPU's and probably beyond that.
No comments:
Post a Comment