DeViLaToR Posted December 18, 2014 Share Posted December 18, 2014 Hello everyone! I am making a game and i have some objects with triangular shape.I use p2js just for collision detection with onBeginContact (everything else is handled manually, like movement etc.). So i am wondering if there is a less expensive way to detect polygon collision. Thanks DeViLaToR 1 Link to comment Share on other sites More sharing options...
DeViLaToR Posted December 24, 2014 Author Share Posted December 24, 2014 Bump! Link to comment Share on other sites More sharing options...
wayfinder Posted December 24, 2014 Share Posted December 24, 2014 Not built in. You could program your own stripped-down broadphase but it feels to me like that would fail my personal cost/benefit analysis Link to comment Share on other sites More sharing options...
Wavertron Posted December 25, 2014 Share Posted December 25, 2014 You can set every P2 body as a sensor, eg:this.game.physics.p2.enable(sprte);sprte.body.data.shapes[0].sensor = true;It may not be super efficient, but it will mean the objects/sensors will just float through each other when they "collide", the onBeginContact and onEndContact events will fire. Link to comment Share on other sites More sharing options...
DeViLaToR Posted December 27, 2014 Author Share Posted December 27, 2014 You can set every P2 body as a sensor, eg:this.game.physics.p2.enable(sprte);sprte.body.data.shapes[0].sensor = true;It may not be super efficient, but it will mean the objects/sensors will just float through each other when they "collide", the onBeginContact and onEndContact events will fire.This is what i 'm using right now @wayfinder True.I will search if there is any js library for polygon collision and check if prerformance improves. Link to comment Share on other sites More sharing options...
wayfinder Posted December 27, 2014 Share Posted December 27, 2014 PIXI includes PolyK, which is an awesome lightweight poly library. Perhaps you will find something useful in there! http://polyk.ivank.net/ Link to comment Share on other sites More sharing options...
kleepklep Posted January 6, 2015 Share Posted January 6, 2015 I have the same exact need! Is there any way to create a sensor that can be moved by updating it's x and y coordinates that has no other physics involved? I can't seem to find the magic combination of settings to make this happen. Did you find a lightweight JS library for polygon collision detection? These look promising: http://codepen.io/soulwire/pen/DoIufhttps://github.com/jriecken/sat-js Link to comment Share on other sites More sharing options...
kleepklep Posted January 8, 2015 Share Posted January 8, 2015 I achieved success using SAT.js using the function below. Since my the player's poly never changes I'll predefine that to optimize. The obstacle poly would also be predefined and passed to the function. function collisionCheck(obstacle) { var V = SAT.Vector; var P = SAT.Polygon; // points are negative because objects are bottom right aligned var carPoly = new P(new V(car.x, car.y), [new V(-200, -184), new V(-6, -76), new V(-90, -25), new V(-285, -132)]); var obstaclePoly = new P(new V(obstacle.x, obstacle.y), [new V(-200, -184), new V(-6, -76), new V(-90, -25), new V(-285, -132)]); var response = new SAT.Response(); var collided = SAT.testPolygonPolygon(carPoly, obstaclePoly, response); //console.log(car.key, obstacle.key, collided); if (collided){ car.tint = 0xFF0000; }}This class my be more robust and not as lightweight as I need, but it's probably more efficient than turning on the P2JS system just for collision detection. Pooya72 and DeViLaToR 2 Link to comment Share on other sites More sharing options...
DeViLaToR Posted March 25, 2015 Author Share Posted March 25, 2015 I achieved success using SAT.js using the function below. Since my the player's poly never changes I'll predefine that to optimize. The obstacle poly would also be predefined and passed to the function. function collisionCheck(obstacle) { var V = SAT.Vector; var P = SAT.Polygon; // points are negative because objects are bottom right aligned var carPoly = new P(new V(car.x, car.y), [new V(-200, -184), new V(-6, -76), new V(-90, -25), new V(-285, -132)]); var obstaclePoly = new P(new V(obstacle.x, obstacle.y), [new V(-200, -184), new V(-6, -76), new V(-90, -25), new V(-285, -132)]); var response = new SAT.Response(); var collided = SAT.testPolygonPolygon(carPoly, obstaclePoly, response); //console.log(car.key, obstacle.key, collided); if (collided){ car.tint = 0xFF0000; }}This class my be more robust and not as lightweight as I need, but it's probably more efficient than turning on the P2JS system just for collision detection.Excellent library. Exactly what i needed!! Link to comment Share on other sites More sharing options...
Recommended Posts