bool physGrapple(float lx,float ly,float lz){ bool ret = false; flip = 0; dir = glm::normalize(glm::vec3(-lx,ly,-lz)); //printf("grapple in dir %f %f %f\n",dir.x,dir.y,dir.z); //printf("looks at is %f %f %f\n",lookAt.x,lookAt.y,lookAt.z); /* btCollisionWorld::ClosestRayResultCallback RayCallback(btVector3(lookAt.x+.0*dir.x,lookAt.y-.0*dir.y,lookAt.z+.0*dir.z), btVector3(lookAt.x+50*dir.x,lookAt.y-50*dir.y,lookAt.z+50*dir.z)); dynamicsWorld->rayTest(btVector3(lookAt.x+.0*dir.x,lookAt.y-.0*dir.y,lookAt.z+.0*dir.z), btVector3(lookAt.x+50*dir.x,lookAt.y-50*dir.y,lookAt.z+50*dir.z), RayCallback); */ ////////////////////////////// glm:: vec3 ggaze = GetLookAt() - GetEye(); glm::vec3 gw = ggaze/magnitude(ggaze); gw = glm::vec3(-1.0 * gw.x, -1.0 * gw.y, -1.0 * gw.z); glm::vec3 gu = glm::cross(GetUp(), gw)/magnitude(glm::cross(GetUp(), gw)); gu *= 2; lookAt += glm::vec3(gu.x, 0, gu.z); /////////////////////////////// btCollisionWorld::ClosestRayResultCallback RayCallback(btVector3(lookAt.x+1.6*dir.x,lookAt.y-1.6*dir.y,lookAt.z+1.6*dir.z), btVector3(lookAt.x+75*dir.x,lookAt.y-75*dir.y,lookAt.z+75*dir.z)); dynamicsWorld->rayTest(btVector3(lookAt.x+1.6*dir.x,lookAt.y-1.6*dir.y,lookAt.z+1.6*dir.z), btVector3(lookAt.x+75*dir.x,lookAt.y-75*dir.y,lookAt.z+75*dir.z), RayCallback); //player->setLinearVelocity(btVector3(dir.x*50,dir.y*50,dir.z*50)); btTransform plPos; player->getMotionState()->getWorldTransform(plPos); if(RayCallback.hasHit()&& !playerGrappleActive && RayCallback.m_hitPointWorld.distance(plPos.getOrigin())>1.8) { playerJump=0; //End = RayCallback.m_hitPointWorld; //Normal = RayCallback.m_hitNormalWorld; //printf("hit!\n"); btVector3 go=RayCallback.m_hitNormalWorld*-5+player->getLinearVelocity(); if (go.length()>10)(go/go.length())*10; //player->setLinearVelocity(go); //printf("%f %f %f on hit norm",RayCallback.m_hitNormalWorld.getX(),RayCallback.m_hitNormalWorld.getY(),RayCallback.m_hitNormalWorld.getZ()); tmp = RayCallback.m_hitPointWorld; // Do some clever stuff here if(tmp.getY()>1){ playerGrappleActive =1; //SFX HERE //Sound s = Sound(); //s.playSFX("../Assets/Sounds/HookShot.mp3"); //printf("%f %f %f - player loc; %f %f %f grapple loc\n", physGetPlayerX(), physGetPlayerY(), physGetPlayerZ(), grapplingHookLocation().x, grapplingHookLocation().y, grapplingHookLocation().z); //printf("%f %f %f grapple sfx distance\n", abs(grapplingHookLocation().x - physGetPlayerX()), abs(grapplingHookLocation().y - physGetPlayerY()), abs(grapplingHookLocation().z - physGetPlayerZ())); musicPlayer.BGM.play3DSFX("../Assets/Sounds/HookShot.mp3", physGetPlayerX(), physGetPlayerY(), physGetPlayerZ(), grapplingHookLocation().x, grapplingHookLocation().y, grapplingHookLocation().z); ret = true; } } lookAt -= glm::vec3(gu.x, 0, gu.z); return ret; }
float physGetHeight(float x, float y){ btCollisionWorld::ClosestRayResultCallback RayCallback(btVector3(x,1050,y), btVector3(x,y,1100)); dynamicsWorld->rayTest(btVector3(x,1050,y), btVector3(x,y,1100), RayCallback); if(RayCallback.hasHit()){ return RayCallback.m_hitPointWorld.getY(); } return -1.0; }
// raycast function by jazztickets // copied from: http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?f=9&t=7202 // and modified by xor on Apr 27, 2014 bool RagdollDemo::raycast(const btVector3 &start, btVector3 &end) { btCollisionWorld::ClosestRayResultCallback RayCallback(start, end); m_dynamicsWorld->rayTest(start, end, RayCallback); if(RayCallback.hasHit()) { end = RayCallback.m_hitPointWorld; return true; } return false; }
btCollisionWorld::ClosestRayResultCallback PhysicsEngine::shootRay(glm::vec3 worldPosition, glm::vec3 worldDirection) { btCollisionWorld::ClosestRayResultCallback RayCallback(btVector3(worldPosition[0], worldPosition[1], worldPosition[2]), btVector3(worldDirection[0], worldDirection[1], worldDirection[2])); _dynamicsWorld->rayTest(btVector3(worldPosition[0], worldPosition[1], worldPosition[2]), btVector3(worldDirection[0], worldDirection[1], worldDirection[2]), RayCallback); return RayCallback; }
bool Enemy::nearToCollision(btDiscreteDynamicsWorld* dynamicsWorld) { btVector3 start = btVector3(position.x, position.y, position.z); btVector3 end = btVector3(position.x + direction.x * 10, position.y, position.z + direction.z * 10); btCollisionWorld::ClosestRayResultCallback RayCallback(start, end); dynamicsWorld->rayTest(start, end, RayCallback); if (RayCallback.hasHit()) { return true; } else { return false; } }
bool DynamicsWorld::RayCastHitPoint(const glm::vec3& start, const glm::vec3& end, glm::vec3& hitpoint, Entity** hitObject) { auto Start = btVector3(start.x, start.y, start.z); auto End = btVector3(end.x, end.y, end.z); btCollisionWorld::ClosestRayResultCallback RayCallback(Start, End); // Perform raycast dynamicsWorld->rayTest(Start, End, RayCallback); if (RayCallback.hasHit()) { hitpoint = glm::vec3(RayCallback.m_hitPointWorld.getX(), RayCallback.m_hitPointWorld.getY(), RayCallback.m_hitPointWorld.getZ()); auto fb = (FreeBody*)(RayCallback.m_collisionObject->getUserPointer()); *hitObject = fb->Entity; } return RayCallback.hasHit(); }
// Performs raycasting on the world and returns the point of collision bool _Physics::RaycastWorld(const btVector3 &Start, btVector3 &End, btVector3 &Normal) { if(Enabled) { btCollisionWorld::ClosestRayResultCallback RayCallback(Start, End); RayCallback.m_collisionFilterMask = FILTER_CAMERA; // Perform raycast World->rayTest(Start, End, RayCallback); if(RayCallback.hasHit()) { End = RayCallback.m_hitPointWorld; Normal = RayCallback.m_hitNormalWorld; return true; } } return false; }
int grappleInRange(float lx,float ly,float lz){ //glm::vec3 tempLookAt; dir = glm::normalize(glm::vec3(-lx,ly,-lz)); glm:: vec3 ggaze = GetLookAt() - GetEye(); glm::vec3 gw = ggaze/magnitude(ggaze); gw = glm::vec3(-1.0 * gw.x, -1.0 * gw.y, -1.0 * gw.z); glm::vec3 gu = glm::cross(GetUp(), gw)/magnitude(glm::cross(GetUp(), gw)); gu *= 2; lookAt += glm::vec3(gu.x, 0, gu.z); btCollisionWorld::ClosestRayResultCallback RayCallback(btVector3(lookAt.x+1.6*dir.x,lookAt.y-1.6*dir.y,lookAt.z+1.6*dir.z), btVector3(lookAt.x+75*dir.x,lookAt.y-75*dir.y,lookAt.z+75*dir.z)); dynamicsWorld->rayTest(btVector3(lookAt.x+1.6*dir.x,lookAt.y-1.6*dir.y,lookAt.z+1.6*dir.z), btVector3(lookAt.x+75*dir.x,lookAt.y-75*dir.y,lookAt.z+75*dir.z), RayCallback); lookAt -= glm::vec3(gu.x, 0, gu.z); // int ret = //inRange(50,75,glm::lookAt(playerPoss(),glm::vec3(tempLookAt.x+75*dir.x,tempLookAt.y+75*dir.y,tempLookAt.z+75*dir.z),glm::vec3(0,1,0))); //printf("grapple is %d range\n",RayCallback.hasHit()); //if(RayCallback.hasHit()) printf("%f %f %f",RayCallback.m_hitPointWorld.getX(),RayCallback.m_hitPointWorld.getY(),RayCallback.m_hitPointWorld.getZ()); return RayCallback.hasHit(); }
PelletHitData * Shotgun::getHitData(btVector3 &start, btVector3 &end) //returns playerID or -1 if no hit { CharacterRayTest RayCallback(start, end, owner); dynamicsWorld->rayTest(start, end, RayCallback); PelletHitData * hitData = new PelletHitData(); hitData->playerID = -1; hitData->hitObject = NULL; hitData->end = end; if (RayCallback.hasHit()) { hitData->end = end = RayCallback.m_hitPointWorld; const btCollisionObject * hitObject = RayCallback.m_collisionObject; hitData->hitObject = ((RigidObject*)((btRigidBody*)hitObject)->getOwner()); if (hitObject != NULL) { RigidObject * ownerPointer = ((RigidObject*)((btRigidBody*)hitObject)->getOwner()); if (ownerPointer != NULL) hitData->playerID = ownerPointer->getCharID(); } } return hitData; }
bool Enemy::seesPlayer(btDiscreteDynamicsWorld* dynamicsWorld, vec3* playerPosition) { float endX, endZ; btVector3 start = btVector3(position.x, position.y, position.z); if (playerPosition->x > position.x) endX = playerPosition->x - 0.5f; else endX = playerPosition->x + 0.5f; if (playerPosition->z > position.z) endZ = playerPosition->z - 0.5f; else endZ = playerPosition->z + 0.5f; btVector3 end = btVector3(endX, position.y, endZ); btCollisionWorld::ClosestRayResultCallback RayCallback(start, end); dynamicsWorld->rayTest(start, end, RayCallback); if (RayCallback.hasHit() || distanceBetween2Points(position.x, position.z, endX, endZ) > sightDistance ) { return false; } else { float vectorToPlayerX = playerPosition->x - position.x; float vectorToPlayerZ = playerPosition->z - position.z; float angleBetween = radiansToDegrees(acosf((vectorToPlayerX * direction.x + vectorToPlayerZ * direction.z) / (sqrt(pow(vectorToPlayerX, 2) + pow(vectorToPlayerZ, 2)) * sqrt(pow(direction.x, 2) + pow(direction.z, 2))))); if (angleBetween < sightAngle / 2) { seeing_count = 0; return true; } else { return false; } } }
void ray_test(glm::vec2 Mouse, glm::vec2 screen_size) { //glm::vec2 Mouse = mouse_pos; Mouse.y = screen_size.y - Mouse.y; //glm::vec4 RayStart = glm::vec4(2*Mouse.x/screen_size.x - 1,2*Mouse.y/screen_size.y - 1, -1, 1); //glm::vec4 RayEnd = RayStart; RayEnd.z = 0; glm::vec4 RayStart( ((float)Mouse.x/(float)screen_size.x - 0.5f) * 2.0f, // [0,1024] -> [-1,1] ((float)Mouse.y/(float)screen_size.y - 0.5f) * 2.0f, // [0, 768] -> [-1,1] -1.0, // The near plane maps to Z=-1 in Normalized Device Coordinates 1.0f ); glm::vec4 RayEnd = RayStart; RayEnd.z = 0.0; /*lRayEnd_NDC( ((float)mouseX/(float)screenWidth - 0.5f) * 2.0f, ((float)mouseY/(float)screenHeight - 0.5f) * 2.0f, 0.0, 1.0f );*/ //RayStart = glm::inverse( get_g_projection()*get_g_view() )*RayStart; RayStart /= RayStart.w; //RayEnd = glm::inverse( get_g_projection()*get_g_view() )*RayEnd; RayEnd /= RayEnd.w; // Faster way (just one inverse) glm::mat4 M = glm::inverse(get_g_projection() * get_g_view()); glm::vec4 lRayStart_world = M * RayStart; lRayStart_world/=lRayStart_world.w; glm::vec4 lRayEnd_world = M * RayEnd ; lRayEnd_world /=lRayEnd_world.w; //glm::vec4 RayDirection = glm::normalize(RayEnd-RayStart); glm::vec3 lRayDir_world(lRayEnd_world - lRayStart_world); lRayDir_world = 1000.0f * glm::normalize(lRayDir_world); //btCollisionWorld::AllHitsRayResultCallback RayCallback(btVector3(lRayStart_world.x,lRayStart_world.y,lRayStart_world.z), btCollisionWorld::ClosestRayResultCallback RayCallback(btVector3(lRayStart_world.x,lRayStart_world.y,lRayStart_world.z), btVector3(lRayDir_world.x,lRayDir_world.y,lRayDir_world.z)); btDiscreteDynamicsWorld* TheWorld = get_physics_world(); TheWorld->rayTest(btVector3(lRayStart_world.x,lRayStart_world.y,lRayStart_world.z), btVector3(lRayDir_world.x,lRayDir_world.y,lRayDir_world.z), RayCallback); int closest = 0; if ( RayCallback.hasHit() ) { //for ( int i = 0; i < RayCallback.m_collisionObjects.size(); i++ ) // { /* std::cout<<i<<": "<<glm::ToVec3(RayCallback.m_hitPointWorld[i])<<" " <<RayCallback.m_hitFractions[i]<<" " <<RayCallback.m_collisionObjects[i]->getCollisionShape()->getShapeType()<<std::endl; */ //printf("collisions %i distance %f ID: %i\n", i, RayCallback.m_hitPointWorld[i].getZ(), RayCallback.m_collisionObjects[i]->getUserIndex()); // if (RayCallback.m_hitPointWorld[i].getZ() > // RayCallback.m_hitPointWorld[closest].getZ() ) // closest = i; // } //printf("closest %i\n", closest); } else{ printf("no hits!!\n"); return; } //struct d3_object * clicked_object = find_object_by_id(RayCallback.m_collisionObjects[closest]->getUserIndex()); struct d3_object * clicked_object = find_object_by_id(RayCallback.m_collisionObject->getUserIndex()); //printf("id trying to find: %i, ID GOT: %i\n", RayCallback.m_collisionObjects[closest]->getUserIndex(), clicked_object->id); //clicked_object->flags = clicked_object->flags | D3_OBJECT_NORENDER; printf("physics state: %i\n", clicked_object->physics_obj->fallRigidBody->getActivationState()); //delete_object(clicked_object); }