# Gravity and Collision in CS4

I worked on a new Flash project, but I cant seem to find a good solution for how I want my objects to collide.

In my project I have: a Ball(moving object) and a Floor(not moving).

The ball is positioned over the floor and drops onto the floor through the gravity that is applied with the code.

Code:

Code:
```var gy:Number = 0;
var gravity:Number = .2

function ballMovement(event:Event):void{
if(ball_mc.hitTestObject(floor_mc)){
ball_mc.y = floor_mc.y-23 //the -23 is because the ball has a diameter of 46 and seems to me calculating from the middle, i figured this out through trial and error
gy=0  //set the gravity to zero once it is on the floor, so it stops moving
} else { //this part does the gravity
gy+=gravity;
ball_mc.y+=gy;
}
};

My problems are these.

First: the -23 in the code is what I don't like.

Second: every single object that deals with collisions would need to be put into the if statement.

Bottom line:
I Don't like this code!

Is there a way to have a global gravity in flash AS3. In which you could choose what objects you want to be affected by it?

Is there a way to have a block of code that separates moving from still objects and will not let any objects collide(not overlap)?

• ###### 1. Re: Gravity and Collision in CS4

an object's x,y properties reference its registration point.  if you want to determine the edge nearest the colliding object, use the appropriate factor.

in the simple situation you have, you're looking at the reg point + ball.height/2 because your reg point is probably in the center of the ball.  in more complex situations (like two balls colliding from any direction) you would use trig to determine the points on each ball where the collision occurs.

add each object you want to evaluate in your enterframe loop to an array and loop through that array on each enterframe loop.