Forum » Programiranje » Collision detection
Collision detection
Phil ::
A kdo ve za kakšno stran kjer bi bil razložen collision detection v openGL na kakšnem enostavnem primeru. (pa ne na http://nehe.gamedev.ne ker je prezapleten zame )
LP
LP
DarkWIng ::
eden boljsih je res na gamedev al pa flipcode ... amapk tale je pa res zakon! .. drugače maš pa na NeHe-jevi strani en tutorial o tem... samo bl tko.. (ni dost splošn, ma ps source code)
You're a wonderful friend and a raving psycho.
Phil ::
Senitel thx za link je ful dober. Collision detection sem zdej ze usposobu. Btw a veš kak dober link za nalaganje modelov.
Senitel ::
Eko sem spisal zlo na hiterco en 3DS loader:
struct Vector{
float x,y,z;
};
struct Vertex{
Vector Pos; // Pozicija
Vector Normal; // Normala
float U,V; // Koordinate tekstur
};
struct FileChunk{
WORD Head;
DWORD Length;
};
Vertex* VertexBuffer;
WORD* IndexBuffer;
Vector CrossProduct(Vector a,Vector b)
{
Vector ret;
ret.x=a.y*b.z-a.z*b.y;
ret.y=a.z*b.x-a.x*b.z;
ret.z=a.x*b.y-a.y*b.x;
return ret;
}
Vector Normalize(Vector a)
{
float Max=a.x;
if (Max < a.y) Max=a.y;
if (Max < a.z) Max=a.z;
a.x /= Max; a.y /= Max; a.z /= Max;
return a;
}
int ReadChunk(FileChunk* Data,FILE** File)
{
fread(&Data->Head,sizeof(WORD),1,*File);
fread(&Data->Length,sizeof(DWORD),1,*File);
return 0;
}
BOOL FindChunk(DWORD ChunkID,FILE** File)
{
FileChunk chunk;
ReadChunk(&chunk,File);
while (chunk.Head!=ChunkID || !feof(*File))
{
fseek(*File,SEEK_CUR,chunk.Length); // Preskočimo trenutni kos in preberemo naslednjega
ReadChunk(&chunk,File);
}
if (chunk.Head!=ChunkID) return FALSE;
return TRUE;
}
int LoadModel(char* FileName)
{
FileChunk chunk;
FILE* load;
if (VertexBuffer) delete [] VertexBuffer;
if (IndexBuffer) delete [] IndexBuffer;
load=fopen(FileName,"rb");
// Preberemo osnovno glavo
ReadChunk(&chunk,&load);
if (chunk.Head!=0x4D4D) return -1; // Datoteka ni 3DS file
// Poiščemo glavo editorja
if (!FindChunk(0x3D3D,&load)) return -2; // File je neki čuden?
// Poiščemo kos z objekti
if (!FindChunk(0x4000,&load)) return -3; // Ni objekta?
// Poiščemo mesh -> Lahko jih je več!!
FindChunk(0x4100,&load);
// Poiščemo tabelo vertexov
FindChunk(0x4110,&load);
WORD NumVertices;
fread(&NumVertices,sizeof(WORD),1,load);
VertexBuffer=new Vertex[NumVertices];
ZeroMemory(VertexBuffer,sizeof(Vertex)*NumVertices);
for (DWORD i=0; i < NumVertices; i++)
{
fread(&VertexBuffer[i].Pos.x,sizeof(float),1,load);
fread(&VertexBuffer[i].Pos.z,sizeof(float),1,load);
fread(&VertexBuffer[i].Pos.y,sizeof(float),1,load);
}
// Poiščemo tabelo indexov
FindChunk(0x4120,&load);
WORD NumIndices;
DWORD ptr=0;
fread(&NumIndices,sizeof(WORD),1,load);
IndexBuffer=new WORD[NumIndices*3]; // Shranjeno je število trikotnikov
for (i=0; i < NumIndices; i++)
{
fread(&IndexBuffer[ptr],sizeof(WORD),1,load);
fread(&IndexBuffer[ptr+1],sizeof(WORD),1,load);
fread(&IndexBuffer[ptr+2],sizeof(WORD),1,load);
ptr+=3;
fseek(load,SEEK_CUR,sizeof(WORD)); // Preskočimo en word, ki za nas ni pomemben
}
// 3DS ne shranjuje normal, zato jih moramo izračunati sami
ptr=0;
for (i=0; i < NumIndices; i++)
{
Vector a,b,c;
a.x=VertexBuffer[IndexBuffer[ptr]].Pos.x-VertexBuffer[IndexBuffer[ptr+1]].Pos.x;
a.y=VertexBuffer[IndexBuffer[ptr]].Pos.y-VertexBuffer[IndexBuffer[ptr+1]].Pos.y;
a.z=VertexBuffer[IndexBuffer[ptr]].Pos.z-VertexBuffer[IndexBuffer[ptr+1]].Pos.z;
b.x=VertexBuffer[IndexBuffer[ptr]].Pos.x-VertexBuffer[IndexBuffer[ptr+2]].Pos.x;
b.y=VertexBuffer[IndexBuffer[ptr]].Pos.y-VertexBuffer[IndexBuffer[ptr+2]].Pos.y;
b.z=VertexBuffer[IndexBuffer[ptr]].Pos.z-VertexBuffer[IndexBuffer[ptr+2]].Pos.z;
c=CrossProduct(a,b);
VertexBuffer[IndexBuffer[ptr]].Normal.x+=c.x;
VertexBuffer[IndexBuffer[ptr]].Normal.y+=c.y;
VertexBuffer[IndexBuffer[ptr]].Normal.z+=c.z;
ptr+=3;
}
// Normaliziramo vse normale
for (i=0; i < NumVertices; i++) VertexBuffer[i].Normal=Normalize(VertexBuffer[i].Normal);
// Poiščemo kos, ki vsebuje koordinate za teksture
FindChunk(0x4140,&load);
fread(&NumVertices,sizeof(WORD),1,load);
for (i=0; i < NumVertices; i++)
{
fread(&VertexBuffer[i].U,sizeof(float),1,load);
fread(&VertexBuffer[j].V,sizeof(float),1,load);
}
fclose(load);
return 0;
}
struct Vector{
float x,y,z;
};
struct Vertex{
Vector Pos; // Pozicija
Vector Normal; // Normala
float U,V; // Koordinate tekstur
};
struct FileChunk{
WORD Head;
DWORD Length;
};
Vertex* VertexBuffer;
WORD* IndexBuffer;
Vector CrossProduct(Vector a,Vector b)
{
Vector ret;
ret.x=a.y*b.z-a.z*b.y;
ret.y=a.z*b.x-a.x*b.z;
ret.z=a.x*b.y-a.y*b.x;
return ret;
}
Vector Normalize(Vector a)
{
float Max=a.x;
if (Max < a.y) Max=a.y;
if (Max < a.z) Max=a.z;
a.x /= Max; a.y /= Max; a.z /= Max;
return a;
}
int ReadChunk(FileChunk* Data,FILE** File)
{
fread(&Data->Head,sizeof(WORD),1,*File);
fread(&Data->Length,sizeof(DWORD),1,*File);
return 0;
}
BOOL FindChunk(DWORD ChunkID,FILE** File)
{
FileChunk chunk;
ReadChunk(&chunk,File);
while (chunk.Head!=ChunkID || !feof(*File))
{
fseek(*File,SEEK_CUR,chunk.Length); // Preskočimo trenutni kos in preberemo naslednjega
ReadChunk(&chunk,File);
}
if (chunk.Head!=ChunkID) return FALSE;
return TRUE;
}
int LoadModel(char* FileName)
{
FileChunk chunk;
FILE* load;
if (VertexBuffer) delete [] VertexBuffer;
if (IndexBuffer) delete [] IndexBuffer;
load=fopen(FileName,"rb");
// Preberemo osnovno glavo
ReadChunk(&chunk,&load);
if (chunk.Head!=0x4D4D) return -1; // Datoteka ni 3DS file
// Poiščemo glavo editorja
if (!FindChunk(0x3D3D,&load)) return -2; // File je neki čuden?
// Poiščemo kos z objekti
if (!FindChunk(0x4000,&load)) return -3; // Ni objekta?
// Poiščemo mesh -> Lahko jih je več!!
FindChunk(0x4100,&load);
// Poiščemo tabelo vertexov
FindChunk(0x4110,&load);
WORD NumVertices;
fread(&NumVertices,sizeof(WORD),1,load);
VertexBuffer=new Vertex[NumVertices];
ZeroMemory(VertexBuffer,sizeof(Vertex)*NumVertices);
for (DWORD i=0; i < NumVertices; i++)
{
fread(&VertexBuffer[i].Pos.x,sizeof(float),1,load);
fread(&VertexBuffer[i].Pos.z,sizeof(float),1,load);
fread(&VertexBuffer[i].Pos.y,sizeof(float),1,load);
}
// Poiščemo tabelo indexov
FindChunk(0x4120,&load);
WORD NumIndices;
DWORD ptr=0;
fread(&NumIndices,sizeof(WORD),1,load);
IndexBuffer=new WORD[NumIndices*3]; // Shranjeno je število trikotnikov
for (i=0; i < NumIndices; i++)
{
fread(&IndexBuffer[ptr],sizeof(WORD),1,load);
fread(&IndexBuffer[ptr+1],sizeof(WORD),1,load);
fread(&IndexBuffer[ptr+2],sizeof(WORD),1,load);
ptr+=3;
fseek(load,SEEK_CUR,sizeof(WORD)); // Preskočimo en word, ki za nas ni pomemben
}
// 3DS ne shranjuje normal, zato jih moramo izračunati sami
ptr=0;
for (i=0; i < NumIndices; i++)
{
Vector a,b,c;
a.x=VertexBuffer[IndexBuffer[ptr]].Pos.x-VertexBuffer[IndexBuffer[ptr+1]].Pos.x;
a.y=VertexBuffer[IndexBuffer[ptr]].Pos.y-VertexBuffer[IndexBuffer[ptr+1]].Pos.y;
a.z=VertexBuffer[IndexBuffer[ptr]].Pos.z-VertexBuffer[IndexBuffer[ptr+1]].Pos.z;
b.x=VertexBuffer[IndexBuffer[ptr]].Pos.x-VertexBuffer[IndexBuffer[ptr+2]].Pos.x;
b.y=VertexBuffer[IndexBuffer[ptr]].Pos.y-VertexBuffer[IndexBuffer[ptr+2]].Pos.y;
b.z=VertexBuffer[IndexBuffer[ptr]].Pos.z-VertexBuffer[IndexBuffer[ptr+2]].Pos.z;
c=CrossProduct(a,b);
VertexBuffer[IndexBuffer[ptr]].Normal.x+=c.x;
VertexBuffer[IndexBuffer[ptr]].Normal.y+=c.y;
VertexBuffer[IndexBuffer[ptr]].Normal.z+=c.z;
ptr+=3;
}
// Normaliziramo vse normale
for (i=0; i < NumVertices; i++) VertexBuffer[i].Normal=Normalize(VertexBuffer[i].Normal);
// Poiščemo kos, ki vsebuje koordinate za teksture
FindChunk(0x4140,&load);
fread(&NumVertices,sizeof(WORD),1,load);
for (i=0; i < NumVertices; i++)
{
fread(&VertexBuffer[i].U,sizeof(float),1,load);
fread(&VertexBuffer[j].V,sizeof(float),1,load);
}
fclose(load);
return 0;
}
Vredno ogleda ...
Tema | Ogledi | Zadnje sporočilo | |
---|---|---|---|
Tema | Ogledi | Zadnje sporočilo | |
» | [Java] Zasnova shoot em up igreOddelek: Programiranje | 1176 (855) | PecenkA |
» | DirectX - težavicaOddelek: Programiranje | 1245 (978) | Senitel |
» | C++ programirane v Open GL oz. Direct xOddelek: Programiranje | 2952 (2223) | Vesoljc |
» | OpenGL problemOddelek: Programiranje | 2456 (2022) | Gundolf |