How to add a physics body to each frame of a sprite sheet? - sprite

Let's say I have a Sprite Sheet of a character in a game. There are 4 frames of him walking to the right direction, his shape changing in each frame. How do I define the shape for each individual frame when I add a physics body to this sprite?
I'm under the impression that I have to split the sprite sheet into individual images and define the shape for each image, but if that's the case I don't know what to do from there, programming-wise.

You cannot have different physics bodies for each frame of a sprite. You can try to get it close for most of the frames. If you choose to go the separate frame route, you will have to write your own animation function to animate the sprites.

Please don't try to do that. It'll completely kill your physics simulation.
The problem with it is that the mass of a shape is calculated from its area. If you have a character animation - e.g. walking, swinging his - he would gain weight when the arms are stretched and loose weight when the arms are next to the body.
Try not to be too exact about the collision shapes - the player usually won't see the difference anyways. Just make it good enough.
One option is to approximate the shape by using a union or intersection of your animation frames.
Take a look at this tutorial - it's for cocos2d - but the physics shape creation section is basically the same for corona.
http://www.raywenderlich.com/33525/how-to-build-a-monkey-jump-game-using-cocos2d-2-x-physicseditor-texturepacker-part-1
Near the center is a more detailed explanation how to create collision shapes for animations.

Related

Clipping a circle with svg... or?

Currently I am using canvas to draw the gold circle, then snip off the areas half way to adjacent stars with globalCompositeOperation : destination-out, then paste the result into the main canvas.
I am contemplating changing to svg for this.
The closest method I have found is clip masks. I would have to create a set of clip-masks for each circle (star), this seems excessive if there were 2000 stars.
Another way would be to create polygons, but the math required to calculate the path may be beyond my capabilities.
So before I go down either of these routes, I would like to know if there is a better way or which of the above methods are recommended.

AnyLogic - Move presentation frame dynamicaly

I've got a model with cars moving over the road. To make roads length similar to real-life sizes I had to change the scale so the cars turned into points (4px*2px).
Are there any facilities in AnyLogic 7 PLE to, for ex, zoom one of the cars and track it?
Yes, it is possible. If you want to zoom in one car and follow it in 3D (like if the car has GoPro at top), use Camera object with dynamic coordinates. Railway Station example model and its cameraOnTrain object illustrates the concept.
In case if you want to do similar thing in 2D space (GTA2 view mode), you may drag & drop empty Group element. In its On Draw action use the code:
getPresentation().getPanel().setOffsets( 300-agent.getX(), 300-agent.getY());
The code will constantly move frame, so car always will appear with in the bottom right corner of the 300x300 square, drawn from top left corner. Zoom can be adjusted with mouse wheel, or with code as well:
getPresentation().getPanel().setZoom( double value);

Pygame sprites always on top example?

I was wondering if someone could give me simple example of how to always draw sprites in pygame on --the top layer-- whilst an animation is being blitted on screen?
The senario I'm trying to solve is having an animated sprite (eg a man walking on the spot) and various other objects passing him, whilst he is still animated.
My solution so far has the animation layering on top of the "passing objects" which is not what I want.
Thanks in advance.
I think you've partially answered your own question here.
It's a matter of organizing what gets drawn first. Some side-scrolling 2D games use a "layer" solution, in which there is a background layer, a middleground layer, and a foreground layer, and the drawing system renders one layer after another.
I've also seen Pokémon top-down style games simply sort the sprites by their vertical position, so sprites "nearest" to the camera are drawn last, and thus on top of the other sprites.
See how the current implementation of Scene2D in libGDX gives each Actor a z-index property which can later be used to organize Actors into layers.
Just draw your sprites with a LayeredUpdates group:
class pygame.sprite.LayeredUpdates
LayeredUpdates is a sprite group that handles layers and draws like OrderedUpdates.
LayeredUpdates(*spites, **kwargs) -> LayeredUpdates
You can set the default layer through kwargs using ‘default_layer’ and an integer for the layer. The default layer is 0.
If the sprite you add has an attribute layer then that layer will be used. If the **kwarg contains ‘layer’ then the sprites passed will be added to that layer (overriding the sprite.layer attribute). If neither sprite has attribute layer nor **kwarg then the default layer is used to add the sprites.
and give your sprites the correct layer value.

How to draw shapes in the proper order when rendering?

I am trying my hand at writing a 3d graphics engine, but I am having some trouble with drawing the shapes in the correct order.
When I translate the points of triangles into window space, i.e. the 2-dimensional space that directly correlates to position on the screen, in addition to an x and y position of each point, I also assign them a depth variable that stores how far away from the viewer that point was in 3d space.
At the moment, the only shapes I am rendering are triangles. My current render order algorithm sorts the triangles by the average depth of their 3 points. I knew when I started it that it would not be perfect, but I wanted a placeholder for testing.
For testing purposes, I constructed a square box with an open top, each side being a different color and made from 2 triangles, as shown below:
As you can see from the image above, the algorithm I am using works most of the time. However, at certain angles and positions, the triangles will be rendered in the wrong order, as show below:
As you can see, one of the cyan triangles on the bottom of the box is being drawn before one of the yellow triangles on the side. Clearly, sorting the triangles by the average depth of their points is not satisfactory.
Is there a better method of ordering shapes so that they are rendered in the correct order?
The standard method to draw 3D in correct depth order is to use a Z-buffer.
Basically, the idea is that for each pixel you set in the color buffer, you also set it's interpolated depth in the z (depth..) buffer. Whenever you're about to paint the next pixel, you first check that z-buffer to validate the new pixel if in front of the already painted pixel.
On top of that you can add various sorts of optimizations, such as sorting triangles in order to minimize the number of times you actually paint the color buffer.
On the other hand, it's sometimes required to do the exact opposite in order to properly handle transparency or other "advanced" effects.

Erase Pixels From Sprite Cocos2d-JS

I'm getting the feeling this won't be possible, but worth asking anyway I guess.
I have a background sprite and a foreground sprite, both are the same size as the window/view.
As the player sprite moves across the screen I want to delete the pixels it touches to reveal the background sprite.
This is not just for display purposes, I want the gaps the player has drawn or "dug" out of the foreground layer to allow enemies to travel through, or objects to fall into. So hit detection will be needed with the foreground layer.
This is quite complex and maybe Cocos2D-JS is not the best platform to use, if not possible could you recommend another which would be easier to achieve this effect with?
I believe it's possible, but I'm not capable of giving you a proper answer.
All I can say is that you'll most likely have two choices:
a. Make a physics polygonal shape and deform it, then use it as a "filter" to display your terrain image (here's a proof of concept example in another language using box2d).
b. Directly manipulate pixels and use a mask for collision detection (here's pixel-perfect collision detection in cocos2d-js, sadly I've got no info in modifying pixels).

Resources