I have been searching the forum and the net for hours on how to apply physics to sprite sheet animations. What I mean is in the spritesheet I have 3 frames of animations, eg. a circle on frame 1, a square on frame 2 and a triangle on frame 3. How do I specify the collision shape for each frame? Let’s say when a ball is colliding with the animating sprite, I want it to react according to the shape of the frame. Appreciate it if anyone can show me how or whether it can be done. Thanks! [import]uid: 48742 topic_id: 8774 reply_id: 308774[/import]
As far as I know, Corona doesn’t (yet) allow you to shift physics bodies depending on spritesheet frames. In fact, it’s pretty much impossible to change a physics body at all after it’s defined… at this point you can add and delete physics bodies, but changing them during runtime isn’t possible (somebody please correct me if this has been fixed in a recent build).
What I would suggest for your dilemma is just creating 3 different images, each with the associated physics body. You won’t have the full amount of animation control that spritesheets give you, but “animating” the object shouldn’t be too difficult with just 3 frames. You could easily flip between the shapes via other methods (timer, loop, etc.), just replacing the object with one of the pre-set shapes.
Not sure if that idea accomplishes everything that you need, but it might be a place to start. [import]uid: 9747 topic_id: 8774 reply_id: 32021[/import]
Hi Guys,
Thanks for the reply. I get what you mean but if I’m creating a street fighter like fighting game with lots of character animations then it will be very difficult to implement. Any idea? [import]uid: 48742 topic_id: 8774 reply_id: 32059[/import]
If you’re doing a Street Fighter type game, then the best solution (in my opinion) would be to create each fighter’s “body” using complex, multi-piece physics bodies. This would be the most accurate… but yes, it’s very hard, under any SDK. You’d have to create each body with a minimum of 6 shapes (head, 2 arms, torso, 2 legs), then fuse them all together with physics joints.
Even after all of that work, the results could be unpredictable. Box2D can be powerful, but “ragdoll” construction can lead to some rather crazy results, unless you are very skilled at constructing the joints properly with the specific properties, rotation limits, etc.
I’m not trying to dissuade you from your idea, I would like to see a Street Fighter type game made with Corona! It’s just a hefty undertaking.
Street Fighter and alot of those classic games worked on a per-pixel collision system, if I’m correct. Corona doesn’t have that (nor did Torque2D which I used before this). Per-pixel collision, as I understand it, is extremely processor-intensive and far more complicated than it might seem… but I could be wrong about that. Anybody want to elaborate on this? [import]uid: 9747 topic_id: 8774 reply_id: 32060[/import]
Ok, I’m thinking about this. Let’s say I am still using spritesheet animations, but instead of defining a fix shape for the physics body, I store the shape of every frame in an array. Then on the enterframe event, I check which frame is being displayed and reset the physics body (maybe remove the current one and add a new one). You think this will work? [import]uid: 48742 topic_id: 8774 reply_id: 32061[/import]
Hmm, after some trial I think I might have found the solution (sort of). Will post it here after I do more test. [import]uid: 48742 topic_id: 8774 reply_id: 32076[/import]
Did you end up getting this worked out?
(D’oh! Sorry, for some reason I thought this post was older. No rush
[import]uid: 41243 topic_id: 8774 reply_id: 32144[/import]
Yeap, it works. Here’s what I did after digging around the forum for similar solutions:
–i stored all the shape of the animation frames in a table named spriteShape
–after sprite declarations and etc…
local spriteInstance = sprite.newSprite(spriteSet)
spriteInstance.x = 100
spriteInstance.y = 100
spriteInstance:play()
local spriteBody = display.newRect(0, 0, 0, 0)
spriteBody.x = spriteInstance.x
spriteBody.y = spriteInstance.y
physics.addBody( spriteBody, “static”, spriteShape[spriteInstance.currentFrame] )
local enterFrameFunction = function ()
spriteBody:removeSelf()
spriteBody = display.newRect(0, 0, 0, 0)
physics.addBody( spriteBody, “static”, spriteShape[spriteInstance.currentFrame] )
end
Runtime:addEventListener( “enterFrame”, enterFrameFunction)
Basically instead of adding the sprite instance itself into the physics, I add a dummy shape (spriteBody) with the same location and remove and recreate it on every enterframe event and set the shape according to the spriteInstance’s current frame. Those shapes are predefined and stored in the table spriteShape.
There might be a 1 or 2 frame difference and I’m not sure whether this method will impact the performace of the app but it’s all I can think of now. Anyone with a better solution/optimisation? [import]uid: 48742 topic_id: 8774 reply_id: 32230[/import]
That is a clever solution, to have a dummy physics object. In fact, that’s probably the way to go for adding physics to the tile code in the feature request forum, hm…
[import]uid: 12108 topic_id: 8774 reply_id: 32551[/import]
Does this has been fixed by Corona team? please. please. [import]uid: 62853 topic_id: 8774 reply_id: 39324[/import]