Jump to content

[P2] animating sprite + custom shape


titmael
 Share

Recommended Posts

Hi,

 

I didn't find examples or explainations for it and my question is pretty simple :

 

My player has animations (from a spritesheet). How to load custom collisions shapes and apply for each frame ? Because the player is moving and performing actions, the shape is not always the same and I need to load a custom shape for each frame.

 

Thx ;)

Link to comment
Share on other sites

There's no way to include body shapes in the animation set so I'd say this, with maybe a cleaner way to access the right body for the frame (an array of shapes with indices matching the relevant frames maybe?) put into the update method of an extended sprite would probably be the best way to do it.

Link to comment
Share on other sites

Most games make do with very simple collision bounds, or collections of rectangles for the bits that matter. When you get complex shapes that need accurate collision, those shapes are usually characters with separate collision shapes for each of their 'parts' - head, body, arms, legs etc.

Link to comment
Share on other sites

Yep, I bought the package with Physics, really worth it !

 

To get a nice rectangle shape for animations I did that in the update function of my Enemy class :

if(this.lastFrame != this.animations.currentFrame.name){    this.body.clearShapes();    this.body.addRectangle(this.texture.width, this.texture.height, 0, 0, 0);    this.lastFrame = this.animations.currentFrame.name;}

So at every animated frame, rectangle is update. This works only for animations (not if you set this.frame = X)

Link to comment
Share on other sites

Since this operations are quite heavy (removing and adding a body) I would suggest you to add simpler bodies like circles, but that's just a suggestion depending on what you need.

 

There's no way to include body shapes in the animation set so I'd say this, with maybe a cleaner way to access the right body for the frame (an array of shapes with indices matching the relevant frames maybe?) put into the update method of an extended sprite would probably be the best way to do it.

Yeah that's what I thought but I was just explaining how I see it working.

Link to comment
Share on other sites

Since this operations are quite heavy (removing and adding a body) I would suggest you to add simpler bodies like circles, but that's just a suggestion depending on what you need.

 

That's what my piece of code do, it adds a rectangle  ;)

Link to comment
Share on other sites

Yes I was just saying that circles are (I think) easier shapes since it's just some kind of distance constraint (a position and a radius).

 

Edit: About your new problem, it is because you don't set the right offset, it is centered to the center of your sprite when it is at its maximum height. You shoud add an offset when addin the shape.

 

For instance:

this.body.addRectangle(width, height, 0, 20, 0);

Now, how to know the correct exact offset ? No idea.

Link to comment
Share on other sites

Very nice, I did that :

 

In the init of my "classe" :

this.defaultSize = {width: this.texture.width, height: this.texture.height};

Then in the update function :

if(this.lastFrame != this.animations.currentFrame.name){    var offsetX = (this.defaultSize.width - this.texture.width) / 2,        offsetY = (this.defaultSize.height - this.texture.height) / 2;    this.body.clearShapes();    this.body.addRectangle(this.texture.width, this.texture.height, offsetX, offsetY, 0);    this.lastFrame = this.animations.currentFrame.name;}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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