PDA

View Full Version : freeskier89→ 3. Terrain Collisions



freeskier89
January 22nd, 2006, 03:48 PM
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[i]);
}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[i]));
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 :)

Sinister Shadow
January 22nd, 2006, 03:58 PM
Whoa! Very funky!

freeskier89
January 22nd, 2006, 03:59 PM
It does not work in firefox... so you must view it in IE at the moment. I believe this is caused by the onLoad event not being called... I'll try to get it fixed soon

Thanks Sinister! :)

Sciurus
January 22nd, 2006, 11:57 PM
Hey, it seems to work in firefox if you press space now.

Gelatine Cow
January 23rd, 2006, 07:57 AM
Awesome! :thumb2:

Works for me in Firefox btw...

Stratification
January 23rd, 2006, 02:12 PM
Awesome! :thumb2:

Works for me in Firefox btw...

Does it generate the terrain for you in Firefox? It doesn't for me, though the ball still works. Very slick entry freeskier.

freeskier89
January 23rd, 2006, 02:53 PM
Thanks! :D

To have it work in firefox you need to click on the swf then press the space bar.

JoshuaJonah
January 23rd, 2006, 02:57 PM
wow.... free that is the best one in my opinion.

lunatic
January 23rd, 2006, 03:31 PM
Very clever! Works just fine for me in FF. :love:

hybrid101
January 24th, 2006, 12:09 AM
cool. the physics work perfectly

freeskier89
January 24th, 2006, 12:14 AM
cool. the physics work perfectly Thanks hybrid, lunatic, and defective!

Its not quite perfect. If you through the ball horizontally to hit a steep slope it has a jerky motion because it only tests the y position for hits not the x. But I am rather happy result anyway though. :hugegrin:

freeskier89
January 29th, 2006, 04:59 AM
Alright I added a second version that I am not sure is too legal, (multiple initializers in for loops) that displays the terrain better, but removes the ability to generate new terrains with the spacebar. :)

hybrid101
January 29th, 2006, 05:35 AM
better than ever, although i noticed that when i threw the ball to the upper right, it never came back. i think it went out of the boundaries...:)

ElectricGrandpa
January 29th, 2006, 08:31 PM
Alright I added a second version that I am not sure is too legal, (multiple initializers in for loops) that displays the terrain better, but removes the ability to generate new terrains with the spacebar. :)

Yeah, I'm pretty sure that this:
for (g=0, lineStyle(10, 0xFFFFFF, 70),beginFill(0xFFFFFF, 100); g<20; g++) {

is illegal, but I imagine there's some way you could probably squeeze it in somewhere else?

freeskier89
January 29th, 2006, 08:45 PM
Shucks, I figured... Do you think that something like this is legal?


for (g=0; g<20; g++) {
g == 0 ? yverts=[lineStyle(10+beginFill(0xFFFFFF, 100), 0xFFFFFF, 70)] : yverts.push(g+Math.random()*50);
}


I have no clue if that code will work my guess is that 10+beginFill() = 10; if something is undefined flash usually treats it as 0.

NiñoScript
January 30th, 2006, 12:23 AM
maybe you would like to use my function to chain your stuff :) (http://www.kirupa.com/forum/showthread.php?p=1764643#post1764643)

Patch^
March 15th, 2007, 01:01 PM
I sent a PM but I was wondering if anyone else can help me out a bit.

just wondering is there a way so that a movieclip or something could be used for the terrain/ground instead? If so how could it been done?

If you can't, I understand, keep up the great work.

as you can tell I'm a bit of a n00b lol.

freeskier89
March 15th, 2007, 02:11 PM
Yeah there is a way. I did it for someone else a long time ago. This weekend I will try to find the file. It has got to be somewhere :)

Patch^
March 15th, 2007, 02:18 PM
cheers!! :) Thanks a lot. I'll keep checking like crazy!

Patch^
March 17th, 2007, 02:40 PM
hey mate, any luck finding anything?

freeskier89
March 18th, 2007, 04:19 AM
I haven't found it yet, mainly because i believe it is located on a laptop that i do not have access to right now. I was going to look today, but due to Delta Airlines' crappy management, I am now in a hotel instead of back home... so I can't get at that computer right now. I'll keep trying though! :D

Patch^
March 18th, 2007, 09:40 AM
ok cool. Thanks :)

Patch^
March 18th, 2007, 08:25 PM
Sorry bump the thread etc, but when do you think you will be able to show it? Because I hope to use a part of it for something really soon (within a day or so if it's possible). Again thanks for your support :) .

freeskier89
March 19th, 2007, 04:55 PM
Arg... I looked and I had one other person look on their computer, and I cannot seem to find it anywhere. I am very sorry about that. I will sure let you know if I find it anywhere, but it is not looking very likely. :(

freeskier89
March 19th, 2007, 05:49 PM
Never mind, I just found it :D. I'll get it to you soon.

Patch^
March 19th, 2007, 06:08 PM
AWESOME!! Thank you so much. Should be interesting =)

Digitalosophy
March 19th, 2007, 06:13 PM
That's awesome

freeskier89
March 20th, 2007, 12:19 AM
Alright here it is... it is as easy as arranging the dots. You need to be aware of five things.

1. Line segments should never be on top of each other. If you were to slice the stage into vertical sections, only one segment should be in each slice. (Kind of like the vertical line test for seeing if a graph is a function in algebra class). 2. It starts to get more buggy as you increase the steepness of the lines. 3. Make sure to always position the pink dot that makes up one section to the left of the green one. 4. A segment is made up of a pair of movieclips (sX and cX) where X is a number. 5. To make a new segment copy and paste a new sX and cX clip and rename it to s(X+1) and c(X+1)

Just position the dots around your movieclip. Hope this helps and sorry it took so long for me to find it!

Pasquale
March 20th, 2007, 08:01 AM
:D thats how we did the bouncy balls on a clip I made - good ol' freeskier :thumb:

BS
March 20th, 2007, 09:38 AM
Fun good job!

Patch^
March 20th, 2007, 11:39 AM
thanks mate, i'll play around with it and let you know how I get on :)

Patch^
March 20th, 2007, 01:39 PM
This piece of flash is really cool, but after all that trouble I don't think I'll be able to use it for my project as I planned :(. However it has given me some ideas for a future project.

Thanks for all the help freeskier89 :D

freeskier89
March 20th, 2007, 07:06 PM
No problem :) Let me know if you have any questions with your future project ;)