void QuatTestComp::ResetCalculation( GActorHandle i_Handle ) { GActor* actor = GActor::FromHandle( i_Handle ); GVector3 facing = -actor->m_rotation.Dir(); // TODO: Fix the z-hack soon.. GVector3 worldZ( 0.0f, 0.0f, 1.0f ); float angle = acosf( worldZ.Dot( facing ) ); GVector3 ortho = worldZ.Cross( facing ); if( ortho.LengthSquared() > 0.0f ) { ortho.Normalize(); m_quat.FromAngleAxis( angle, ortho ); } }
void QuatTestComp::NewRandomVector( ) { m_targetQuat.Identify(); GVector3 randomVector( 0.0f, 0.5f, 0.0f ); randomVector.x( static_cast<float>( rand() % 100) / 100.0f - 0.5f ); randomVector.z( static_cast<float>( rand() % 100 ) / 100.0f - 0.5f ); randomVector.Normalize(); float angle = acosf( randomVector.Dot( GVector3::Forward ) ); GVector3 cross = GVector3::Forward.Cross( randomVector ); cross.Normalize(); m_targetQuat.FromAngleAxis( angle, cross ); }
void CtfAiPlayerComp::HandlePathFound( GActorHandle i_actor ) { GActor* actor = GActor::FromHandle( i_actor ); m_LastNodePos = actor->m_position; // the closest node could be behind us. make sure that's not the case. this could be breaking some rules... u16 selected = m_PathToTarget.m_PathNodes[0]; if( m_PathToTarget.m_PathNodes.size() > 1 ) { u16 second = m_PathToTarget.m_PathNodes[1]; GVector3 firstNode, secondNode; g_WayPointManager.NodePosition( m_PathToTarget.m_PathNodes[0], firstNode); g_WayPointManager.NodePosition( m_PathToTarget.m_PathNodes[1], secondNode ); GVector3 nodeDir = secondNode - firstNode; GVector3 toFirst = firstNode - m_LastNodePos; if( nodeDir.Dot( toFirst ) < 0.0f ) { m_PathIndex = 1; selected = m_PathToTarget.m_PathNodes[1]; m_TargetNodePos = secondNode; if( m_PathToTarget.m_PathNodes.size() > 2 ) g_WayPointManager.NodePosition( m_PathToTarget.m_PathNodes[2], m_NextTargetNodePos ); else m_NextTargetNodePos = secondNode; } else { m_PathIndex = 0; m_TargetNodePos = firstNode; m_NextTargetNodePos = secondNode; //m_CurrentPathNode = selected; } } else { m_PathIndex = 0; //m_CurrentPathNode = selected; g_WayPointManager.NodePosition( selected, m_TargetNodePos ); if( m_PathToTarget.m_PathNodes.size() > 1 ) g_WayPointManager.NodePosition( m_PathToTarget.m_PathNodes[1], m_NextTargetNodePos ); else m_NextTargetNodePos = m_TargetNodePos; } m_CurrentPathNode = selected; }
GGlyphStyle() { // full opaque gOpaque = G_TRUE; gOpacity = 1; // white color gColor.Set(1, 1, 1); gGlyphIndex = -1; }
static float GCtfHeuristic( GWayNode* i_current, GWayNode* i_next, void* i_data ) { CtfAiPlayerComp* comp = (CtfAiPlayerComp*) i_data; GVector3 toEnd = i_next->m_Position - i_current->m_Position; float toTargetLen = toEnd.LengthSquared(); GActor* player = GActor::FromHandle( comp->m_Player ); if( !player ) return 1.0f; GVector3 toPlayer = player->m_position - i_current->m_Position; float toPlayerLen = toPlayer.LengthSquared(); if( toPlayerLen < (250.0f * 250.0f) * 2.0f ) return FLT_MAX / 5.0f; // this node is BASICALLY off limits... return toTargetLen; //return toTargetLen; }
void QuatTestComp::EndUpdate( GActorHandle i_actor ) { if ( m_keyDown ) { // Attempt 1. Works, but weird... /* float totalRot = fabs(acosf(m_quat.DotProduct(m_targetQuat))); // time / (rot speed / totalRot). if (totalRot > 0.0f) m_quat.Slerp(m_quat, m_targetQuat, g_Clock::Get().SecondsSinceLastFrame() * (GMath::Deg2Rad(30.0f) / totalRot)); m_quat.Normalize(); */ // Attempt 2. GVector3 target = m_targetQuat * GVector3::Forward; target.Normalize(); GVector3 current = m_quat * GVector3::Forward; current.Normalize(); float dot = target.Dot( current ); if (dot < 0.99999f) { GVector3 cross = current.Cross( target ); float sign = GMath::Sign( cross.y() ); cross.Normalize(); GQuat delta; delta.FromAngleAxis( GMath::Deg2Rad( 30.0f ) * g_Clock::Get().SecondsSinceLastFrame(), cross ); m_quat = m_quat * delta; if( GMath::Sign( ( m_quat * GVector3::Forward ).Cross( m_targetQuat * GVector3::Forward ).y() ) != sign ) m_quat = m_targetQuat; } } GActor* actor = GActor::FromHandle( i_actor ); assert( actor ); GVector3 vStartPoint = actor->m_position; GVector3 vEndPoint = actor->m_position + ( m_quat * GVector3::Forward * 100.0f ); GDebugDraw::DrawLine( vStartPoint, vEndPoint, 0.002f, 0.002f, GDebugDraw::RED ); GVector3 vEndTargetPoint = actor->m_position + ( m_targetQuat * GVector3( 0.0f, 0.0f, 100.0f ) ); GDebugDraw::DrawLine( vStartPoint, vEndTargetPoint, 0.002f, 0.002f, GDebugDraw::BLUE ); }
Color Union::rayTrace(CRay ray, int depth, CObject* &_object,IntersectResult*& res) { if(depth>max_depth) return Color::white(); int size = (int)CVector.size(); float distance = 100000000.0f; CObject* primitive_near = NULL; bool visible = true; Color totalColor = Color::black(); CObject* pLight; int num=0; IntersectResult s = IntersectResult::noHit(); for(int i=0;i<size;i++){ CObject* primitive = CVector.at(i); IntersectResult result = primitive->isIntersected(ray); if(result.isHit){ if(result.distance < distance){ s = result; distance = result.distance; primitive_near = result.object; num = i; } } } _object = primitive_near; res = &s; if(primitive_near == NULL) { return Color::black(); } else if(primitive_near->isLight()){ return Color::white(); } else if(!s.front){ cout<<depth<<" "<<s.object->code<<endl; GVector3 v = ray.getDirection(); v = v.normalize(); GVector3 normal = s.normal.normalize(); double cosA = v.dotMul(normal); double sinA = sqrt(1-cosA*cosA); double n = 1.000 / 1.500; //cout<<sinA<<endl; if(sinA >= n){ //cout<<"zhixingle"<<endl; CRay newray; newray.setDirection(normal*(-cosA) + (ray.getDirection() - normal*cosA).normalize()*sinA); newray.setOrigin(s.position + ray.getDirection()*1e-3); CObject* no_use = NULL; IntersectResult* n0=NULL; Color refraction_second = rayTrace(newray,depth+1,no_use,n0); //cout<<refraction_second.r<<" "<<refraction_second.g<<" "<<refraction_second.b<<endl; Color absorbance(0,0,0); if(n0){ Color absorbance = primitive_near->getMaterial()->getColor().multiply(0.15f * ((-1)*n0->distance)); } Color transparancy = Color(exp(absorbance.r),exp(absorbance.g),exp(absorbance.b)); return refraction_second.moderate(transparancy); } else{ double sinB = sinA / n; double cosB = sqrt(1 - sinB*sinB); CRay newray; newray.setDirection(normal*cosB + (v-normal*cosA).normalize()*sinB); newray.setOrigin(s.position + newray.getDirection()*1e-3); //cout<<"Origin:"<<newray.getOrigin().getX()<<" "<<newray.getOrigin().getY()<<" "<<newray.getOrigin().getZ()<<endl; //cout<<"Direction:"<<newray.getDirection().getX()<<" "<<newray.getDirection().getY()<<" "<<newray.getDirection().getZ()<<endl; CObject* no_use = NULL; IntersectResult* n0=NULL; Color refraction_second = rayTrace(newray,depth+1,no_use,n0); //cout<<refraction_second.r<<" "<<refraction_second.g<<" "<<refraction_second.b<<endl; Color absorbance(0,0,0); if(n0){ Color absorbance = primitive_near->getMaterial()->getColor().multiply(0.15f * ((-1)*n0->distance)); } Color transparancy = Color(exp(absorbance.r),exp(absorbance.g),exp(absorbance.b)); return refraction_second.moderate(transparancy); } } else{ GVector3 point; point = ray.getPoint(distance); for(int i=0;i<size;i++){ CObject* primitive = CVector.at(i); if(primitive->isLight()){ pLight = primitive; GVector3 inDir = ((Lamp*) primitive)->getCenter() - point; inDir = inDir.normalize(); CRay line(point+inDir*0.001,inDir); for(int j=0;j<size;j++){ CObject* ano_primitive = CVector.at(j); if(!ano_primitive->isLight() && j!=num){ IntersectResult result = ano_primitive->isIntersected(line); if(result.isHit && result.distance < inDir.getLength()){ visible = false; break; } } } break; } } } GVector3 point; point = ray.getPoint(distance); if(visible){ GVector3 lig = ((Lamp*)pLight)->getCenter() - point; lig = lig.normalize(); primitive_near->getMaterial()->setLightDir(lig); totalColor = totalColor.add( primitive_near->getMaterial()->sample(ray, point, primitive_near->getNormal(point)) ); } float reflection = primitive_near->getMaterial()->getRef(); if( (reflection>0.0f) && (depth<max_depth) ){ GVector3 normal_point = primitive_near->getNormal(point); normal_point = normal_point.normalize(); float s0 = ray.getDirection().dotMul(normal_point) * (-2.0f); CRay newRay; newRay.setDirection(normal_point * s0 + ray.getDirection()); newRay.setOrigin(point + newRay.getDirection()*1e-3); CObject* no_use = NULL; IntersectResult* n0=NULL; Color reflectionColor = rayTrace(newRay,depth+1,no_use,n0); reflectionColor = reflectionColor.multiply(reflection); //reflectionColor = reflectionColor.moderate(primitive_near->getMaterial()->getColor()); totalColor = totalColor.add(reflectionColor.moderate(primitive_near->getMaterial()->getColor())); } float refraction = primitive_near->getMaterial()->getRefr(); //cout<<refraction<<endl; if((refraction>0.0f) && (depth<max_depth)){ GVector3 normal_point = primitive_near->getNormal(point); GVector3 Direction = ray.getDirection().normalize(); normal_point = normal_point.normalize(); float cosA = -Direction.dotMul(normal_point); float sinA = sqrt(1-cosA*cosA); float sinB = sinA / 1.5; float cosB = sqrt(1 - sinB*sinB); //cout<<sinA<<" "<<cosA<<" "<<sinB<<" "<<cosB<<endl; CRay newray; newray.setDirection(normal_point*(-cosB) + (Direction + normal_point*cosA).normalize()*sinB); newray.setOrigin(point + newray.getDirection() * 1e-3); CObject* no_use = NULL; IntersectResult* n0=NULL; Color refractionColor = rayTrace(newray,depth+1,no_use,n0); refractionColor = refractionColor.multiply(refraction); //refractionColor = refractionColor.moderate(primitive_near->getMaterial()->getColor()); totalColor = totalColor.add(refractionColor); } return totalColor; }
// alright. this is the "state machine" :( void CtfAiPlayerComp::Update( GActorHandle i_actor ) { GActor* actor = GActor::FromHandle( i_actor ); // make sure the player is valid every frame. if( !m_Player ) { m_Player = g_World::Get().GetActorByName( "PlayerOne" ); if( !m_Player ) return; } g_AudioManager.UpdateChannelPosition( m_walkOnGroundSlow, actor->m_position ); StaminaBrain( i_actor ); if( m_usingStamina ) m_staminaMeter -= g_Clock::Get().SecondsSinceLastFrame() * 5.0f; else m_staminaMeter += g_Clock::Get().SecondsSinceLastFrame() * 1.66667f; GMath::Clamp( &m_staminaMeter, 0.0f, 10.0f ); switch( m_State ) { case SEEK: Seek( i_actor ); break; case CAPTURE: Capture( i_actor ); break; case PURSUE: Pursue( i_actor ); break; } if( m_PathToTarget.m_pathFound ) { GGroundFollowerComp* gFollower = GetComponent<GGroundFollowerComp>( i_actor ); assert( gFollower ); // you f****d up. GVector3 velocity; if( m_PathToTarget.m_PathNodes.size() <= 1 && m_State == PURSUE ) { GActorHandle playerHandle = g_World::Get().GetActorByName( "PlayerOne" ); GActor* player = GActor::FromHandle( playerHandle ); if( !player ) velocity = GVector3( 0.0f, 0.0f, 0.0f ); else velocity = player->m_position - actor->m_position; } else { velocity = m_TargetNodePos - actor->m_position; } velocity.Normalize(); if( m_usingStamina ) velocity *= 1000.0f; else velocity *= 500.0f; m_LastVelocity = velocity; gFollower->m_velocity = velocity; } }