# Optimization Tips

Show 40 post(s) from this thread on one page
• 12-01-2006, 05:26 AM
Cimmerian
So, the purpose of using radius this way
Code:

```var radius1 = (clip1._width * clip1._width)/2 var radius2 = (clip2._width * clip2._width)/2```
Code:

```var radius1 = clip1._width/2 var radius2 = clip2._width/2```
Is cos the first way you are taking the whole formula and making everyhting to the power of 2,
So, its faster cos you have to do this
Code:

```var radius1 = (clip1._width * clip1._width)/2 var radius2 = (clip2._width * clip2._width)/2```
Only once, out of each hitTest, instead of having an extra step to calculate the sqrt inside each test,right?
Clever, i wonder if this principle can be used in other things
• 12-01-2006, 12:59 PM
Cimmerian
I may be very dumb, cos i cant understand how you go from this
Code:

```var radius1 = clip1._width/2 var radius2 = clip2._width/2```
To this
Code:

```var radius1 = (clip1._width * clip1._width)/2 var radius2 = (clip2._width * clip2._width)/2```
I mean, if its all to the power of 2, why its not
(clip1._width * clip1._width)/ 4
?
Or this power of 2 has nothing to do?
• 12-02-2006, 05:02 AM
mr_malee
the radius of something is half its circumference

so if a ball has a radius of 10cm its circumference is 20cm
• 12-02-2006, 05:18 AM
Cimmerian
I know that, i dont understand the stuff necessary to drop the sqrt in the distance formula
• 12-02-2006, 05:24 AM
Cimmerian
I dont know if i made something wrong, but please look at this fla
With big clips,in this way
Code:

```var radius1 = (clip1._width * clip1._width)/2; var radius2 = (clip2._width * clip2._width)/2; var radius = radius1 + radius2;```
the test start to gives true without them to touch eah other,but this
Code:

```var radius1 = clip1._width/2; var radius2 = clip2._width/2; var radius = radius1 + radius2; radius = radius*radius;```
dont
• 12-02-2006, 06:28 PM
Ray Beez
Quote:

Originally Posted by mr_malee
the radius of something is half its circumference

so if a ball has a radius of 10cm its circumference is 20cm

WRONG. It's half the diameter.
• 12-02-2006, 07:57 PM
mr_malee
• 12-02-2006, 07:58 PM
SaphuA
What's a circumference?
• 12-02-2006, 08:12 PM
mr_malee
the distance around a circle
• 12-02-2006, 08:27 PM
SaphuA
Ah, ok :)
• 12-04-2006, 06:18 AM
Cimmerian
• 12-04-2006, 06:24 AM
Cimmerian
Since noone bother in makes things clear, i am beginning to think there is no such thing as circle hitTest without sqrt, cos this way has a bug
Code:

```var radius1 = (clip1._width * clip1._width)/2; var radius2 = (clip2._width * clip2._width)/2; var radius = radius1 + radius2;```
But this dont
Code:

```var radius1 = clip1._width/2; var radius2 = clip2._width/2; var radius = radius1 + radius2; radius = radius*radius;```
BUT this way is difficult to use cos you cant calculate the summ of the two radius before any test, cos you dont know what are you going to test, unless you pre calculate all the possible combinations before any hitTest, but with many clips this may be very dificult, and even if more clips are added dynamically?
• 12-04-2006, 07:44 PM
mr_malee
create a circleToCircle hitTest function which accepts two circles and find the radius like that.
• 12-05-2006, 01:15 PM
VENGEANCE MX
Circle to circle collision without a square root is very easy.

function checkCollision(circle1, circle2) {
xdist = circle2._x-circle1._x;
ydist = circle2._y-circle1._y;
}
• 12-05-2006, 02:44 PM
Cimmerian
But what is the advantage in drop the sqrt this way since you have to add the stuff to calculate the power of the radius of the circle?
I mean, take out one thing and add another
OR do you mean that calculate this power stuff is faster them to calculate some sqrt?
• 12-05-2006, 03:09 PM
VENGEANCE MX
Well, lemme put it this way:the radius of the balls will never change, so you can precalculate the square of their radii, but the x and y distances will be changing constantly.
• 12-05-2006, 06:33 PM
mr_malee
thats an interesting way to do circle to circle vengeance, good to know :)
• 12-06-2006, 04:46 AM
Cimmerian
Quote:

Originally Posted by VENGEANCE MX
Well, lemme put it this way:the radius of the balls will never change, so you can precalculate the square of their radii,

Still didnt get it
The way i see, even if you have the two pre calculated sqrt radius, the distance should be compared to the sqrt of the sum of the two radius, not to the sqrt of one radius plus the sqrt of the another
I mean, sqrt(a + b) its not the same as sqrt(a) + sqrt(b),and so as (a+b)*(a+b) too
• 01-12-2007, 11:58 AM
rubbersharkman

FASTER HITTEST FOR DOING TONS OF HIT DETECTION FOR PLATFORMS + PHYSICS

see www.spiritonin.com/radius - to see example in action

When doing a platform based game where you are checking collision on lots of blocks, I found it is much faster to do the hitTest(x,y,true) on the movieclip containing all the blocks, than it is to do a mc.hitTest(mc) on every block.

Problem is I have a bunch of physics stuff based on individual block detection.

I tried a method that I think is faster:

You do Two MC's in the same coordinates in the same timeline - "world"(contains all the blocks) and "worldEdges"(contains dynamically created blocks that are a certain amount larger than the "world" blocks, creating an edge around all of them visually You will see this in the game if you look at the example)

The amount larger is based on the size of your sprites, where the size should be a few pixels larger than half the width of the widest sprite (so if you are doing 32x32 sprites, make the edges ~20 pixels wider in each direction) so if you have a 50wx100h block, it should spawn a 90wx140h block that is 20 pixels up and left of the collision block, but in the "worldEdge" mc

Then when you do the hitest you check to see if the center point of the sprites is hitting "worldEdge" then if it is you check collsion on each platfrom in "world".

If you have different sized sprites, you can create more edge clips ("worldEdgeBig", "WorldEdgeMedium" etc...)

You can also do it for different types of platforms or platform groups to cut down on the number of MC's being tested when an edge is penetrated.

Hope that makes sense... It was a bit messy to set up, but the collision seems to work a lot cheaper especially with lots of enemies bouncing around the screen.

If anyone knows a faster way to check a complex set of block for detection, leg me know.
• 01-12-2007, 07:45 PM
webgeek
It was also recently proven by several of us that for AS3 at least, circle-to-circle collision detection (without the sqrt call) is significantly faster then anything else. The results are outlined in this thread along with sample code: