Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Using collision detection without physics


flameiguana
 Share

Recommended Posts

Hello.

 

I am really enjoying coding with phaser (besides a few weird behaviors) so far.

One thing I would like to do is use the collision detection system for things that don't involve physics.

I mean why start from scratch when the system is there already?

 

In my game prototype I am trying to detect if there is something in front of the player sprite by adding an invisible sprite in front of the player and checking for collision with other things. I set the sprite body to immovable, meaning it won't be affected physically by other objects. Since the objects I want to be able to interact with the player are also immovable, a problem arises. I believe the physics collision system is just ignoring the overlap, because my callback function isn't called. It would be nice if one could still detect overlap between two objects even though collisions won't be resolved.

 

If anyone has alternative solution in the meantime, I would like to hear from you.

Link to comment
Share on other sites

Not 100% sure if this is your issue but since I'm beating around with phasers phisycs let me show you this:

 

//this is in the update

game.physics.collide(balls, balls, ballHitBallHandler, ballHitBallProcess, this);

 

//two custom functions

function ballHitBallHandler(ball1, ball2) {
 return true;
}
function ballHitBallProcess(ball1, ball2) {
 return true;
}

 

So the code above do what what you need... at least what i understand from your post.

So collision is detected but a custom process and resolve is used... if you don't do nothing with the bodies/sprites 

than in the game nothing is going to happened but the collision was marked.

 

And also check the examples and docs.

 

Edit:

I just saw again that you said that the two objects are immovable...

if that is true... how can they collide if the two aren't moving (except a overlap on creation) ?

I mean are you sure that the player object must be immovable ?

Edited by Mike
Link to comment
Share on other sites

I just create the second object as a normal sprite and then call a custom function in update to see if a collision occurs

function collides (a,      {    	if(a != undefined)    	{	    	return !(		        ((a.y + a.height) < (b.y)) ||		        (a.y > (b.y + b.height)) ||		        ((a.x + a.width) < b.x) ||		        (a.x > (b.x + b.width))		    );	    	}}

where a and b can be Phaser.Sprite objects

Link to comment
Share on other sites

@ Mike The property immovable is set, but really I am still moving them manually independently of the physics system. You can visually see them collide, but the physics system doesn't  apparently, because I did try to use a simple collision handler and collision process (whats the difference between the two anyway?) but they were ignored.

 

@ReachTheFlag : I was going to do something like that as a last resort, thanks though.

 

@rich : That worked beautifully. I just passed the sprite.bounds property to check for collision.

 

Thanks all for the quick responses.

Link to comment
Share on other sites

Yes two immovable are ignored: from phaser source

 

        //  Can't separate two immovable bodies
        if (body1.immovable && body2.immovable)
        {
            return false;
        }
 
and for tiles too:
        //  Can't separate two immovable objects (tiles are always immovable)
        if (body.immovable || body.deltaX() == 0 || Phaser.Rectangle.intersects(body.hullX, tile) == false)
        {
            return false;
        }

 

and @rich already give us a solution. :)

Link to comment
Share on other sites

Just to explain further, two immovable objects basically cannot ever separate from each other, because they are both set to refuse or give out any impact, but they most certainly could overlap. I need to enhance physics.overlap() to cover more object types - right now it only handles sprite vs. sprite, but it should cover all the same ones as collide does really.

Link to comment
Share on other sites

Hi All, 

 

I ran into the same thing. I was working around it by setting customSeparateX/Y to avoid the physics system actually moving objects, but it was still setting the "touching" properties, which I use to trigger animations and sound (such as when a character lands on the ground).

 

I'm just working around it like this right now, but it doesn't use the fancy quadtree lookup code:

collectibles.forEach(function(collectible){    if(game.physics.overlap(avatar, collectible))    {        collectHandler(avatar, collectible);    }});

As an aside: this approach also works much better tracking intersections with a manually key-framed weapon and hitboxes. Occasionally, from one frame to the next, the weapon would go from not overlapping to fully embedded - and newly embedded objects don't trigger the handler callbacks either. 

 

---

 

Rich, I posted this as a GitHub issue as well - would you prefer this sort of discussion be kept to the forums?

Link to comment
Share on other sites

Hello all, 

 

Can you please provide an example using the Rectange.bounds method?

 

I have a group of sprites which I move using tweens and they don't seem to collide with other sprites.

 

I reckon that on the update loop I have to check for each sprite inside the group if collides with another sprite (or group?)

but I cannot make sense on how the bounds work.

 

Thank you

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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