2

Here's the thing : I have a ball bouncing using Tweens, and I want to detect collisions between the ball and platforms (only when the ball is falling down).

But, my solution is testing for collisions in an ENTER_FRAME loop, so when the ball speed is to high, at a x frame the ball is above the plateform, and at a x+1 frame the ball is below the platform, so my loop never detects collision (because the ball and the platform never really collide).

Here's the jump method of my ball :

public function jump():void
{
    TweenLite.killTweensOf(this);
    TweenLite.to(this, jumpSpeed, {y:250, ease:Cubic.easeOut});
    TweenLite.to(this, jumpSpeed, {delay:jumpSpeed, y:stage.stageHeight-this.height, ease:Cubic.easeIn, onComplete:jump});
}

And here's what's executed in my ENTER_FRAME loop :

for each (var platform:Platform in platforms) 
{
    if (ball.hitTestObject(platform) &&  ballPreviousY < ball.y)
        ball.jump();
}
ballPreviousY = ball.y;

I already started to work on a solution using the physics engine Box2D, but I would like to know if there was a simpler solution to that problem.

Florent
  • 12,310
  • 10
  • 49
  • 58
qbeauperin
  • 591
  • 4
  • 21
  • You would essentially have to write a lot of math that is otherwise already accounted for in libraries like box2d, which already has handy arguments built in to make for easy use...I see you're already using greensock though, which does have some handy tricks built in to be able to simulate collision effects, which may or may not be ample depending on the nature of your collision. but for true power of a physics engine, box2d is the way to go! – MaxG Oct 08 '12 at 17:16
  • I've read something recently about tweening and hitTestObject together not working, perhaps it's your case as well. – Vesper Oct 08 '12 at 18:14
  • While the 3rd party libraries solve these problems, the issue you are dealing with can be solved using Verlet integration to track the motion of your objects. This allows you to detect a collision **before** the rendering is done, so you can handle it before you render anything. There are a lot of resources available on this topic (do a search for "Euler versus Verlet integration"). I recently read about it in a book titled [AdvancED Game Design w/Flash](http://www.apress.com/9781430227397). – Sunil D. Oct 08 '12 at 21:15
  • what if you use the onUpdate property in the tween to check for the collision rather than an enter frame? – Ronnie Oct 08 '12 at 23:16

1 Answers1

0

This is a common problem in collision detection - small, fast objects moving through thin obstacles. What your looking for is sweep testing

Instead of testing for intersection between two static shapes, we can instead create new shapes by sweeping the original shapes along their trajectory, and testing for overlap between these swept shapes.

From N Tutorial A - Fast Moving Objects, (I'd recommend reading all their stuff if your interested in more detail in collision algorithms).

For example, if you had a 1px by 1px square and it was travelling at 5px per frame along the x axis, you'll want to distort your square so that becomes a 6px by 1px rectangle for collision testing. Catching anything that it would hit next frame, but not anything more.

Circles are a little more difficult because when you stretch them they become ellipses (which are far harder to check collisions for). See this answer if you want to go the ellipse route.

Community
  • 1
  • 1
Jono
  • 3,949
  • 4
  • 28
  • 48