forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Wierd crashes

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
Recipio



Joined: 14 Aug 2005
Posts: 11
Location: Sweden

PostPosted: Sun Sep 03, 2006 9:45 pm    Post subject: Wierd crashes Reply with quote

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:

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(i = 0; i < model->polygonAmount; i++) {

      // AABB check
      bbplx = model->aabb[l++];
      bbphx = model->aabb[l++];
      bbply = model->aabb[l++];
      bbphy = model->aabb[l++];
      bbplz = model->aabb[l++];
      bbphz = model->aabb[l++];
      if((bbplrhx <= bbplx || bbplrlx >= bbphx ||
          bbplrhy <= bbply || bbplrly >= bbphy ||
          bbplrhz <= bbplz || bbplrlz >= bbphz)) {
         j += 3;
      } else {

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

         float dist = (nx*(cr->pos.x) + ny*(cr->pos.y) + nz*(cr->pos.z) - d);
         if(dist >= -2 && dist <= 8) {
            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 = (float)fabs(nx);
            if(fabs(ny) > dominentValue) dominentAxis = 1;
            if(fabs(nz) > dominentValue) dominentAxis = 2;

            vector2f v1 = {0,0}, v2 = {0,0}, l1 = {0,0}, l2 = {0,0}, l3 = {0,0};
            if(dominentAxis == 0) {
               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;
            } else if(dominentAxis == 1) {
               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;
            } else if(dominentAxis == 2) {
               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;
            }

            if(sameSideofLine(v1, v2, l1, l2) &&
               sameSideofLine(v1, v2, l2, l3) &&
               sameSideofLine(v1, v2, l3, l1)) {

               // move the car
               cr->pos.x -= nx*(dist-8.0f);
               cr->pos.y -= ny*(dist-8.0f);
               cr->pos.z -= nz*(dist-8.0f);

               cr->onground = 1;

               if(ny > 0.75) {
                  if(cr->speed.y < 0) cr->speed.y = 0;
               }
            }
         }
      }
   }

}


Does anyone know what can be causing this bug?
Back to top
View user's profile Send private message MSN Messenger
Raphael



Joined: 17 Jan 2006
Posts: 646
Location: Germany

PostPosted: Sun Sep 03, 2006 11:05 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
Recipio



Joined: 14 Aug 2005
Posts: 11
Location: Sweden

PostPosted: Sun Sep 03, 2006 11:41 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message MSN Messenger
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Mon Sep 04, 2006 11:46 pm    Post subject: Reply with quote

Quote:
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
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
Recipio



Joined: 14 Aug 2005
Posts: 11
Location: Sweden

PostPosted: Tue Sep 05, 2006 4:21 am    Post subject: Reply with quote

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:

typedef struct {
...
} car;
...
int carAmount = 1;
car *cars;

int main(int argc, char* argv[]) {

...

   cars = (car *) malloc(sizeof(car)*carAmount);
   memset(cars, 0, sizeof(car)*carAmount);
   resetValues();
...
   while(!done) {
...
      for(i=0;i<carAmount;i++) {
         carPhysics(&cars[i]);
      }
   }
...
   return 0;
}

void carPhysics(car *cr) {
...
   collisionCheck(&level->objects[0], cr);
...
}


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.
Back to top
View user's profile Send private message MSN Messenger
Raphael



Joined: 17 Jan 2006
Posts: 646
Location: Germany

PostPosted: Tue Sep 05, 2006 4:35 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
Recipio



Joined: 14 Aug 2005
Posts: 11
Location: Sweden

PostPosted: Tue Sep 05, 2006 11:19 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group