Search code examples
androidanimationcollision-detection

animation with collision detection?


I am having trouble figuring out the best way to set up animation with collision detection. I have a fully functional version of falldown (the popular calculator game), using a handler and onDraw() to draw rectangles for the rows and a bitmap ball. However, this animation skips (looks laggy) and so I am trying to switch the animation over to a tween animation.

My question is a conceptual one: how do i detect collisions in the middle of an animation? I see the sideways animation as taking one of two routes: a) I animate in very small steps. However, I imagine this would look just as bad as the current animation set-up. b) I set an animation all the way to the left or right of the screen, but every step in the animation checks to make sure the ball is not going through a row.

I face a similar problem with the downward motion of the ball. Is it possible to constantly access/update the position of the ball (stored as an integer) and then cut the animation short if it is colliding with something? Is it possible to have the ball animate downward if and only if it is not above a piece of the row? There must be some way to detect collisions inside of the animation but I don't know how.

edit: if the animation class isn't suitable for games, how am I supposed to animate my game? Like I said, the handler-ondraw system I have makes the ball skip around, even when i set the delay to 1 millisecond.


Solution

  • One thread for UI canvas draw methods and one thread for Game Logic / Anything non UI related.

    One thread updates objects positions etc... and the other UI thread just draws them onto the screen.

    In this manor your game logic thread is continuous and on every path updates X,Y positions and handles things like collision detection etc... The draw thread just takes those updated X,Y positions and draws bitmaps onto a canvas.

    You can use the SurfaceView as the canvas on which to draw the collection of bitmaps/image/sprites. You can look at the numerous Android Game examples provided by Google for a quick and dirty look at how this is accomplished but it is generally how I described above.