Jump to content

Super simple collision detection for JavaScript


NessEngine
 Share

Recommended Posts

Hi all,

 

For one of my projects I wrote a smart & lightweight collision detection lib and decided to publish it. It's not a physical engine, its just the collision part, but made extremely easy and efficient. something like this:

var world = new SSCD.World();world.add(new SSCD.Circle(new SSCD.Vector(100, 100), 75));if (world.test_collision(new SSCD.Vector(105, 125))){    alert("Yeah!")}

Key features:

 

  • Collision detection with the following shapes:
    • Points
    • Circles
    • Rectangles
    • Lines
    • Line strips (for complex shapes)
    • Composite shapes
  • Collision-World manager to hold and manage all collision shapes.
  • Grid-based optimizations for efficient collision testing.
  • Can easily handle massive worlds with lots of shapes.
  • Collision filters and tags.
  • Built-in debug rendering to show the collision world with all the objects in it (+ camera support).
  • Lightweight minimalistic API.

 

You can get it here: https://github.com/RonenNess/SSCD.js

Any feedback / ideas / Etc. will be appreciated :)

 

thanks,

 

 

Link to comment
Share on other sites

This looks a lot like https://github.com/jriecken/sat-js to me, but slightly simpler. It's cool that you made it, I've been struggling to find such a thing for a long time. Have you used it in a game yet?

 

One "small" recommendation: you should add unit tests. They are worth the time, especially since you are building something purely mathematical and thus very easy to test.

Link to comment
Share on other sites

This looks a lot like https://github.com/jriecken/sat-js to me, but slightly simpler. It's cool that you made it, I've been struggling to find such a thing for a long time. Have you used it in a game yet?

 

One "small" recommendation: you should add unit tests. They are worth the time, especially since you are building something purely mathematical and thus very easy to test.

 

Hi adrigau, thank you for the feedback :)

 

When I was working on my project I didn't know about sat-js (somehow I missed it in my search), if I did I could use it as a base and build my collision-world logic and simplified API as a wrapper over it. sat-js looks pretty good! 

 

I'm using this lib for a (hobby) game I'm working on, but it will take some time until it sees light. its basically a procedurally-generated endless top-down world, which I think is the best use-case for sscd:

 

screen1.png

 

I totally agree about the tests, its a huge thing currently missing. I should also add some performance tests too.

 

What I find most important to add in next version is convex polygons collision and transformations (scale & rotation), which I feel like critical features. so while at it I might throw in few tests.

 

note: while I don't have convex polygons I do have line-strips, which can make up any shape. but unlike polygons, line strip will only detect collision when crossing the lines and not if tested completely inside the shape. so I feel like polygons are still missing.

Link to comment
Share on other sites

  • 4 weeks later...

Have you done any comparative performance analysis on your collision detection... say for a circle, vs. a simple square that one might code up? 

 

I do a not very complicated collision detection within my game in the Phaser update function,  but I'd much rather use a circle.  However, performance is extremely important in update....

Link to comment
Share on other sites

  • 3 weeks later...

You can do a very easy circle collision detection avoiding Math.sqrt which i observed to increase performance a lot, i hope you get the idea through code:

    AI.prototype.intersect = function(circle1,circle2) {        return this.double(circle1.radius + circle2.radius) >= this.getDistance(circle1,circle2);    };    AI.prototype.getDistance = function(circle1,circle2)    {        return this.double(circle2.x - circle1.x) + this.double(circle2.y - circle1.y);    };    AI.prototype.double = function(arg)    {        return arg*arg;    };
Link to comment
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...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...