Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Line Rider game, Flash and Users contributions.

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; i0) {
		checkArray(w);
		//collisionResponse(w);
	}
	w.cArray = [];
}
function checkWL2(w, level) {
	w.col = false;
	for (i=1; i0) {
		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; i0) {
				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.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.