Advertisement

Arcanoid AI: problem in the ball

Started by September 03, 2006 03:04 PM
11 comments, last by apatriarca 18 years, 2 months ago
Good morning. I'm developing a little Arcanoid game. Now i have some problems with the ball. I've developed with success a system with which the ball when touch wall, it change direction. here is the algoritm

//PosPalla: current ball position
//SpostamentoPalla: the movement of the ball gained.
	PosPalla.x += SpostamentoPalla.x;
	PosPalla.y += SpostamentoPalla.y;
		if (PosPalla.x > 34)
		{
			PosPalla.x = 34;
			SpostamentoPalla.x = -SpostamentoPalla.x;
		}


		if (PosPalla.x < -4)
		{
			PosPalla.x = -4;
			SpostamentoPalla.x= -SpostamentoPalla.x;
		}

		if (PosPalla.y < -20)
		{
			PosPalla.y = -20;
			SpostamentoPalla.y = -SpostamentoPalla.y;
		}


		if (PosPalla.y > 20)
		{
			PosPalla.y = 20;
			SpostamentoPalla.y = -SpostamentoPalla.y;
		}

		D3DXMATRIXA16 d3dmat;
		D3DXMatrixIdentity(&d3dmat);
		D3DXMatrixMultiply(&Sfera.WorldMatrix,&Sfera.WorldMatrix,D3DXMatrixTranslation(&d3dmat,SpostamentoPalla.x,SpostamentoPalla.y,0.0f));

As you can see when the ball touch wall it change x or y. But now there is a problem: I've to do collisions with pad: for the pad i've decided to do
if (Pospalla.y == 0) {//code for the pad}
But as you can see when ball touchs wall it change x or y. and then when i check if y = 0 i see that y is 0 when it is going up...it's a very big problem Can you help me??
To understand better

immaginela8.jpg
Advertisement
I guess I don't see why Y is 0 at the top of the screen, too. Usually, this is done like a graph in math, where 0,0 is one point, and it increases as you get further away (or decreases if you're going negative). EG: (scuse the ASCII art)

y50 | |   .(25,25) | |00-------50          x


So, there's only one line where Y == 0. Prehaps you should explain your coordinate system better, since it definately seems different from what I'd expect.
Ciao,
try use it...
Prova ad usare questo.. io ho fatto cosi. invertendo l'angolo ogni volta in base a dove urta la palla. più urta laterale più l'angolo incrementa.

void DX8CLASS::ControlloPalla()
{
if (direct == 0)
{
mball.chgy(mball.rety()-mball.velocita*cos(mball.angolo));
mball.chgx(mball.retx()-mball.velocita*sin(mball.angolo));
}

if (direct == 1)
{
mball.chgy(mball.rety()+mball.velocita*cos(mball.angolo));
mball.chgx(mball.retx()+mball.velocita*sin(mball.angolo));
}

if (mball.rety() < 10)
{
direct = 1;
mball.angolo = -mball.angolo;
}

if (mball.rety() == 530)
{
direct = 0;
mball.angolo = -mball.angolo;
}

if (mball.retx() < 5 || mball.retx() > 795)
{

mball.angolo = -mball.angolo;
}

if (mball.retx() >= mpad.retx() && mball.retx() <= mpad.retx()+70 && mball.rety() >= mpad.rety())
{
direct = 0;

if (mball.retx() <= (mpad.retx()+70 / 2))
mball.angolo = (mball.angolo + mpad.direzpad * 0.05);
else
mball.angolo = -(mball.angolo + mpad.direzpad * 0.15);
}
Ciao,
Why you have y = 0 at the top and at the bottom of the screen?
You have a problem with the collisions. When the ball hits a wall you have to find the time when the ball hits it (have the correct x or y to hit the wall). Then calculate the time that remains and move the ball correctly in the new position.
You have to do the same with the pad. You can use something like the code posted by luchetto. You can also consider the velocity of the pad when the ball hits it. If the pad is faster the angle is wider.

Sorry for my english. Se non hai capito ti posso mandare un messaggio privato in cui te lo spiego in italiano...
Italiani? Qui??? Fantastico! Voglio i vostri contatti msn!! SUBITO!!!
Azz l'euforia di trovare gente compentente italiana mi ha impedito di leggere le vostre risposte!
Advertisement
As you can see from the image when ball touchs wall it change itself in y =-y
In this mode when the ball it's up y = 0, while it should not be 0. Every time that ball touchs wall it change itself and then one time y = 0 at top, othet times y = 0 down.
I need that y = 0 only down and never at top.
I think using an absolute coordinate system is better. Why you invert the y coordinate? There isn't any reason to do that.
The collision between a circle and an axis aligned line is very easy. You only have to verify if the x (or y) value of the center is over a threshold (R unit from the wall where R is the radius). To find the time of collision you have to find the intersection between the line P + vt (where P is the start position, v the velocity and t the time) and the line that represent the wall (translated of R unit). When you have found the point of intersection and the time of intersection you have to move the ball using the new reflected velocity.
I've done this strange system becouse i do not know how to change direction of the ball when it touchs the ball.
I can explain you other if you have a msn-messenger contact.
I have sent to you a PM with my MSN contact...
If the walls are aligned with the axis you have to invert only one component of the velocity (the perpendicular one).

This topic is closed to new replies.

Advertisement