Hilfe bei q3bsp kollision



  • hallo liebe Leute ich schreib zurzeit gerade an einen kleinen Spiel (soll ein
    Adventure später mal werden) als Mapformat habe ich mich fürs Quake 3 bsp (also
    version 46) entschieden.Soweit klappt auch alles ganz gut und opengl zeichnet
    fröhlich und lustig vor sich hin.Nun wollte ich aber die Kollisionsabfrage mal
    endlich einbauen und habe mir dazu das Tutorial auf devmaster.net reingezogen.
    Nur leider gibt es ein großes Problem ... denn die Variable "outputFraction"
    welche benötigt wird um den Schnittpunkt zu errechnen ist stehts 0.0 oder 1.0
    (niemals dazwischen) ... zu gut deutsch ich weiß nur ob die Gerade geschnitten
    wird aber nicht wo.Hat jemand vieleicht eine Idee woran das liegen könnte?

    [edit] oh hat sich erledigt ... das hat man davon wenn man tagelang nen kleinen
    Fehler übersieht 🙂

    float outputFraction;
    bool outputStartsOut;
    VECTOR3D originputStart,originputEnd;
    bool outputAllSolid;
    void BSP::CheckBrush(VECTOR3D inputStart,VECTOR3D inputEnd, int brushnum,float fraca,float fracb )
    {
        float startFraction = -1.0;
        float endFraction = 1.0;
        bool startsOut = false;
        bool endsOut = false;
    
    	if(brushnum > numBrushes || brushnum < 0) return;
    
    	if(brushes[brushnum].numBrushSides <= 0) return;
    	//we gotta check for texturescripts at this point
    
    	for (int i = 0; i < brushes[brushnum].numBrushSides; i++)
    	{
    		int brushsidenum = brushes[brushnum].BrushSide + i;
    		if(brushsidenum >= numBrushsides || brushsidenum < 0) continue;
    		if(brushsides[brushsidenum].PlaneIndex < 0 || brushsides[brushsidenum].PlaneIndex >= numPlanes) continue;
    		float startDistance = planes[brushsides[brushsidenum].PlaneIndex].GetDistance(inputStart);
    		float endDistance   = planes[brushsides[brushsidenum].PlaneIndex].GetDistance(inputEnd);
    
    		if (startDistance > 0) startsOut = true;
    		if (endDistance > 0) endsOut = true;
    
    		if (startDistance > 0 && endDistance > 0) return;
    		if (startDistance <= 0 && endDistance <= 0) continue;
    
    		if (startDistance > endDistance)
    		{
    
    			float fraction = (startDistance - EPSILON) / (startDistance - endDistance);
    			if (fraction > startFraction) startFraction = fraction;
    
    		}
    
    		else
    		{
                float fraction = (startDistance + EPSILON) / (startDistance - endDistance);  // *
                if (fraction < endFraction) endFraction = fraction;
    
    		}
    
    	}
    
        if (startsOut == false)
        {
            outputStartsOut = false;
            if (endsOut == false)
                outputAllSolid = true;
            return;
        }
    
        if (startFraction < endFraction)
        {
    		sys.sys_client.con_printf("startFraction %4.2f endFraction  %4.2f  \n",startFraction,endFraction );
            if (startFraction > -1 && startFraction < outputFraction)
            {
    
                if (startFraction < 0)
                    startFraction = 0;
                outputFraction = startFraction;
            }
        }
    
    }
    void BSP::CheckNode( int nodeIndex, float startFraction, float endFraction, VECTOR3D start, VECTOR3D end)
    {
    
    	if (nodeIndex < 0)
    	{
    
    		for(int i= 0;i< leaves[-(nodeIndex + 1)].numBrushes;i++)
    		{
    
    			CheckBrush(originputStart,originputEnd, leafbrushes[leaves[-(nodeIndex + 1)].firstLeafBrush + i] ,startFraction,endFraction);
    
    		}
    		return;
    	}
    	float startDistance = planes[nodes[nodeIndex].planeIndex].GetDistance(start);
    	float endDistance = planes[nodes[nodeIndex].planeIndex].GetDistance(end);
    
    	if (startDistance >= 0 && endDistance >= 0)
    	{
    
    		CheckNode( nodes[nodeIndex].front, startFraction, endFraction, start, end );
    	}
    	else if (startDistance < 0 && endDistance < 0)
    	{
    
    		CheckNode( nodes[nodeIndex].back, startFraction, endFraction, start, end );
    	}
    	else
    	{
    		int side;
    		float fraction1, fraction2, middleFraction;
    		VECTOR3D middle;
    
    		if (startDistance < endDistance)
    		{
    			side = 1; // back
    			float inverseDistance = 1.0f / (startDistance - endDistance);
    			fraction1 = (startDistance + EPSILON) * inverseDistance;
    			fraction2 = (startDistance + EPSILON) * inverseDistance;
    		}
    		else if (endDistance < startDistance)
    		{
    			side = 0; // back
    			float inverseDistance = 1.0f / (startDistance - endDistance);
    			fraction1 = (startDistance + EPSILON) * inverseDistance;
    			fraction2 = (startDistance - EPSILON) * inverseDistance;
    		}
            else
            {
                side = 0; // front
                fraction1 = 1.0f;
                fraction2 = 0.0f;
            }
    		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;
    
            middleFraction = startFraction + (endFraction - startFraction) * fraction1;
    		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);
    
    		if(side == 0) CheckNode( nodes[nodeIndex].front, startFraction, middleFraction, start, middle );
    
    		else CheckNode( nodes[nodeIndex].back, startFraction, middleFraction, start, middle );
    
    		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);
    		if(side == 0) CheckNode( nodes[nodeIndex].back, middleFraction, endFraction, middle, end );
    
    		else CheckNode( nodes[nodeIndex].front, middleFraction, endFraction, middle, end );
    
    	}
    
    }
    bool BSP::Trace( VECTOR3D inputStart, VECTOR3D inputEnd , VECTOR3D *out)
    {
    	outputStartsOut = true;
    	outputAllSolid = false;
    	outputFraction = 1.0f;
    	VECTOR3D		temp;
    	originputStart = inputStart;
    	originputEnd = inputEnd;
    
    	CheckNode( 0, 0.0f, 1.0f, inputStart, inputEnd );
    
    	if (outputFraction == 1.0f)
    	{	
    		out->x = inputEnd.x;
    		out->y = inputEnd.y;
    		out->z = inputEnd.z;
    		return false;
    	}
    	else
    	{	
    		inputEnd.Subtract(inputStart,temp);
    		temp.Scale(outputFraction,temp);
    		temp.Add(inputStart,temp);
    		out->x = temp.x;
    		out->y = temp.y;
    		out->z = temp.z;
    
    		return true;
    	}
    
    }
    

Anmelden zum Antworten