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

**URL:** <https://forum.kirupa.com/t/gravity-issue-object-slides-through-another-object-instead-of-sitting-on-top-of-it/260521>\
**Category:** flash\
**Created:** [May 16, 2008, 12:34pm UTC](https://forum.kirupa.com/t/gravity-issue-object-slides-through-another-object-instead-of-sitting-on-top-of-it/260521 "2008-05-16T12:34:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hondo311](https://avatars.discourse-cdn.com/v4/letter/h/6a8cbe/32.png) [@hondo311](https://forum.kirupa.com/u/hondo311)\
**Post date:** [May 16, 2008, 12:34pm UTC](https://forum.kirupa.com/t/gravity-issue-object-slides-through-another-object-instead-of-sitting-on-top-of-it/260521/1 "2008-05-16T12:34:07Z")

</div>

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 🙂 ?

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

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

```php

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

stop();

```

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

```php

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)

```php

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

```

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

```php

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)

```php

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

---

<div class="post-metadata">

**Author:** ![hondo311](https://avatars.discourse-cdn.com/v4/letter/h/6a8cbe/32.png) [@hondo311](https://forum.kirupa.com/u/hondo311)\
**Post date:** [May 22, 2008, 5:00pm UTC](https://forum.kirupa.com/t/gravity-issue-object-slides-through-another-object-instead-of-sitting-on-top-of-it/260521/2 "2008-05-22T17:00:13Z")

</div>

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

Hondo311

---

<div class="post-metadata">

**Author:** ![ShadowViper](https://avatars.discourse-cdn.com/v4/letter/s/54ee81/32.png) [@ShadowViper](https://forum.kirupa.com/u/ShadowViper)\
**Post date:** [May 22, 2008, 9:14pm UTC](https://forum.kirupa.com/t/gravity-issue-object-slides-through-another-object-instead-of-sitting-on-top-of-it/260521/3 "2008-05-22T21:14:08Z")

</div>

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
