Can't figure out what is wrong with my code

Started by
4 comments, last by Puck 15 years, 9 months ago
Hi, I'm trying to make a game similar to the ones at fallingsandgame.com, but with more realistic physics using vectors. I have the vectors working but there is a problem with collisions... they aren't working. The particles will hit the walls but will run right over each other. This is my physics (actually just vector movement and collisions now) code:

		       		if(p[x][y][2] > 0 && p[x][y][3] > 0)
		       		{
		       	    	for(vx = 0; vx < p[x][y][2]; vx++)
		       	      	{
                        	for(vy = 0; vy < p[x][y][3]; vy++)
                            {
           	    	            if(!pexists(x + vx, y + vy))
                                {
									tmpvx = vx;
									tmpvy = vy;
                                }
                            }
                        }
                    }
		       		if(p[x][y][2] < 0 && p[x][y][3] > 0)
		       		{
		       	    	for(vx = p[x][y][2]; vx < 0; vx++)
		       	      	{
                                 for(vy = 0; vy < p[x][y][3]; vy++)
                                 {
                                        if(!pexists(x + vx, y + vy))
                                        {
                                              tmpvx = vx;
                                              tmpvy = vy;         
                                        }
                                 }
                        }
                    }
		       		if(p[x][y][2] > 0 && p[x][y][3] < 0)
		       		{
		       	    	for(vx = 0; vx < p[x][y][2]; vx++)
		       	      	{
                                 for(vy = p[x][y][3]; vy < 0; vy++)
                                 {
                                        if(!pexists(x + vx, y + vy))
                                        {
                                              tmpvx = vx;
                                              tmpvy = vy;         
                                        }
                                 }
                        }
                    }
		       		if(p[x][y][2] < 0 && p[x][y][3] < 0)
		       		{
		       	    	for(vx = p[x][y][2]; vx < 0; vx++)
		       	      	{
                                 for(vy = p[x][y][3]; vy < 0; vy++)
                                 {
                                        if(!pexists(x + vx, y + vy))
                                        {
                                              tmpvx = vx;
                                              tmpvy = vy;         
                                        }
                                 }
                        }
                    }
pexists():

int pexists(int x, int y)
{
	if(x < 0 || x >= w - 10 || y < 0 || y >= h)
		return 1;
	else if(p[x][y][0] == 1)
		return 1;
	else
		return 0;
}
moveParticle():

void moveParticle(int x, int y, int x2, int y2)
{
	if((pexists(x, y)) && (!pexists(x2, y2)))
	{
		if(x2 < w && x2 >= 0)
		{
			p[x2][y2][0] = 1;
			p[x2][y2][1] = p[x][y][1];
			p[x2][y2][2] = p[x][y][2];
			p[x2][y2][3] = p[x][y][3];
			p[x2][y2][5] = p[x][y][5];
			p[x2][y2][6] = p[x][y][6];
			p[x2][y2][7] = p[x][y][7];
			p[x2][y2][4] = 0;
			p[x][y][0] = 0;
			p[x][y][2] = 0;
			p[x][y][3] = 0;
			p[x][y][4] = 0;
		}
	}
}
Thanks for any help :) Edit: Sorry about the messed up tabs... I hadn't figured out how to turn off Dev-C++'s "Smart Tabs" feature when I wrote this, and the forum made it worse...
Advertisement
I found a rather stupid flaw in the coding of movement to the left, now the collisions on that side are really bizarre (work sometimes, sometimes not, sometimes the particles stop too soon) and the right collisions don't work at all.

New code (now with better tabs):

					if(p[x][y][2] > 0 && p[x][y][3] > 0)					{						for(vx=0;vx < =p[x][y][2];vx++)						{							for(vy=0;vy < =p[x][y][3];vy++)							{								if(!pexists(x+vx,y+vy))								{									tmpvx=vx;									tmpvy=vy;								}							}						}					}					if(p[x][y][2] < 0 && p[x][y][3] > 0)					{						for(vx=0;vx < -p[x][y][2];vx++)						{							for(vy=0;vy < p[x][y][3];vy++)							{								if(!pexists(x+vx,y+vy))								{									tmpvx = vx;									tmpvy = vy;								}							}						}					}					if(p[x][y][2] > 0 && p[x][y][3] < 0)					{						for(vx=0;vx < p[x][y][2];vx++)						{							for(vy=0;vy < -p[x][y][2];vy++)							{								if(!pexists(x+vx,y+vy))								{									tmpvx = vx;									tmpvy = -vy;								}							}						}					}					if(p[x][y][2] < 0 && p[x][y][3] < 0)					{						for(vx=0;vx < -p[x][y][2];vx++)						{							for(vy=0;vy < -p[x][y][3];vy++)							{								if(!pexists(x+vx,y+vy))								{									tmpvx = -vx;									tmpvy = -vy;								}							}						}					}
Nobody knows? I've been trying to fix various versions of this since January :(
instead of just posting the code here and hoping someone else will solve it for you, you might wanna try sitting down and design with pseudo on paper what exactly you want to happen.
Hint: you need to check if the particles are colliding with each other and not just the walls. Your code is pretty messy and lacks comments, so I can't see exactly where this is supposed to happen.
For starters you should really post the complete section of code you're having trouble with. We can only take guesses at what kind of data "p" holds, which makes finding your problem very difficult at best.

Also, I notice that you've essentially copied and pasted the same code four times, then made minor changes to it. If you ever find yourself in this situation, you should probably reconsider the design of the code as that kind of structure is almost never necessary, difficult to comprehend, and tends to lead to the kind of strange errors you're experiencing.

For example, in your code you have this:

if(p[x][y][2] > 0 && p[x][y][3] > 0){	for(vx=0;vx < =p[x][y][2];vx++)	{		for(vy=0;vy < =p[x][y][3];vy++)		{			if(!pexists(x+vx,y+vy))			{				tmpvx=vx;				tmpvy=vy;			}		}	}}if(p[x][y][2] < 0 && p[x][y][3] > 0){	for(vx=0;vx < -p[x][y][2];vx++)	{		for(vy=0;vy < p[x][y][3];vy++)		{			if(!pexists(x+vx,y+vy))			{				tmpvx = vx;				tmpvy = vy;			}		}	}}


In the first instance you have the code "for(vx=0;vx < =p[x][y][2];vx++)" which is interpreted as "less than or equal to" where in the second case you have "for(vx=0;vx < -p[x][y][2];vx++)" which is simply "less than."

Try to modify the code so that the same code doesn't appear more than once and in all likelihood your problem will simply disappear. [smile]

This topic is closed to new replies.

Advertisement