• Advertisement

Archived

This topic is now archived and is closed to further replies.

Sprite size change = sliding

This topic is 5034 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Some friends of mine and I have recently begun development of a Java 2d game library. Unfortunately, we''ve had some trouble designing the rendering engine. The Sprite class will have two integer fields that describe it''s location. It will use these fields to draw itself. Images in Java, however, are drawn from the top-left corner of the image. So the problem is, what happens if the sprite''s size changes when it switches to another frame or performs an attack? For example, when Link stands still, his sprite is 16x16 pixels. His location is (0,0). If he is facing north and uses his sword, his sprite will grow taller. However, he is still being drawn at (0,0). This will cause him to appear to slide downwards. We have come up with a few solutions to this problem. The first thing we though of was to make all the frames for each sprite the same size (the size of the biggest frame). Then we realized that this would cause each sprite''s bounding box to be unnaturally large, and would thus make sprite collision detection (if bounding boxes collide, do bitwise AND on every pixel''s alpha in the intersecting bounding boxes) very inefficient. One of my friends suggested that we use the above ''biggest frame'' method and calculate the width of the opaque part of the sprite for each frame. Then we would use this number to detect sprite collision. This seemed generally anti-Java to me. Also, it only takes into account the x-axis. A method I devised would be to use the smallest bounding box size possible for each frame (to optimize collision detection) and ensure that, in every single frame, the sprite is centered in the exact center of the bounding box. (i.e. the middle of link''s head would always be in the center of the bounding box) Java would then draw the sprite at it''s center. This would create extra work on the sprite artist''s part, and seems like a bad idea. So, what do you think? How can we keep sprites from sliding when they grow in the north or west directions? Thanks, Tommy

Share this post


Link to post
Share on other sites
Advertisement
i was going to suggest that you relocate the "location" x,y to the center of the sprite. That way, it grows but does not slide. However, you have to refactor a lot of your code to take into account that your position is now the center of the box and not a corner.

Might work...

And the other idea you already suggested as well: make all sprites the same size but change the bounding box per-frame.

Share this post


Link to post
Share on other sites

  • Advertisement