Collisions

Hello.

I am currently building a game in which my main sprite is no other than… captain america! :smile:
To make him swing the enemies and walk to reck thanos(does he really?) I used a sprite sheet. So far so good, however now I want him to actually hit enemies. I was wondering how I can manage to detect the collision exactly. I can detect the bounding boxes collision with arcade physics, however I wanted to detect only when his fist hits the enemy. With pure JS I used a function to detect this pixel perfect collision, using the alpha value of each pixel. I was wondering if there was any way to do that in phaser.

30
(not colliding)

08
(colliding)

Thank you for your time!!

Usually, in game of this type, you must create a new hitbox (or a new Phaser Sprite) when the player execute a attack.

1 Like

How can I perform that?

Looks pretty cool. Have you looked at Physics Editor (https://www.codeandweb.com/physicseditor) I think that along with matter.js can be what you need. There’s a cool example at: https://labs.phaser.io/view.html?src=src\physics\matterjs\advanced%20shape%20creation.js

I haven’t tried it yet, but might help you.

Take care,
DaSoup

1 Like

Hello man. Thank you for your answer.

I actually have tried it out. It’s a nice feature, however I found myself having to change body physics all the time. I managed to do that(despite the lack of documentation and information about that), by using the setBody() function, passing as parameter the physics texture, the x and y(yea, you have to specify it too). This worked fine for texture changing, however not so great for my sprite position and gravity. Because it’s constantly modifying the x and y, the physics don’t have time to ‘act’ properly, so when I jumped with my sprite it looked a bit awkward. Moving left and right seemed to work just fine.

After that I found that I needed to also rotate my sprite body physics, because my sprite sheet only has one direction of the sprite, and when I want to rotate it on animation I just do a .setFlipX(true) and works like a charm. I was hoping there was a function like that for physics, but no, nothing. So I had to add body physics on every moving direction. More trouble… There’s when I decided to stop trying because I have a deadline for this project and if I keep messing around with this I think I will always have a stone in my shoe.

What I hope phaser creators do is to document and give more examples about this framework, because I felt like it wasn’t yet at the level of Arcade Physics and it has much more potencial.

Oh and also I hope that someone starts an opensource project like Physics Editor, because it is payed ^^

Any ideas, thoughts about this problem and experiencies with this framework are welcome to the discussion! Cheers

Hi amserra,

You say you wish for an open source editor like Physics Editor. I actually started an open source physics body editor called Physics Shaper. You can find it on GitHub here (https://github.com/jaredyork/physics-shaper), but you’re not able to export JSON properly yet. Right now I’m trying to figure out the format for Physics Editor, but also exploring programming my own parser. I started this project with the same frustrations for the lack of an open source 2D physics body editor. Hopefully you and others may be able to find value in the project once it’s up and going. :slight_smile: If anyone sees this thread and would like to contribute to the open source project, please feel free.

1 Like

@jaredyork such good news! That’s amazing. Definitely a project to keep an eye on. Best of luck!

Hello!

@amserra did you ever find a simple solution to what you are trying to do?

I too would like “pixel-perfect” collision detection for my sprites and am baffled as to why the physics body is built from the bounding box around the image rather than only the non-transparent pixels of the image/animation…

I have opened a GitHub issue here about this to see if we could maybe add this functionality to the framework as I’m sure anyone trying to handle collisions between non-rectangular sprites would benefit greatly from this! Hopefully Rich or someone else who knows the framework well can give us some clarification on this! :pray:

Btw your artwork / animations are awesome! :grin:

You can adapt your js function to work on the processCallback:
https://photonstorm.github.io/phaser3-docs/Phaser.Physics.Arcade.Collider.html

If your function detect a pixel perfect collision return true else return false, If true the collideCallback is called and you can handle the collision.

For detecting if the captain america has hit an enemy you can check for the current animation frame:

captainAmerica.anims.currentFrame.textureFrame

Thanks @BlunT76!

Are there any examples of how to use this processCallback function to check for the pixel-perfect collision? I’m still confused about what is really the difference between these two callbacks.

I never see any example with pixel perfect collisions with arcade physics… it is generally not needed, you can do a lot with square and circle bodies. All the old platformers (8bit and 16bit era) use square boxes with the AABB collision detection, like arcade physics do.

You must think your game with:

  • whate you see
  • and what happens behind

Perfect pixel collision is extremely heavy, and no real game uses it primarily.

I advise you to create an arcade body big enough to get the whole sprite.
After the collision occurs, you can apply a mixture of zones and pixel verification to make sure the collision is the way you would like.

Another way to do it, compromising performance a little more, is to create a container with your main sprite and several zones.

https://photonstorm.github.io/phaser3-docs/Phaser.GameObjects.Particles.Zones.html

It depends a lot on the type of game or need. Even in 3d games multiple colliders are used

https://forum.unity.com/threads/creating-gameobjects-with-multiple-colliders-image-heavy.123811/

3 Likes

1 Like

For some reason this won’t work at all for me. I don’t get it. All I get is a rectangular bounding box, no custom shapes.