Gravity issue? Object slides through another object instead of sitting on top of it!

Hello!

I have a file where two objects fall from the top of the stage and bounce and then eventually stop (using gravity and hit test).

You can grab the objects (a ball and a box — each has a button symbol n the movie clip) and toss them and they bounce off each other and off the 4 sides of the stage.

The issue is when one of the objects is dropped directly on top of the other object, after it stops bouncing, it slides down until it hits the bottom of the stage (in this case, the box slides down in front of the ball, and the ball slides down in back of the box).

Is this because the gravity is pulling the object down to the bottom of the stage even though there’s another object in its way?

How can I fix this so that the objects will still bounce off each other, and when dropped directly on top of each other object will stop instead of oozing through it (sorry, this is the best way I can describe it :slight_smile: ?

I’ve attached 2 files (Fla 8 and Fla mx 2004).

-------- here is the gravity code on the first frame:


var gravity:Number = 1.2;
var restitution:Number = 0.6;
var friction:Number = 0.9;

stop();

-------- here is the code on the ball


onClipEvent(load) {
	var dragging:Boolean = false;
	var vel:Object = { x: 0, y: 0 };
	var pos:Object = { x: _x, y: _y };
	var old:Object = { x: _x, y: _y };
	var radius:Number = this._width / 2;
	var movie:Object = { width: 400, height: 300 };
}

onClipEvent(enterFrame){	
	if( !dragging ) {
		vel.y += _root.gravity;
		
		pos.x += vel.x;
		pos.y += vel.y;

		if( pos.y + radius > movie.height ) {
			pos.y = movie.height - radius;
			vel.y *= -_root.restitution;
			vel.x *= _root.friction;
		}
		
		if( pos.x + radius > movie.width ) {
			pos.x = movie.width - radius;
			vel.x *= -_root.restitution;
		}
		
		if( pos.x < radius ) {
			pos.x = radius;
			vel.x *= -_root.restitution;
		}
		
		_x = pos.x;
		_y = pos.y;
		
	} else {
		old.x = pos.x;
		old.y = pos.y;
		pos.x = _x;
		pos.y = _y;
		
		vel.x = ( pos.x - old.x ) / 2;
		vel.y = ( pos.y - old.y ) / 2;		
	}	

	
if(this.hitTest(_root.box)) {
        trace("hit");
        // ball and box are colliding, so lets reverse their movements.
        vel.x *= -1;
        vel.y *= -1;
        _root.box.vel.x*=-1;
        _root.box.vel.y*=-1;
        
        /*  these actions could be made more realistic by determining the precise
        position of the ball relative to the box during the collision, for instance,
        determine if the right side is colliding with the left side, so only reverse
        the x velocity, instead of both.  */
    }
    
    //this prevents the ball from leaving the upper extreme of the stage
    if (this._y < 0) {
            vel.y*=-1;
    }  
}

-------- here is the code on the button inside the ball (so you can drag and release the ball)


on(press){
	startDrag(this,false,16,16,384,284);
	dragging = true;
}
on(release, releaseOutside){
	stopDrag();
	dragging = false;
}

-------- here is the code on the box


onClipEvent(load) {
	var dragging:Boolean = false;
	var vel:Object = { x: 0, y: 0 };
	var pos:Object = { x: _x, y: _y };
	var old:Object = { x: _x, y: _y };
	var radius:Number = this._width / 2;
	var movie:Object = { width: 400, height: 300 };
}

onClipEvent(enterFrame){	
	if( !dragging ) {
		vel.y += _root.gravity;
		
		pos.x += vel.x;
		pos.y += vel.y;

		if( pos.y + radius > movie.height ) {
			pos.y = movie.height - radius;
			vel.y *= -_root.restitution;
			vel.x *= _root.friction;
		}
		
		if( pos.x + radius > movie.width ) {
			pos.x = movie.width - radius;
			vel.x *= -_root.restitution;
		}
		
		if( pos.x < radius ) {
			pos.x = radius;
			vel.x *= -_root.restitution;
		}
		
		_x = pos.x;
		_y = pos.y;
		
	} else {
		old.x = pos.x;
		old.y = pos.y;
		pos.x = _x;
		pos.y = _y;
		
		vel.x = ( pos.x - old.x ) / 2;
		vel.y = ( pos.y - old.y ) / 2;		
	}	
}

-------- here is the code on the button inside the box (so you can drag and release the box)


on(press){
	startDrag(this,false,16,16,384,284);
	dragging = true;
}
on(release, releaseOutside){
	stopDrag();
	dragging = false;
}

I appreciate any help anyone can give me to fix this issue, I don’t understand enough yet about how these objects are interacting to fix this — but I hope to learn from you brilliant Kirupians!) Thanks!

Hondo311

Anyone? I know it’s challenging, but someone must know how to do this . . . i have faith in you Kirupa Forumians!

Hondo311

Well, you may have the ball going too fast. Alot of times, collision detection problems are caused be cause an object is moving too fast and ‘jumps’ over sometrhing. for example, if the hit area (1 pixel high) starts off at (60, 100) and an obstacle 10 pixels high is below it at (60, 120). The hit area has to move between 20 and 30 pixels down to hit it. If it moves more, it just skips the obstacle.

~Cody