home *** CD-ROM | disk | FTP | other *** search
- #include "collision.h"
- #include "intersection.h"
- //#include "camera.h"
- #include "log.h"
- //#include "quadtree.h"
- #include "Game.h"
- #include "Renderer.h"
- //#include "mech.h"
- #include "random.h"
- #include "Network.h"
-
- #include "LightEffectsParticleClusters.h"
-
- //trace_t lastTrace={0};
-
- bool rayIntersectsFace(vec3_t pos, vec3_t dir, Face* face, float* distance, vec3_t hitpoint){
- vec4_t plane;
- vec3_t p1p2, p1p3, p2p3;
- vec3_t p1point, p2point;
- vec3_t c1,c2;
-
- float* p1=&face->vertices[0];
-
- float* p2=&face->vertices[3];
- float* p3=&face->vertices[6];
-
- vectorSub3d(p2, p1, p1p2);
- vectorSub3d(p3, p1, p1p3);
- vectorSub3d(p3, p2, p2p3);
-
- vectorCopy3d(face->normal, plane);
- //vectorNormalize3d(plane, plane);
- plane[3]=vectorDotP3d(p1, plane);
-
- double xd=vectorDotP3d(plane, dir);
-
- if(xd==0.0)
- return false;
-
- *distance=(float)( (plane[3]-vectorDotP3d(plane, pos))/xd );
- if(*distance<0.0)
- return false;
-
- vectorMA3d(pos, *distance, dir, hitpoint);
-
- // check if hitpoint is in triangle
- vectorSub3d(hitpoint, p1, p1point);
- vectorCrossP3d(p1p2, p1p3, c1);
- vectorCrossP3d(p1p2, p1point, c2);
- if(vectorDotP3d(c1,c2) < 0.0)
- return false;
-
- // vectorSub3d(point, p1, p1point);
- vectorCrossP3d(p1p3, p1p2, c1);
- vectorCrossP3d(p1p3, p1point, c2);
- if(vectorDotP3d(c1,c2) < 0.0)
- return false;
-
- vectorScale3d(-1.0f, p1p2, p1p2);
- vectorSub3d(hitpoint, p2, p2point);
- vectorCrossP3d(p2p3, p1p2, c1);
- vectorCrossP3d(p2p3, p2point, c2);
- if(vectorDotP3d(c1,c2) < 0.0)
- return false;
-
- return true;
-
- }
-
- bool linesegmentIntersectsFace(vec3_t pos1, vec3_t pos2, Face* face, float* distance, vec3_t hitpoint){
- vec4_t plane;
- vec3_t p1p2, p1p3, p2p3;
- vec3_t dir;
- vec3_t p1point, p2point;
- vec3_t c1,c2;
-
- vectorSub3d(pos2, pos1, dir);
-
- float* p1=&face->vertices[0];
- float* p2=&face->vertices[3];
- float* p3=&face->vertices[6];
-
- vectorSub3d(p2, p1, p1p2);
- vectorSub3d(p3, p1, p1p3);
- vectorSub3d(p3, p2, p2p3);
-
- vectorCopy3d(face->normal, plane);
- //vectorNormalize3d(plane, plane);
- plane[3]=vectorDotP3d(p1, plane);
-
- double xd=vectorDotP3d(plane, dir);
-
- if(xd==0.0)
- return false;
-
- *distance=(float)( (plane[3]-vectorDotP3d(plane, pos1))/xd );
- if(*distance<0.0 || *distance>1.0)
- return false;
-
- vectorMA3d(pos1, *distance, dir, hitpoint);
-
- // check if hitpoint is in triangle
- vectorSub3d(hitpoint, p1, p1point);
- vectorCrossP3d(p1p2, p1p3, c1);
- vectorCrossP3d(p1p2, p1point, c2);
- if(vectorDotP3d(c1,c2) < 0.0)
- return false;
-
- // vectorSub3d(point, p1, p1point);
- vectorCrossP3d(p1p3, p1p2, c1);
- vectorCrossP3d(p1p3, p1point, c2);
- if(vectorDotP3d(c1,c2) < 0.0)
- return false;
-
- vectorScale3d(-1.0f, p1p2, p1p2);
- vectorSub3d(hitpoint, p2, p2point);
- vectorCrossP3d(p2p3, p1p2, c1);
- vectorCrossP3d(p2p3, p2point, c2);
- if(vectorDotP3d(c1,c2) < 0.0)
- return false;
-
- return true;
-
- }
-
-
-
-
-
- void traceRay(vec3_t startPos, vec3_t dir, trace_t* trace){
- vectorCopy3d(startPos, trace->startPos);
- vectorCopy3d(dir, trace->dir);
- trace->traceType=TRACE_TYPE_RAY;
-
- trace->hits.clear();
-
- if( !(trace->ignoreFlags & COLLISION_FLAG_ARENA) ){
- Game::arena->sptree->traceRay(trace);
- }
-
- if( !(trace->ignoreFlags & COLLISION_FLAG_VEHICLES) ){
-
- for(int i=0;i<GAME_MAX_VEHICLES;i++){
- if( Game::vehicles[i] == NULL )
- continue;
-
- Vehicle* v = Game::vehicles[i];
-
- vec3_t absMin, absMax;
- vectorAdd3d(v->pos, v->hitAABB.min, absMin);
- vectorAdd3d(v->pos, v->hitAABB.max, absMax);
-
- float dist = 0.0f;
- vec3_t hp;
- if( rayIntersectsAABB( startPos, dir, absMin, absMax, &dist, hp ) ){
- vector<hit_t>::iterator hits_iter;
- for (hits_iter = trace->hits.begin(); hits_iter != trace->hits.end(); hits_iter++){
- if( dist < hits_iter->distance ){
- break;
- }
-
- }
-
- hit_t hit;
-
- hit.distance = dist;
- hit.face = NULL;
- hit.node = NULL;
- hit.vehicle = v;
- vectorCopy3d(hp, hit.pos);
-
- // trace->hits.push_back(hit);
- hits_iter = trace->hits.insert(hits_iter, hit);
- }
- }
- }
- }
- void traceLinesegment(vec3_t pos1, vec3_t pos2, trace_t* trace){
- vectorCopy3d(pos1, trace->startPos);
- vectorCopy3d(pos2, trace->endPos);
- vectorSub3d(pos2, pos1, trace->dir);
- trace->traceType=TRACE_TYPE_LINE_SEGMENT;
-
- trace->hits.clear();
-
- if( !(trace->ignoreFlags & COLLISION_FLAG_ARENA) ){
- Game::arena->sptree->traceLinesegment(trace);
- }
-
- if( !(trace->ignoreFlags & COLLISION_FLAG_VEHICLES) ){
-
- for(int i=0;i<GAME_MAX_VEHICLES;i++){
- if( Game::vehicles[i] == NULL )
- continue;
-
- Vehicle* v = Game::vehicles[i];
-
- vec3_t absMin, absMax;
- vectorAdd3d(v->pos, v->hitAABB.min, absMin);
- vectorAdd3d(v->pos, v->hitAABB.max, absMax);
-
- float dist = 0.0f;
- vec3_t hp;
- if( linesegmentIntersectsAABB( pos1, pos2, absMin, absMax, &dist, hp ) ){
- vector<hit_t>::iterator hits_iter;
- for (hits_iter = trace->hits.begin(); hits_iter != trace->hits.end(); hits_iter++){
- if( dist < hits_iter->distance ){
- break;
- }
-
- }
-
- hit_t hit;
-
- hit.distance = dist;
- hit.face = NULL;
- hit.node = NULL;
- hit.vehicle = v;
- vectorCopy3d(hp, hit.pos);
-
- // trace->hits.push_back(hit);
- hits_iter = trace->hits.insert(hits_iter, hit);
- }
- }
- }
- }
-
- void traceAABB(vec3_t pos1, vec3_t pos2, AABB_t aabb, trace_t* trace){
- vectorCopy3d(pos1, trace->startPos);
- vectorCopy3d(pos2, trace->endPos);
- vectorSub3d(pos2, pos1, trace->dir);
- vectorCopy3d(aabb.min, trace->min);
- vectorCopy3d(aabb.max, trace->max);
- trace->traceType = TRACE_TYPE_AABB;
-
- trace->hits.clear();
-
- if( !(trace->ignoreFlags & COLLISION_FLAG_ARENA) ){
- Game::arena->sptree->traceAABB(trace );
- }
-
- if( !(trace->ignoreFlags & COLLISION_FLAG_VEHICLES) ){
-
- vec3_t trace_absMin, trace_absMax;
- vectorAdd3d(trace->endPos, aabb.min, trace_absMin);
- vectorAdd3d(trace->endPos, aabb.max, trace_absMax);
-
- for(int i=0;i<GAME_MAX_VEHICLES;i++){
- if( Game::vehicles[i] == NULL )
- continue;
-
- Vehicle* v = Game::vehicles[i];
-
- vec3_t absMin, absMax;
- vectorAdd3d(v->pos, v->moveAABB.min, absMin);
- vectorAdd3d(v->pos, v->moveAABB.max, absMax);
-
- if( AABBIntersectsAABB(trace_absMin, trace_absMax, absMin, absMax ) ){
- hit_t hit;
-
- hit.distance = vectorLength3d(trace->dir);
- hit.face = NULL;
- hit.node = NULL;
- hit.vehicle = v;
- vectorCopy3d(trace->endPos, hit.pos);
-
- trace->hits.push_back(hit);
- }
- }
-
- }
- }
-
-
-
-
-
-
-
-
- /*
- static SpacePartitioningTreeNode* debug_node = NULL;
- static Face* debug_face = NULL;
-
- void castRay(){
- trace_t trace;
- trace.ignoreFlags = 0 | COLLISION_FLAG_BACKFACES | COLLISION_FLAG_SHOOT_THROUGH;
-
- debug_node = NULL;
- debug_face = NULL;
-
- //Game::arena->sptree->traceRay(Game::cam.pos, Game::cam.dir, &trace);
- traceRay(Game::cam.pos, Game::cam.dir, &trace);
- if( trace.hits.empty() ){
- log("castRay(): hit nothing\n");
- }else{
- int vehicleHits = 0;
- int faceHits = 0;
- for( unsigned int i=0; i<trace.hits.size(); i++){
- if( trace.hits[i].face != NULL ){
- faceHits++;
- }
- if( trace.hits[i].vehicle != NULL ){
- vehicleHits++;
- }
- }
-
- log("castRay(): hit %i faces and %i vehicles\n", faceHits, vehicleHits);
-
- vec3_t col, pos;
- //vectorInit3d(0.1f, 0.6f, 0.2f, col);
- vectorInit3d(frand(1.0f), frand(1.0f), frand(1.0f), col);
- vectorCopy3d(trace.hits[0].pos, pos);
- Renderer::particleSystem.linkParticleCluster(new DynamicLightParticleCluster(pos, col, 5.0f, 10000));
-
- // debug_node = trace.hits[0].node;
- // debug_face = trace.hits[0].face;
- }
- }
-
- void renderDebugStuff(){
- if( debug_node != NULL )
- debug_node->drawBorders();
-
- if( debug_face != NULL ){
- Renderer::disableLighting();
- glColor3f(0.0f, 1.0f, 0.0f);
- Renderer::debug_renderFace(debug_face);
- Renderer::enableLighting();
- }
- }
- */
- /*
- bool castLinesegmentAgainstArena(vec3_t pos1, vec3_t pos2, float* distance, vec3_t hitPoint){
- return false;
- }
- */
-
-
-
-
-
-
-