Jump to content

2D Sprite Animation on a Box's face


royibernthal
 Share

Recommended Posts

Hi, welcome back, hope you had fun, and thanks.

Let's put it this way.  It is EASIER if the sprites are ordered in one line.  But there's ways around it.

Let's look at the sprite sheet.  player.png

If I said that the top row of sprites... it's bottom edge... is 128 pixels up the Y axis, would you buy that?  Sure ya will.  :)
If I said that the middle row of sprites... it's bottom edge... is 64 pixels up the Y axis, would you buy that?  I'll loan ya the money so you can say "yes".  :)
And you can predict the rest. Our lone standing sprite on the bottom row... is 0 pixels up the Y axis.  Let's leave him out of the equation for now.

http://www.babylonjs-playground.com/#20MSFF#43

There ya go.  Lines 31 - 50 got some serious adjustings.

See line 11?  We start at 0, 128.  Top row... (128 up the Y axis)... leftmost edge (0 amount of X axis).  In other words, we start at the lower left corner of the zeroeth sprite in the top row (22 frames in the top row... 0 to 21).

Now let's look at line 36.  It checks if the animation is on the 21st sprite of the 128-high row (top row).  If so... guess what?  We need to move to the 2nd row of sprites... and you can see how it is done.

Line 42... it checks if we are on the 21st sprite of the 2nd row (64-high row).  If so, yep, head back up to the first row, 0th sprite.  Cool, huh?

And I imagine you could figure out how to get to the 3rd row, but we don't care about him right now.  Your animation speed is that value seen in line 50.

Hope this helps.

Link to comment
Share on other sites

Yeah I got the concept of going over a sprite I was just wondering if there's something in bjs sprite preventing you from achieving that :) I see in your updated PG that you used spriteLocation to address that issue.

Anyway, I got my (our) own solution to compete with yours :P Changing uv after creating the box was a piece of cake using getVerticesData and setVerticesData like you suggested.

I don't usually (never actually) go open source but since you helped me so nicely, here you go:

https://github.com/royibernthal/BabylonJS-2D-Animation

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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