Ejemplo n.º 1
0
bool
check_polyhedron_collision_with_dag(SceneNode *node,
	const pp::Matrix& modelMatrix, const pp::Matrix& invModelMatrix,
    const ppogl::Polyhedron& ph)
{
	PP_CHECK_POINTER( node );

    bool hit = false;
    pp::Matrix newModelMatrix=modelMatrix*node->trans;
    pp::Matrix newInvModelMatrix=node->invtrans*invModelMatrix;

    if(node->isSphere == true){
        ppogl::Polyhedron newph(ph);
		trans_polyhedron( newInvModelMatrix, newph );
		hit = intersect_polyhedron( newph );
	}

    if (hit == true) return hit;

	SceneNode* child = node->child;
    while (child != NULL) {

        hit = check_polyhedron_collision_with_dag( 
	    child, newModelMatrix, newInvModelMatrix, ph );

        if ( hit == true ) {
            return hit;
        }

        child = child->next;
    } 

    return false;
}
Ejemplo n.º 2
0
bool_t check_polyhedron_collision_with_dag( 
                                           scene_node_t *node, matrixgl_t modelMatrix, matrixgl_t invModelMatrix,
                                           polyhedron_t ph)
{
    matrixgl_t newModelMatrix, newInvModelMatrix;
    scene_node_t *child;
    polyhedron_t newph;
    bool_t hit = False;
    
    check_assertion( node != NULL, "node is NULL" );
    
    multiply_matrices( newModelMatrix, modelMatrix, node->trans );
    multiply_matrices( newInvModelMatrix, node->invtrans, invModelMatrix );
    
    if ( node->geom == Sphere ) {
        newph = copy_polyhedron( ph );
        trans_polyhedron( newInvModelMatrix, newph );
        
        hit = intersect_polyhedron( newph );
        
        free_polyhedron( newph );
    } 
    
    if (hit == True) return hit;
    
    child = node->child;
    while (child != NULL) {
        
        hit = check_polyhedron_collision_with_dag( 
                                                  child, newModelMatrix, newInvModelMatrix, ph );
        
        if ( hit == True ) {
            return hit;
        }
        
        child = child->next;
    } 
    
    return False;
} 
Ejemplo n.º 3
0
static bool
check_model_collisions(Player& plyr, const ppogl::Vec3d& pos, 
			      ppogl::Vec3d *tree_loc, float *tree_diam)
{
    if(GameConfig::disableCollisionDetection){
		return false;
	}	
	
	if(modelLocs.empty()){
		//no models? what a boring course...
		return false;
	}
		
    pp::Matrix mat;
    bool hit = false;
    ppogl::Vec3d distvec;
    float squared_dist;
        
    /* These variables are used to cache collision detection results */
    static bool last_collision = false;
    static ppogl::Vec3d last_collision_tree_loc( -999, -999, -999 );
    static double last_collision_tree_diam = 0;
    static ppogl::Vec3d last_collision_pos( -999, -999, -999 );

    /* If we haven't moved very much since the last call, we re-use
       the results of the last call (significant speed-up) */
	   
	  
    ppogl::Vec3d dist_vec = pos - last_collision_pos;
    if ( dist_vec.length2() < COLLISION_TOLERANCE ) {
	if ( last_collision ) {
	    if ( tree_loc != NULL ) {
		*tree_loc = last_collision_tree_loc;
	    }
	    if ( tree_diam != NULL ) {
		*tree_diam = last_collision_tree_diam;
	    }
	    return true;
	} else {
	    return false;
	}
    }

	ppogl::RefPtr<ModelType> model_type = (*modelLocs.begin()).getType();
    ppogl::Polyhedron *ph = model_type->ph;

	float diam=0.0;
    float height;
	ppogl::Vec3d loc;

	std::list<Model>::iterator it;
	
    for(it=modelLocs.begin();it!=modelLocs.end();it++) {
		diam = (*it).getDiameter(); 
    	height = (*it).getHeight();
		loc = (*it).getPosition();

        distvec = ppogl::Vec3d( loc.x() - pos.x(), 0.0, loc.z() - pos.z() );

	/* check distance from tree; .6 is the radius of a bounding
           sphere around tux (approximate) */
	squared_dist = ( diam/2. + 0.6 );
	squared_dist *= squared_dist;
        if ( distvec.length2() > squared_dist ) {
            continue;
        } 

	/* have to look at polyhedron - switch to correct one if necessary */
	if ( model_type != ((*it).getType()) )  {
	    model_type = (*it).getType();
	    ph = model_type->ph;
	}
		ppogl::Polyhedron ph2(*ph);

		mat.makeScaling( diam, diam, diam );
	
        trans_polyhedron( mat, ph2 );
        mat.makeTranslation( loc.x(), loc.y(), loc.z() );
        trans_polyhedron( mat, ph2 );

	const std::string& tux_root = tux[plyr.num].getRootNode();
	reset_scene_node( tux_root );
	translate_scene_node( tux_root, 
			      ppogl::Vec3d( pos.x(), pos.y(), pos.z() ) );
        hit = collide( tux_root, ph2 );
		

        if ( hit == true ) {
	    if ( tree_loc != NULL ) {
		*tree_loc = loc;
	    }
	    if ( tree_diam != NULL ) {
		*tree_diam = diam;
	    }


            break;
        } 
    } 

    last_collision_tree_loc = loc;
    last_collision_tree_diam = diam;
    last_collision_pos = pos;

    if(hit){
		last_collision = true;

		// Record collision in player data so that health can be adjusted
		plyr.collision = true;
    }else{
		last_collision = false;
    }
    return hit;
}