Jump to content

p2js just for collision detection


DeViLaToR
 Share

Recommended Posts

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

  • 2 weeks later...

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/DoIuf

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 2 months later...

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

 Share

  • Recently Browsing   0 members

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