Create a flash draw game like Line Rider or others - A different approach - Part 3
Maybe some of you did not notice it, but there was a real pearl in a comment of Create a flash draw game blike Line Rider or others - A different approach - Part 2 post.
Jesuson studied all the source codes and came up with this comment:
----------------
I have been testing it and it chash a lot of times. I would change next things:
1. For the collision response, to calculate the out velocity I would use
vx1=-cosa*w.vx sina*w.vy;
vy1=(-cosa*w.vy-sina*w.vx)*bounce;
w.vx = -cosa*vx1-sina*vy1; //ball X velocity
w.vy = -cosa*vy1 sina*vx1; //ball Y velocity
Now it has got aceleration and velocity won’t be infinite.
2. There is some problems when you test the perimeter of the ball for collisions. The first is you don’t do it in order. For do it in order change
px=w._x w.radius*Math.sin(i*360/precision);
py=w._y-w.radius*Math.cos(i*360/precision);
by
px=w._x w.radius*Math.sin(i*Math.PI/precision);
py=w._y-w.radius*Math.cos(i*Math.PI/precision);
The second is, what happend if there are diferent points of collisions? for expample if ball collide with floor and wall, you don’t must take it like a single collision, becouse the response won’t be good.
For fix it I take all the collisions points what are together (For detect than it´s together you have to detect them in order like I said)and put it in a collision group array, and then I calculate diferents responses with diferent arrays.
Here you can see what I’m speaking about:
Bad method –> http://denvish.net/ulf/130607/55530_collisions4.fla
Good method –> http://denvish.net/ulf/130607/55602_collisions5.fla
Look what happend in two cases when the ball hit the wall.
note: use up/down to acelerate/brake.
----------------
I downloaded both methods and the "good" one is... really good!
This is the actionscript with some comments from the author
-
gravity = .2;
-
bounce = 0;
-
precision = 360;
-
w1.vx = 0;
-
w1.vy = 0;
-
w1.vAngle = 0;
-
w1.radius = w1._width/2;
-
w1.col = false;
-
w1.cArray = [];
-
onEnterFrame = function () {
-
w1.vy += gravity;
-
checkWL1(w1, level);
-
checkWL2(w1, level);
-
control(w1);
-
w1._x += w1.vx;
-
w1._y += w1.vy;
-
w1._rotation += w1.vAngle;
-
scrollWorld();
-
};
-
////////////////// Colisiones /////////////////////////////
-
function checkWL1(w, level) {
-
w.sumX = 0;
-
w.sumY = 0;
-
w.collisions = 0;
-
for (i=1; i<precision; i++) {
-
px = w._x+w.radius*Math.sin(i*Math.PI*2/precision);
-
py = w._y-w.radius*Math.cos(i*Math.PI*2/precision);
-
checkDL(px, py, w, level);
-
w.cArray.push([px, py, w.col2]);
-
}
-
if (w.collisions>0) {
-
checkArray(w);
-
//collisionResponse(w);
-
}
-
w.cArray = [];
-
}
-
function checkWL2(w, level) {
-
w.col = false;
-
for (i=1; i<precision/10; i++) {
-
px = w._x+(w.radius+5)*Math.sin(i*360/precision);
-
py = w._y-(w.radius+5)*Math.cos(i*360/precision);
-
checkDL(px, py, w, level);
-
}
-
if (w.collisions>0) {
-
w.col = true;
-
}
-
}
-
function checkDL(px, py, w, l) {
-
if (l.hitTest(px, py, true)) {
-
w.sumX += px;
-
w.sumY += py;
-
w.collisions++;
-
w.col2 = true;
-
} else {
-
w.col2 = false;
-
}
-
}
-
function collisionResponse(w) {
-
w.hitX = w1.sumX/w.collisions;
-
w.hitY = w.sumY/w.collisions;
-
dx = w._x-w.hitX;
-
dy = w._y-w.hitY;
-
d = Math.sqrt((dx*dx)+(dy*dy));
-
d2 = w.radius-d;
-
angle = Math.atan(dx/dy);
-
if (dy>0) {
-
cosa = Math.cos(angle);
-
sina = Math.sin(angle);
-
} else {
-
cosa = -Math.cos(angle);
-
sina = -Math.sin(angle);
-
}
-
w.sina = sina;
-
w.cosa = cosa;
-
w._x += d2*sina*2;
-
w._y += d2*cosa*2;
-
v = Math.sqrt((w.vx*w.vx)+(w.vy*w.vy));
-
vx1 = -cosa*w.vx+sina*w.vy;
-
vy1 = (-cosa*w.vy-sina*w.vx)*bounce;
-
w.vx = -cosa*vx1-sina*vy1;
-
w.vy = -cosa*vy1+sina*vx1;
-
}
-
function checkArray(w) {
-
cont = 20;
-
w.sumX = 0;
-
w.sumY = 0;
-
w.collisions = 0;
-
for (i=0; i<precision-1; i++) {
-
if (w.cArray[i][2]) {
-
w.sumX += w.cArray[i][0];
-
w.sumY += w.cArray[i][1];
-
w.collisions++;
-
} else {
-
if (w.collisions>0) {
-
if (cont == 0) {
-
collisionResponse(w);
-
w.collisions = 0;
-
w.sumX = 0;
-
w.sumY = 0;
-
cont = 20;
-
} else {
-
cont--;
-
}
-
}
-
}
-
}
-
}
-
///////////////////// Control ///////////////////////////////
-
function control(w1) {
-
if (Key.isDown(38) && w1.col && w1.vx<10) {
-
w1.vx += .3;
-
}
-
if (Key.isDown(40) && w1.col && w1.vx>-3) {
-
w1.vx -= .1;
-
}
-
}
-
///////////////////// Camera ////////////////////////////
-
function scrollWorld() {
-
if (w1._x>350 && w1.vx>0) {
-
level._x -= w1.vx;
-
w1._x -= w1.vx;
-
}
-
if (w1._x<200 && w1.vx<0) {
-
level._x -= w1.vx;
-
w1._x -= w1.vx;
-
}
-
}
And this is the result (up and down arrows to move the ball)
Now I am going to study more in depth this work because I find it really interesting, so expect a tutorial based on this code during next days.
And this is the source code hosted on my site, just in case some day the link provied by Jesuson should disappear.
Tell me what do you think about this post. I'll write better and better entries.
They can be easily customized to meet the unique requirements of your project.
8 Responses to “Create a flash draw game like Line Rider or others - A different approach - Part 3”
Leave a Reply
Trackbacks
-
Create a flash draw game like Line Rider or others - A different approach at Emanuele Feronato on
June 15th, 2007 2:43 pm
[...] Create a flash draw game like Line Rider or others - A different approach Published April 8th, 2007 in Flash and Users contributions. April 21st update: part 2 released June 15th update: part 3 released [...]
-
Create a flash draw game like Line Rider or others - A different approach - Part 2 at Emanuele Feronato on
June 15th, 2007 2:44 pm
[...] June 15th update: part 3 released [...]

(6 votes, average: 4.5 out of 5)
Nice, also will there be a tutorail coming up showing us how to go in the toher direction because at the moment the ball or objest only move one way and constantly accelerates!
Very good Though.
Wow, thats brillian. My game is nearlly finished. YOu are truley the greatest Emanuele in the world [:
Hello!!
I’m happy to see you liked this. I want to tell you about another problem with my code.
When I work with the array of points, this array represent the collision points of the circle, and calculate the collisions zones(points together),but, if there are points in the beginning and in the end of the array, THIS IS THE SAME ZONE.
You will see this problem if you change the gravity to a negative value, then, when the ball collision with the roof the response is not good.
I will post my solution (is a good one for my game, but is not the best) with better comments, sorry for this in last one, I didn’t know you would post it here.
http://denvish.net/ulf/200607/12156_collisions5.fla
See you!!
Forgot to show you this first example. This is a game a made when I started to study your tutorials.The collisions are based in distance between a line and a point, and not using hitTest.
The game hadn’t got good scores on newground :(, so I would like to share with all of you the code.
Notice than this one has got a worst script level, so don’t take it like a good way doing things.
http://denvish.net/ulf/200607/13907_LineMotoCross_1.0.fla
how would you make it so when you get tho a point like end so it goes to the next level?????
splashy5, You would have to make a hitTest and when the the ball hit the dot, it would take you to the frame with the next level.