freeskier89→ 3. Terrain Collisions

This one took a bit of math, and is way more condensed than my other entries. If you want a more logical, non 25 line version see this thread : http://www.kirupa.com/forum/showthread.php?t=206051&page=2.

V1

createEmptyMovieClip("ball", 0).lineStyle(3, 0xC7DCE2, 40);
ball.beginGradientFill("radial", [0xFFFFFF, 0xF0F8F7, 0xC7DCE2], [100, 100, 100], [0, 70, 240], {matrixType:"box", x:-15*1.5, y:-15*1.5, w:15*2.3, h:15*2.3, r:0});
for (var t = 0; t<Math.PI*2; t += Math.PI/30) ball.lineTo(15*Math.cos(t), 15*Math.sin(t));
ball.velocity = {x:11, y:0};
ball.onPress = function() {this.pressed = true};
ball.onRelease = ball.onReleaseOutside=function () {this.pressed = false};
ball.onEnterFrame = function() {
 this.pressed ? this.velocity = {x:(_xmouse-this._x)/2, y:(_ymouse-this._y)/2} : null;
 this.old = {x:this._x, y:this._y};
 this._x += this.velocity.x;
 this._y += this.velocity.y++;
 if (this._y>300+(this._x-(Math.floor(this.old.x*(yverts.length-1)/555)*(555/(yverts.length-1))))*Math.tan(-Math.atan2((yverts[Math.ceil(this.old.x*(yverts.length-1)/555)])-(yverts[Math.floor(this.old.x*(yverts.length-1)/555)]), 555/(yverts.length)))-this._width/2-(yverts[Math.floor(this.old.x*(yverts.length-1)/555)])) {
this.velocity={x:(.7*Math.sqrt(Math.pow(this._x-this.old.x, 2)+Math.pow(this._y-this.old.y, 2)))*Math.cos((Math.atan2(this.old.y-this._y, this.old.x-this._x)-Math.PI)+Math.PI+Math.atan2((yverts[Math.ceil(this._x*(yverts.length-1)/555)])-(yverts[Math.floor(this._x*(yverts.length-1)/555)]), 555/yverts.length)-Math.PI), y:-(Math.sqrt(Math.pow(this._x-this.old.x, 2)+Math.pow(this._y-this.old.y, 2))*.7)*Math.sin((Math.atan2(this.old.y-this._y, this.old.x-this._x)-Math.PI)+Math.PI+Math.atan2((yverts[Math.ceil(this._x*(yverts.length-1)/555)])-(yverts[Math.floor(this._x*(yverts.length-1)/555)]), 555/yverts.length)-Math.PI)};
  this._y = 300+(this._x-(Math.floor(this.old.x*(yverts.length-1)/555)*(555/(yverts.length-1))))*Math.tan(-Math.atan2((yverts[Math.ceil(this.old.x*(yverts.length-1)/555)])-(yverts[Math.floor(this.old.x*(yverts.length-1)/555)]), 555/(yverts.length)))-this._width/2-(yverts[Math.floor(this.old.x*(yverts.length-1)/555)]);
 }
 if (this._x>555-this._width/2 || this._x<this._width/2) {
  this._x = [this._width/2, 555-this._width/2][int(this._x>555-this._width/2)];
  this.velocity.x = -this.velocity.x;
 }
};
_root.onKeyDown=function(){
 Key.isDown(Key.SPACE) ? onLoad(clear()) : null;
 }
Key.addListener(_root);
onLoad=function(){
for (g=0; g<20; g++){
 g==0 ? yverts=[] : yverts.push(g+Math.random()*50);
}
lineStyle(10, 0xFFFFFF, 70);
moveTo(0, 300-yverts[0]);
for (i=1; i<yverts.length; i++) lineTo(555/(yverts.length-1)*i, 300-yverts*);
}

V2

createEmptyMovieClip("ball", 0).lineStyle(3, 0xC7DCE2, 40);
ball.beginGradientFill("radial", [0xFFFFFF, 0xF0F8F7, 0xC7DCE2], [100, 100, 100], [0, 70, 240], {matrixType:"box", x:-15*1.5, y:-15*1.5, w:15*2.3, h:15*2.3, r:0});
for (var t = 0; t<Math.PI*2; t += Math.PI/30) {
 ball.lineTo(15*Math.cos(t), 15*Math.sin(t));
}
ball.velocity = {x:11, y:0};
ball.onPress = function() {
 this.pressed = true;
};
ball.onRelease = ball.onReleaseOutside=function () {
 this.pressed = false;
};
ball.onEnterFrame = function() {
 this.pressed ? this.velocity={x:(_xmouse-this._x)/2, y:(_ymouse-this._y)/2} : null;
 this.old = {x:this._x, y:this._y};
 this._x += this.velocity.x;
 this._y += this.velocity.y++;
 if (this._y>300+(this._x-(Math.floor(this.old.x*(yverts.length-1)/555)*(555/(yverts.length-1))))*Math.tan(-Math.atan2((yverts[Math.ceil(this.old.x*(yverts.length-1)/555)])-(yverts[Math.floor(this.old.x*(yverts.length-1)/555)]), 555/(yverts.length)))-this._width/2-(yverts[Math.floor(this.old.x*(yverts.length-1)/555)])) {
  this.velocity = {x:(.7*Math.sqrt(Math.pow(this._x-this.old.x, 2)+Math.pow(this._y-this.old.y, 2)))*Math.cos((Math.atan2(this.old.y-this._y, this.old.x-this._x)-Math.PI)+Math.PI+Math.atan2((yverts[Math.ceil(this._x*(yverts.length-1)/555)])-(yverts[Math.floor(this._x*(yverts.length-1)/555)]), 555/yverts.length)-Math.PI), y:-(Math.sqrt(Math.pow(this._x-this.old.x, 2)+Math.pow(this._y-this.old.y, 2))*.7)*Math.sin((Math.atan2(this.old.y-this._y, this.old.x-this._x)-Math.PI)+Math.PI+Math.atan2((yverts[Math.ceil(this._x*(yverts.length-1)/555)])-(yverts[Math.floor(this._x*(yverts.length-1)/555)]), 555/yverts.length)-Math.PI)};
  this._y = 300+(this._x-(Math.floor(this.old.x*(yverts.length-1)/555)*(555/(yverts.length-1))))*Math.tan(-Math.atan2((yverts[Math.ceil(this.old.x*(yverts.length-1)/555)])-(yverts[Math.floor(this.old.x*(yverts.length-1)/555)]), 555/(yverts.length)))-this._width/2-(yverts[Math.floor(this.old.x*(yverts.length-1)/555)]);
 }
 if (this._x>555-this._width/2 || this._x<this._width/2) {
  this._x = [this._width/2, 555-this._width/2][int(this._x>555-this._width/2)];
  this.velocity.x = -this.velocity.x;
 }
};
for (g=0, lineStyle(10, 0xFFFFFF, 70),beginFill(0xFFFFFF, 100); g<20; g++) {
 g == 0 ? yverts=[] : yverts.push(g+Math.random()*50);
}
moveTo(0, 300-yverts[0]);
for (i=1; i<yverts.length; lineTo(555/(yverts.length-1)*(i++), 300-yverts*));
lineTo(555, 400);
lineTo(0, 400);
endFill();

Press the spacebar to generate a new terrain in the first file.

Please let me know what you think!
-freeskier89 :slight_smile: