0

I'm trying to create a air-hockey game, in flash using AS3.

At the moment, I am using an enter frame function to check the positioning of the 3 objects, 2 paddles and a ball, then it checks to see if they are in contact, if they are it then initiates a detect collision function.

However it's checking every time the frame is loaded, and at 25 fps, this is quite a lot and the app lags.

Any ideas, or better ways to do this?

Thanks in advance.

3
  • How is the rest of your game set up? Do you have a single render cycle? Are the game sprites complex or do they have a simple box/circle character? How do you calculate the collision detection? Do you use the built-in functions, or do you do your own math? Commented Jan 4, 2011 at 15:47
  • At the moment the sprites, are very simple, three circles. Collision detection, did myself, it checks the distance between to the two circles, see's if it is equal to the the radius of the two circles. Commented Jan 4, 2011 at 15:48
  • I wouldn't think that running this every frame would be very expensive as it's only a few objects. I'd stick with using the radius calcs, but regarding optimizations, you can make sure that you exit your comparative logic where it makes sense. IE) if the velocity of all items is 0. if puck is on left half, no need to check the right paddle (if you are sticking to air hockey rules!), etc.. Commented Jan 4, 2011 at 17:29

3 Answers 3

1

Two pythagoras statements are slowing your game down? At 25fps? Something is wrong -- that should not be the case.

Remove the collision detection completely and check that you get your 25fps back, then add statements a line at a time until the slowdown reappears.

Check you are not calling your collision code more than once (well, twice) per frame.

Remember that you can test for collision without using Math.sqrt:

function circlesTouching(circle1:Point, circle1Radius:Number, circle2:Point, circle2Radius:Number):Boolean { var dx:Number = circle1.x - circle2.x; var dy:Number = circle1.y - circle2.y; var minDist:Number = circle1Radius + circle2Radius; return (dx*dx) + (dy*dy) < (minDist * minDist); } 

(You will still need sqrt to resolve the collision but this should be pretty rare.)

However in my experience, even though Math.sqrt is the slowest part of Pythagoras, it's still easily fast enough to manage two calls per frame at 25fps. It sounds like something else is wrong.

Sign up to request clarification or add additional context in comments.

Comments

1

If you need to check something periodically, I guess the enterFrame event is a suitable mechanism to do it.

You don't mention if you use the built-in hit test functions or not, so I thought I'd mention them: hitTestObject and hitTestPoint.

1 Comment

Didn't use hitTestObject as it includes the bounding box... because it was circles thought it would be easier to check to see if the distance is the sum of the two circe's radius
1

Have you tried a timer?

var timer:Timer = new Timer(250); // 4 times a second timer.addEventListener(TimerEvent.TIMER, onTimer); timer.start(); private function onTimer(ev:TimerEvent):void { checkCollision(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.