Wierd crashes

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Recipio
Posts: 11
Joined: Sun Aug 14, 2005 11:46 pm
Location: Sweden

Wierd crashes

Post by Recipio »

I have a bug in my game causing my psp to crash everytime I run into some polygons (always the same polygons, all others work). I've been looking into the problem for more then 4 hours now and it seems like a wierder problem then I thought. I've searched the forums and have found similar problems but nothing quite the same and none of the solutions to those have worked on mine.

The collision code is a direct port of the one I use for the pc and is made so that I can use the same models for collisions and draw arrays. I tried the same models on my pc and everything worked without any problems.

I use sphere to polygon collisions. I pass a pointer to the model structure and a pointer to my car structure to the function. These have before been malloced and memset to 0 before I've given them the values they should have. The first thing I do is to check AABB collisions, if the car is inside of the bounding box I load the rest of the values I need for the polygon. I check the distance and look if the closest point on the polygon to the cars position is really on the polygon. Then I want to change the cars position but this is where it crashes.
Even if I just do something like cr->pos.y = 0; it crashes if I go to the polygons which make it crash. On all other polygons it works just like it should. I never change the value of cr (the car pointer) but yet it seems to try to change the values inside of it is what's causing the crash. I can still read the values without it crashing and I've even tried moving the values I need in the code to normal variables and moving them back again at the end of the function but it still crashes when trying to move it back, but only on these specific polygons that always seem to cause the crash.

Here is the code:

Code: Select all

void collisionCheck(pspObject *model, car *cr) {

	unsigned int i, j, l;
	float mx, nx, p1x, p1y, p1z, my, ny, p2x, p2y, p2z, mz, nz, p3x, p3y, p3z, d;
	float bbplx, bbphx, bbply, bbphy, bbplz, bbphz;

	// player bounding box
	float bbplrlx = cr->pos.x - 8.0f;
	float bbplrhx = cr->pos.x + 8.0f;
	float bbplrly = cr->pos.y - 8.0f;
	float bbplrhy = cr->pos.y + 8.0f;
	float bbplrlz = cr->pos.z - 8.0f;
	float bbplrhz = cr->pos.z + 8.0f;
	cr->onground = 0;

	j = 0; l = 0;
	for&#40;i = 0; i < model->polygonAmount; i++&#41; &#123;

		// AABB check
		bbplx = model->aabb&#91;l++&#93;;
		bbphx = model->aabb&#91;l++&#93;;
		bbply = model->aabb&#91;l++&#93;;
		bbphy = model->aabb&#91;l++&#93;;
		bbplz = model->aabb&#91;l++&#93;;
		bbphz = model->aabb&#91;l++&#93;;
		if&#40;&#40;bbplrhx <= bbplx || bbplrlx >= bbphx ||
		    bbplrhy <= bbply || bbplrly >= bbphy ||
		    bbplrhz <= bbplz || bbplrlz >= bbphz&#41;&#41; &#123;
			j += 3;
		&#125; else &#123;

			mx = model->middlePoint&#91;j&#93;;
			nx = model->polygonNormal&#91;j&#93;;
			p1x = model->object&#91;j&#93;.x;
			p1y = model->object&#91;j&#93;.y;
			p1z = model->object&#91;j++&#93;.z;
			my = model->middlePoint&#91;j&#93;;
			ny = model->polygonNormal&#91;j&#93;;
			p2x = model->object&#91;j&#93;.x;
			p2y = model->object&#91;j&#93;.y;
			p2z = model->object&#91;j++&#93;.z;
			mz = model->middlePoint&#91;j&#93;;
			nz = model->polygonNormal&#91;j&#93;;
			p3x = model->object&#91;j&#93;.x;
			p3y = model->object&#91;j&#93;.y;
			p3z = model->object&#91;j++&#93;.z;
			d = model->d&#91;i&#93;;

			float dist = &#40;nx*&#40;cr->pos.x&#41; + ny*&#40;cr->pos.y&#41; + nz*&#40;cr->pos.z&#41; - d&#41;;
			if&#40;dist >= -2 && dist <= 8&#41; &#123;
				float newx = cr->pos.x - nx*dist;
				float newy = cr->pos.y - ny*dist;
				float newz = cr->pos.z - nz*dist;

				int dominentAxis = 0;
				float dominentValue = &#40;float&#41;fabs&#40;nx&#41;;
				if&#40;fabs&#40;ny&#41; > dominentValue&#41; dominentAxis = 1;
				if&#40;fabs&#40;nz&#41; > dominentValue&#41; dominentAxis = 2;

				vector2f v1 = &#123;0,0&#125;, v2 = &#123;0,0&#125;, l1 = &#123;0,0&#125;, l2 = &#123;0,0&#125;, l3 = &#123;0,0&#125;;
				if&#40;dominentAxis == 0&#41; &#123;
					v1.x = newz, v1.y = newy;
					v2.x = mz, v2.y = my;
					l1.x = p1z, l1.y = p1y;
					l2.x = p2z, l2.y = p2y;
					l3.x = p3z, l3.y = p3y;
				&#125; else if&#40;dominentAxis == 1&#41; &#123;
					v1.x = newx, v1.y = newz;
					v2.x = mx, v2.y = mz;
					l1.x = p1x, l1.y = p1z;
					l2.x = p2x, l2.y = p2z;
					l3.x = p3x, l3.y = p3z;
				&#125; else if&#40;dominentAxis == 2&#41; &#123;
					v1.x = newx, v1.y = newy;
					v2.x = mx, v2.y = my;
					l1.x = p1x, l1.y = p1y;
					l2.x = p2x, l2.y = p2y;
					l3.x = p3x, l3.y = p3y;
				&#125;

				if&#40;sameSideofLine&#40;v1, v2, l1, l2&#41; &&
				   sameSideofLine&#40;v1, v2, l2, l3&#41; &&
				   sameSideofLine&#40;v1, v2, l3, l1&#41;&#41; &#123;

					// move the car
					cr->pos.x -= nx*&#40;dist-8.0f&#41;;
					cr->pos.y -= ny*&#40;dist-8.0f&#41;;
					cr->pos.z -= nz*&#40;dist-8.0f&#41;;

					cr->onground = 1;

					if&#40;ny > 0.75&#41; &#123;
						if&#40;cr->speed.y < 0&#41; cr->speed.y = 0;
					&#125;
				&#125;
			&#125;
		&#125;
	&#125;

&#125;
Does anyone know what can be causing this bug?
User avatar
Raphael
Posts: 646
Joined: Tue Jan 17, 2006 4:54 pm
Location: Germany
Contact:

Post by Raphael »

There is nothing generally wrong with your code, only that you don't check if your object or car are null (which could be a good reason, why something like car->y = 0 crashes).
So unless a safety-check fixes that, it would be more likely that you somewhere else in your code cause a bug (buffer overflow), but which will only appear in this function.
Had something similar yesterday, where a subtile error in my code wouldn't cause it to crash at that part, but somewhere later in a elseway absolutely correct function would make the function result dependent on the presence of a printf - what is just impossible normally.
<Don't push the river, it flows.>
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki

Alexander Berl
Recipio
Posts: 11
Joined: Sun Aug 14, 2005 11:46 pm
Location: Sweden

Post by Recipio »

Thanks for your reply. Checking if cr is NULL didn't solve it either. Everything really seems to be the way it should, and it even is on most polygons. I have no idea what could be different about the ones causing it to crash. I tried checking my exception handlers output with psp-addr2line but it only said that it was in the collisionCheck function.
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

Even if I just do something like cr->pos.y = 0; it crashes if I go to the polygons which make it crash. On all other polygons it works just like it should. I never change the value of cr (the car pointer) but yet it seems to try to change the values inside of it is what's causing the crash. I can still read the values without it crashing and I've even tried moving the values I need in the code to normal variables and moving them back again at the end of the function but it still crashes when trying to move it back, but only on these specific polygons that always seem to cause the crash.
So random data is changing when you change something else? I bet you a buck (only an ozzie one) that one of your previous allocations isn't big enough and you twatted over the end of it into new data. Check your allocations of the car etc.

Jim
Recipio
Posts: 11
Joined: Sun Aug 14, 2005 11:46 pm
Location: Sweden

Post by Recipio »

Jim wrote:So random data is changing when you change something else? I bet you a buck (only an ozzie one) that one of your previous allocations isn't big enough and you twatted over the end of it into new data. Check your allocations of the car etc.

Jim
No, What I meant to say was that I never change the location of the pointer but anything I try to change inside of it makes it crash. Here is what I'm doing:

Code: Select all

typedef struct &#123;
...
&#125; car;
...
int carAmount = 1;
car *cars;

int main&#40;int argc, char* argv&#91;&#93;&#41; &#123;

...

	cars = &#40;car *&#41; malloc&#40;sizeof&#40;car&#41;*carAmount&#41;;
	memset&#40;cars, 0, sizeof&#40;car&#41;*carAmount&#41;;
	resetValues&#40;&#41;;
...
	while&#40;!done&#41; &#123;
...
		for&#40;i=0;i<carAmount;i++&#41; &#123;
			carPhysics&#40;&cars&#91;i&#93;&#41;;
		&#125;
	&#125;
...
	return 0;
&#125;

void carPhysics&#40;car *cr&#41; &#123;
...
	collisionCheck&#40;&level->objects&#91;0&#93;, cr&#41;;
...
&#125;
And I posted the collisionCheck function in my first post. So I don't think there should be anything wrong with my memory allocations. But I've been doing some further searching for the problem and I've tried to find the point in which the cr pointer stops working. If I comment all the code that moves the car to the right position and all that, it stops crashing. If I add the line cr->pos.y = 0; just bellow if(dist >= -2 && dist <= 8) { it crashes. If I add the same line just above the if case (bellow float dist) it doesn't crash. I really don't know how this in any logical way could happen. And as it only happens if I run in to some of the polygons on the level I guess it must have something to do with reading all the values I need to handle the collision but I don't know how that can effect the cr pointer.
User avatar
Raphael
Posts: 646
Joined: Tue Jan 17, 2006 4:54 pm
Location: Germany
Contact:

Post by Raphael »

I doesn't neccessarily have to be the allocation of the car array, it could be any other allocation being too small, then you writing some too big data into that memory, overrunning it and therefore corrupting other data or code. Then a crash can occur on pretty much random points (for a human).
Maybe try out to compile your program with a debug memory manager like the one from Paul Nettle (www.fluidstudios.org). This will help you track any buffer overflows/underruns, or other memory corruption.
<Don't push the river, it flows.>
http://wordpress.fx-world.org - my devblog
http://wiki.fx-world.org - VFPU documentation wiki

Alexander Berl
Recipio
Posts: 11
Joined: Sun Aug 14, 2005 11:46 pm
Location: Sweden

Post by Recipio »

I tried that memory manager but it really wasn't psp compitable.

I got it to work! :D I have no idea why but I tried outputing the polygon data numbers and everytime it crashed the normal was set to 0 on all coordinates so now I check each time if all the normal values are 0 before continuing. I don't know how this could have caused a crash in the first place but right now I'm just really happy.
Post Reply