PDA

View Full Version : bsp colision detection crash when i hit wall



enjoycrf
02-02-2011, 08:04 PM
i cant get rid of seg fault error
which is why trace is set to return on lol = 1
the first colision sets lol = 1 and it seg faults when i move around and hit that face 2
u can see that im doing a trace on just on face 2 for testing
what i am i missing here
i know something is working since it crashes when i hit that wall wtf :p
am i that big of a noob?
jus tell me



TNode node;

TPlane plane;

TLeaf leaf;

TBrush brush;

TBrushSide brushSide;





void CheckBrush(int brushIndex, int nodeIndex, tVector3 start, tVector3 end, tVector3 planeNormal) {





//printf("CheckBrush: %d\n", brushIndex);





float startFraction = -1.0f;

float endFraction = 1.0f;



bool startsOut = false;

bool endsOut = false;





bool outputAllSolid = false;

bool outputStartOut = true;







node = lMap.mNodes[nodeIndex];

plane = lMap.mPlanes[node.mPlane];

leaf = lMap.mLeaves[-(nodeIndex + 1)];

brush = lMap.mBrushes[lMap.mLeafBrushes[leaf.mLeafBrush + brushIndex].mBrushIndex];





int i;



for (i = 0; i < brush.mNbBrushSides; i++) {











brushSide = lMap.mBrushSides[brush.mBrushSide + i];

plane = lMap.mPlanes[brushSide.mPlaneIndex];





float startDistance = DotProduct(start, planeNormal) - plane.mDistance;

float endDistance = DotProduct(end, planeNormal) - plane.mDistance;





if (startDistance > 0) startsOut = true;

if (endDistance > 0) endsOut = true;







// make sure the trace isn't completely on one side of the brush

if (startDistance > 0 &amp;&amp; endDistance > 0)

{ // both are in front of the plane, its outside of this brush

return;

}

if (startDistance <= 0 &amp;&amp; endDistance <= 0)

{ // both are behind this plane, it will get clipped by another one

continue;

}



if (startDistance > endDistance)

{ // line is entering into the brush





float fraction = (startDistance - EPSILON) / (startDistance - endDistance);

if (fraction > startFraction) startFraction = fraction;





}

else

{ // line is leaving the brush





float fraction = (startDistance + EPSILON) / (startDistance - endDistance);

if (fraction < endFraction) endFraction = fraction;





}

}













if (startsOut == false) {



outputStartOut = false;

if (endsOut == false) outputAllSolid = true;

return;



}





if (startFraction < endFraction) {





if (startFraction > -1 &amp;&amp; startFraction < outputFraction) {



if (startFraction < 0) startFraction = 0;

outputFraction = startFraction;



}



}







}





















void CheckNode(int nodeIndex, float startFraction, float endFraction,

tVector3 start, tVector3 end) {





//printf("CheckNode: %d\n", nodeIndex);







node = lMap.mNodes[nodeIndex];

plane = lMap.mPlanes[node.mPlane];









tVector3 planeNormal;



planeNormal.x = plane.mNormal[0];

planeNormal.y = plane.mNormal[1];

planeNormal.z = plane.mNormal[2];







if (nodeIndex < 0) { // this is a leaf



leaf = lMap.mLeaves[-(nodeIndex + 1)];





int i;



for (i = 0; i < leaf.mNbLeafBrushes; i++) {



brush = lMap.mBrushes[lMap.mLeafBrushes[leaf.mLeafBrush + i].mBrushIndex];



//TBrush* brush2 = lMap.mBrushes[lMap.mLeafBrushes[leaf.mLeafBrush + i].mBrushIndex];





//&amp;&amp; (BSP.shaders[brush.mTextureIndex].contentFlags & 1)

if (brush.mNbBrushSides > 0)

CheckBrush(i, nodeIndex, start, end, planeNormal);



}



// don't have to do anything else for leaves

return;



}













float startDistance = DotProduct(start, planeNormal) - plane.mDistance;

float endDistance = DotProduct(end, planeNormal) - plane.mDistance;





//printf("mChildren: %d\n", node.mChildren[0]);

//return;



if (startDistance >= 0 &amp;&amp; endDistance >= 0) {

// both points are in front of the plane





if (node.mChildren[0] > 0) {



// so check the front child

CheckNode( node.mChildren[0], startFraction, endFraction, start, end );



}



} else if (startDistance < 0 &amp;&amp; endDistance < 0) {



// both points are behind the plane







if (node.mChildren[1] > 0) {



// so check the back child

CheckNode(node.mChildren[1], startFraction, endFraction, start, end);

}





} else {



//return;





int side;

float fraction1, fraction2, middleFraction, inverseDistance;







// STEP 1: split the segment into two

if (startDistance < endDistance) {



side = 1; // back

inverseDistance = 1.0f / (startDistance - endDistance);



fraction1 = (startDistance + EPSILON) * inverseDistance;

fraction2 = (startDistance + EPSILON) * inverseDistance;



} else if (endDistance < startDistance) {



side = 0; // front

inverseDistance = 1.0f / (startDistance - endDistance);



fraction1 = (startDistance + EPSILON) * inverseDistance;

fraction2 = (startDistance - EPSILON) * inverseDistance;



} else {



side = 0; // front

fraction1 = 1.0f;

fraction2 = 0.0f;



}









// STEP 2: make sure the numbers are valid

if (fraction1 < 0.0f) fraction1 = 0.0f;

else if (fraction1 > 1.0f) fraction1 = 1.0f;



if (fraction2 < 0.0f) fraction2 = 0.0f;

else if (fraction2 > 1.0f) fraction2 = 1.0f;









// STEP 3: calculate the middle point for the first side

middleFraction = startFraction + (endFraction - startFraction) * fraction1;







tVector3 middle;

//middle = tVector3(x, y, z);





middle.x = start.x + fraction1 * (end.x - start.x);

middle.y = start.y + fraction1 * (end.y - start.y);

middle.z = start.z + fraction1 * (end.z - start.z);







// STEP 4: check the first side

CheckNode(node.mChildren[0], startFraction, middleFraction, start, middle);







// STEP 5: calculate the middle point for the second side

middleFraction = startFraction + (endFraction - startFraction) * fraction2;







middle.x = start.x + fraction2 * (end.x - start.x);

middle.y = start.y + fraction2 * (end.y - start.y);

middle.z = start.z + fraction2 * (end.z - start.z);





// STEP 6: check the second side

CheckNode(node.mChildren[1], middleFraction, endFraction, middle, end);









}



}









void Trace(tVector3 inputStart, int ummnode) {







if (lol == 1) return;



//outputFraction = 0.0f;





float startFraction = 0.0f;

float endFraction = 1.0f;









tVector3 wallNormal;



// Calculate the cross product with the non communitive equation

wallNormal.x = lMap.mFaces[2].mNormal[0];

wallNormal.y = lMap.mFaces[2].mNormal[1];

wallNormal.z = lMap.mFaces[2].mNormal[2];







tVector3 inputEnd;



// walk through the BSP tree

CheckNode(ummnode, startFraction, endFraction, inputStart, inputEnd);









tVector3 outputEnd;



if (outputFraction == 1.0f) { // nothing blocked the trace



outputEnd = inputEnd;



lol = 0;





} else { // collided with something









//outputFraction == 1.0f;

lol = 1;









//printf("colided\n");





//mPos.z = -mPos.z;

//mPos.x = -mPos.x;

//mPos.z = z_m_1 + 1;









//outputEnd[i] = inputStart[i] + outputFraction * (inputEnd[i] - inputStart[i]);



outputEnd.x = inputStart.x + outputFraction * (inputEnd.x - inputStart.x);

outputEnd.y = inputStart.y + outputFraction * (inputEnd.y - inputStart.y);

outputEnd.z = inputStart.z + outputFraction * (inputEnd.z - inputStart.z);











}



}

enjoycrf
02-02-2011, 09:57 PM
ok got rid of that error

i put an if not to check nagative children why are they negative?????????????????


why some children index in bsp node negative?

mobeen
02-02-2011, 10:03 PM
Hi,
Perhaps this might be the culprit


leaf = lMap.mLeaves[-(nodeIndex + 1)];

you are passing a -ve index

enjoycrf
02-02-2011, 10:13 PM
what is -ve ?
and why is that set to nodeindex + 1 and the sign flipped?

enjoycrf
02-02-2011, 10:31 PM
if (nodeIndex < 0) { // this is a leaf


so all the negative children are leaves?
but the sign is flipped because its the same leaf as the positive?

why is the leaf of nodeindex+1 then

mobeen
02-02-2011, 10:31 PM
I pointed out the line which seemed suspicious to me in your code.

-ve means negative

rather than asking me


and why is that set to nodeindex + 1 and the sign flipped?

ask your self why u put a negative index there?

mobeen
02-02-2011, 10:35 PM
You should first check whether the bsp tree is created correctly.
Once this is certain u may then proceed with this code.

enjoycrf
02-02-2011, 10:43 PM
its a sample level that came with tutorial
lol?

mbentrup
02-03-2011, 01:41 AM
A negative number indicates a leaf node. The leaf nodes are numbered from 0 to some n, but -0 is not negative, so leaf number 0 is stored as index -1, leaf number 1 is stored as index -2 etc.

So the -(index + 1) formula just computes the leaf number.

enjoycrf
02-03-2011, 02:30 AM
ya i figured it out already
but ur awesome for knowing it thanx

can u plz help me tho cause i think i got my tracing strat and end vectors wrong

wallNormal.x = lMap.mFaces[f].mNormal[0];

wallNormal.y = lMap.mFaces[f].mNormal[1];

wallNormal.z = lMap.mFaces[f].mNormal[2];





lol = wallNormal.x;





Trace(mPos, wallNormal, n);

see how mpos is the camera pos vector and its teh startvector
face normal is my end vector and n is node
but i dont think the end vector is the normal of a face of the bsp now cause its not coliding

ugluk
02-03-2011, 05:05 AM
Can you provide a link to the bsp tutorial?

enjoycrf
02-03-2011, 05:15 AM
http://www.flipcode.com/archives/Simple_Quake3_BSP_Loader.shtml