Jump to content

one physics body - several sensors - how to get sensors to set variables


valueerror
 Share

Recommended Posts

1 collision body 4 sensors for top/left/right/down 

 

i want the sensor hovering above the player to set object_above = true; and the sensor to the right of the player to set object_right=true;  and so on..  

 

is this possible? and if so - how?

 

i thought maybe i could set onBeginContact events to the sensors individually or somehow find out which sensor reported the collision ??!

 

thx in advance  :)  

Link to comment
Share on other sites

Hey,

it's far too late so it's for the moment only a gist in coffee script I can give you. But this is the core of my collision event system and helps me managing all of my collision events. The base principals: Focus on one object that collides with many others (player centric view). It's like on-to-many. So you always have on constant in your collisions. That's true for most games with a player or something else as the enter of the game. This view allows you to create a listener specific for collision of other objects with this main object. The rest is saving and finding the correct objects during a collision with the main object.

 

Gist with three JS classes. Try to convert it to JS if you are not comfortable to read coffee script.

https://gist.github.com/georgiee/7758583691d33a2c1af1

 

Usage:

Do not copy & past those. It's only example code I never tested.

//create a sprite & a p2 body with shapesball = game.add.sprite(0,0,'ball')myCircle = ball.body.addCircle(20)//CollideableEntity is a helper class so you can reference you sprite from your shape collision later.//You can also pass an array of shapes and of course you can create multiple//instances of this CollideableEntity with the same sprite but with different p2 shapes.ballCollisionEntity = new PinballGame.Core.CollideableEntity(ball, myCircle)//create the contact listeners. This one focus on the job of looking for ALL collision ONLY with the ball (through myCircle)//you will later register all those things that you're interested in to collide with the ball.ballCollisions = new PinballGame.Core.EntityContactListener(game, ballCollisionEntity)//Now register all those things you want to get notified then they collide with the ball.//create another thingythingy = game.add.sprite(0,0,'thingy')thingyShape1 = thingy.body.addCircle(20)thingyShape2 = thingy.body.addCircle(20)thingyShape3 = thingy.body.addCircle(20)thingyCollisionEntity = new PinballGame.Core.CollideableEntity(thingy, [thingyShape1,thingyShape2])@ballCollision.with(thingyCollisionEntity, handleEntityContact, handleEntityEndContact)//you could go on and register other things. My current class manages the collision detection of 50 objects with a ball and triggers a hit on the detected sprite.handleEntityContact: function(collisionEntity, contact){  entity = collisionEntity.getEntity()  if(entity === thingy){    console.log('thingy was hit!')  }}

@valueerror

To retrieve a single shape easily look at that post from neoRiley  (we are the gang of p2 ;) ) http://www.html5gamedevs.com/topic/6054-collider-used-as-a-trigger/#entry36596

 

In this thread he posted an example of using the FixtureList class you might not know yet! I thought this was the core of your question.

 

Regards George

Link to comment
Share on other sites

Ah and by the way for easy checks:

 

A contact always contains among other the following properties:

contact.bodyAcontact.bodyBcontact.shapeAcontact.shapeB

So retrieving a body or shape is always possible when you know the counterpart (shapeA belongs to bodyA, shapeB belongs to bodyB)

Link to comment
Share on other sites

thank you very much george - i guess this is the easiest answer to that question..    so every sensor gets a reference 

    playersensor_up = player.body.addRectangle(10,4,0,-25);    playersensor_up.sensor=true;    playersensor_down = player.body.addRectangle(10,4,0,25);    playersensor_down.sensor=true;

and then i can find out which sensor reports the collision by  

player.body.onBeginContact.add(myfunction,this);

and then do whatever is necessary  (for example)

function myfunction(bodyA,shapeA,shapeB,contactEquation){    if (shapeA == playersensor_up){ console.log('up'); }} 
Link to comment
Share on other sites

I would also like to know the answer to this ;)

 

I was expecting to be able to check the body that was passed in the event but I can't seem to match anything up that says "you hit the ice sensor" 

 

I was an idiot and have relied too much on decent code editors that have nice intellisense / autocomplete for learning api's ;)   I hadn't realized the full signature of the collision arguments included what was being hit.

 

Level.prototype.enterCollisionHandler = function(body, shape_0, shape_1) {     if( shape_1.sensor && shape_1.id === this.ice[0].id )     {        console.log("HIT ICE");    }    else this.collisionManager.addCollision(body);};

 

Shape_1 is what you've hit.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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