Exemplo n.º 1
0
CCopyEntity *EntityManager::CreateEntity( BaseEntityHandle& rBaseEntityHandle,
									   const Vector3& rvPosition,
							           const Vector3& rvVelocity,
									   const Vector3& rvDirection)
{
	CCopyEntityDesc new_entity;

	new_entity.pBaseEntityHandle = &rBaseEntityHandle;

	new_entity.SetWorldPosition( rvPosition );
	new_entity.SetWorldOrient( CreateOrientFromFwdDir(rvDirection) );
	new_entity.vVelocity  = rvVelocity;
	new_entity.fSpeed = Vec3Length( rvVelocity );

	return CreateEntity( new_entity );
}
Exemplo n.º 2
0
  float3* Vec3Normalize(float3 *pout, const float3 *pv)
  {
    float3 out;
    float norm;

    norm = Vec3Length(pv);
    if ( !norm )
    {
      out.x = 0.0f;
      out.y = 0.0f;
      out.z = 0.0f;
    }
    else
    {
      out.x = pv->x / norm;
      out.y = pv->y / norm;
      out.z = pv->z / norm;
    }
    *pout = out;
    return pout;
  }
Exemplo n.º 3
0
//-------------------------------------------------------------------------------------------------
// 境界球取得
//-------------------------------------------------------------------------------------------------
int ComputeBoundingSphere(MESH* mesh, VECTOR3 *pvCenter, float *pfRadius)
{
	VECTOR3		vecMin, vecMax ,vecLen, vecCenter;
	float		fMax, fRadius;
	int			i;

	if (mesh == NULL) return 0;
	if (pvCenter == NULL) pvCenter = &vecCenter;
	if (!ComputeBoundingBox(mesh, &vecMin, &vecMax)) return 0;
	pvCenter->x = (vecMin.x + vecMax.x) / 2.0f;
	pvCenter->y = (vecMin.y + vecMax.y) / 2.0f;
	pvCenter->z = (vecMin.z + vecMax.z) / 2.0f;
	fMax = 0.0f;
	for (i = 0; i < mesh->vnum; i++) {
		vecLen.x = mesh->vertex[i].x - pvCenter->x;
		vecLen.y = mesh->vertex[i].y - pvCenter->y;
		vecLen.z = mesh->vertex[i].z - pvCenter->z;
		fRadius = Vec3Length(&vecLen);
		if (fMax < fRadius) fMax = fRadius;
	}
	if (pfRadius) *pfRadius = fMax;
	return 1;
}
Exemplo n.º 4
0
CameraInstance* CreateDefaultCamera(EditableScene *es, const char *camTag, const char *nodeTag, const char *instTag)
{
	Camera *pCamera = es->CreateCamera();
	pCamera->SetTag(camTag);
	Node *pNode = es->CreateNode(NULL);
	pNode->SetTag(nodeTag);
	CameraInstance *pCI = es->CreateCameraInstance(pNode, pCamera);
	pCI->SetTag(instTag);
	Property* pTransform = pNode->AppendTransform(Transform::MATRIX, Zero)->GetDelegate(NULL);

	AutoPtr<AccessProviderLocal> providerLocal;
	CreateAccessProviderLocal(&providerLocal);

	float3 aabb(es->GetSceneAABB(-1, 0, providerLocal));
	float3 center(es->GetSceneCenter(-1, 0, providerLocal));
	float4x4 ypr;
	MatrixRotationYawPitchRoll(&ypr, 3.141692f/4, -3.141692f/4, 0);

	float4x4 t0;
	MatrixTranslation(&t0, center.x, center.y, center.y);

	float4x4 t1;
	float bbD = Vec3Length(&aabb);
	MatrixTranslation(&t1, 0, 0, bbD*2);
	float4x4 up = es->GetUpAxis();
	float4x4 upI;
	MatrixInverse(&upI, 0, &up);
	float4x4 minusZ;
	MatrixScaling(&minusZ, 1,1,-1);
	float4x4 m = t1*ypr*t0*minusZ*upI;
	MatrixTranspose(&m, &m);
	pTransform->SetValue(m);
	pCamera->SetFar(10*bbD);

	return pCI;
}
void JL_CollisionDetect_Box_Box( CJL_Shape_Box& rBox0, CJL_Shape_Box& rBox1,
							     CJL_CollisionFunctor& rColFunctor )
{

	// set up 15 axes to check overlaps between boxes
	Vector3 avAxis[15];
	avAxis[0] = rBox0.GetWorldOrient().GetColumn(0);
	avAxis[1] = rBox0.GetWorldOrient().GetColumn(1);
	avAxis[2] = rBox0.GetWorldOrient().GetColumn(2);
	avAxis[3] = rBox1.GetWorldOrient().GetColumn(0);
	avAxis[4] = rBox1.GetWorldOrient().GetColumn(1);
	avAxis[5] = rBox1.GetWorldOrient().GetColumn(2);

	Vec3Cross( avAxis[6],  avAxis[0], avAxis[3] );
	Vec3Cross( avAxis[7],  avAxis[0], avAxis[4] );
	Vec3Cross( avAxis[8],  avAxis[0], avAxis[5] );
	Vec3Cross( avAxis[9],  avAxis[1], avAxis[3] );
	Vec3Cross( avAxis[10], avAxis[1], avAxis[4] );
	Vec3Cross( avAxis[11], avAxis[1], avAxis[5] );
	Vec3Cross( avAxis[12], avAxis[2], avAxis[3] );
	Vec3Cross( avAxis[13], avAxis[2], avAxis[4] );
	Vec3Cross( avAxis[14], avAxis[2], avAxis[5] );

	Scalar afDepth[15], fLength;
	Scalar fMinDepth = 100.0f;
	int i, iMinAxis = -1;
	float fCollTolerance = rColFunctor.m_fCollTolerance;
	for(i=0; i<15; i++)
	{
		afDepth[i] = 101.0f;

		if( Vec3LengthSq(avAxis[i]) < 0.0000001f )
			continue;	// invalid axis

		// separation axis test between 'rBox0' and 'rBox1'
		if( !rBox0.SepAxisTest( afDepth[i], avAxis[i], rBox1, fCollTolerance ) )
			return;	// no overlap

		if( 6<=i )
		{	// need to normalize axis length and penetration depth
			fLength = Vec3Length( avAxis[i] );
			if( fLength < 0.000001f )
				continue;

			avAxis[i] /= fLength;
			afDepth[i] /= fLength;
		}

		if( afDepth[i] < fMinDepth )
		{
			fMinDepth = afDepth[i];
			iMinAxis = i;
		}
	}

	if( iMinAxis < 0 )
		return;	// no collision detected

	if( 0.3f < fMinDepth || fMinDepth < 0.0f )
		int iUnexpected = 1;


	// make sure the contact normal is heading toward box0
	Vector3 vBox1ToBox0 = rBox0.GetWorldPosition() - rBox1.GetWorldPosition();
	if( Vec3Dot( vBox1ToBox0, avAxis[iMinAxis] ) < 0 )
		avAxis[iMinAxis] *= -1.0f;

	Vector3& N = avAxis[iMinAxis];

	TCFixedVector< CJL_CollPointInfo, MAX_CONTACTS_PER_BOX_PAIR > vecCollPointInfo;

	if( /*use_bsptree_collision_detect*/ false )
	{
		// add contact points based on the minimum penetration depth
//		AddContactPoint( avAxis[iMinAxis], fMinDepth, rBox0, rBox1, rColFunctor );
		AddContactPoint( vecCollPointInfo,
						 avAxis[iMinAxis], fMinDepth, rBox0, rBox1, rColFunctor );
	}
	else
		AddCollisionPoints( vecCollPointInfo, avAxis[iMinAxis], fMinDepth, rBox0, rBox1, rColFunctor );

	// also get the point from SAT so that we can obtain
	// the penetration depth for each intersection point
	Vector3 vSATPoint = Vector3(0,0,0);
	switch(iMinAxis)
	{
	case 0:
	case 1:
	case 2:
		{
			// box0 face, box1 corner collision
			// get the lowest point on the box1 along box1 normal
			Vector3 vP1;
			GetSupportPoint( vP1, rBox1, -N );
			vSATPoint = ( vP1 - (0.5f * fMinDepth) * N );
		}
		break;

	case 3:
	case 4:
	case 5:
		{
			// box0 corner, box1 face collision
			// get the lowest point on the box1 along box1 normal
			Vector3 vP0;
			GetSupportPoint( vP0, rBox0, N );
			vSATPoint = ( vP0 + (0.5f * fMinDepth) * N );
		}
		break;

	// we have an edge/edge collision
	case 6:
	case 7:
	case 8:
	case 9:
	case 10:
	case 11:
	case 12:
	case 13:
	case 14:
		{
			// retrieve which edges collided
			int i = iMinAxis - 6;
			int ia = i / 3;
			int ib = i - ia * 3;
			assert(0 <= ia && ia < 3);
			assert(0 <= ib && ib < 3);
			// find two vP0, vP1 oint on both edges
			Vector3 vP0, vP1;
			GetSupportPoint( vP0, rBox0, N );
			GetSupportPoint( vP1, rBox1, -N );
			// find edge intersection
			// plane along N and F, and passing through PB
			Vector3 vPlaneNormal = Vec3Cross( N, rBox1.GetWorldOrient().GetColumn(ib) );
			Scalar plane_dist = Vec3Dot( vPlaneNormal, vP1 );
			// find the intersection T, where Pintersection = vP0 + t * box edge dir
			Scalar div = Vec3Dot( rBox0.GetWorldOrient().GetColumn(ia), vPlaneNormal );
			// plane and ray colinear, skip the intersection.
			if( fabsf(div) < SCALAR_TINY )
				return;	// false;

			Scalar t = (plane_dist - Vec3Dot(vP0, vPlaneNormal)) / div;
			// point on edge of box0
			vP0 += rBox0.GetWorldOrient().GetColumn(ia) * t;
			vSATPoint = ( vP0 + (0.5f * fMinDepth) * N );
		}
		break;

	}

	if( vecCollPointInfo.size() < MAX_CONTACTS_PER_BOX_PAIR )
	{
		const Vector3& vNewActorPos0 = rBox0.GetPhysicsActor()->GetPosition();
		const Vector3& vNewActorPos1 = rBox1.GetPhysicsActor()->GetPosition();
		Scalar old_depth = vecCollPointInfo.back().InitialPenetration;
		vecCollPointInfo.push_back( CJL_CollPointInfo( vSATPoint - vNewActorPos0,
													   vSATPoint - vNewActorPos1,
													   old_depth ) );

		vecCollPointInfo.back().vContactPosition = vSATPoint;
	}

	rColFunctor.AddTemporaryContacts( &rBox0,
		                              &rBox1,
									  N,
									  &vecCollPointInfo[0],
									  vecCollPointInfo.size() );
}
Exemplo n.º 6
0
	//--------------------------------------------------------------------------------------------------------------
	//获取该矢量的长度
	float Vector3::GetLength() const
	{
		return Vec3Length( this );
	}
Exemplo n.º 7
0
//-------------------------------------
// Vec3LengthReverce()
//-------------------------------------
float Vec3LengthReverce(const Vector3 &in)
{
    return (1.0f / Vec3Length(in));
}
Exemplo n.º 8
0
//
//	EWaving::Update
//
void EWaving::Update( float dtime, const EVec4 &_view_pos, const EQuat &orient )
{
	time	+=	dtime;

	r_sky->SetPose( _view_pos, QuatIdentity() );
	r_sky->SetFlag( RSE_HIDDEN );
	
	EFrustum fr	=	ESciVis::self->view.frustum;
	
	EVec4	view_pos	=	_view_pos;

	float	elevation		=	sqrt(1+abs(view_pos.z));
	float	view_area_scale	=	elevation * 100;
	float	step			=	1.0f / 64.0f * view_area_scale;

	view_pos.x	=	floor( view_pos.x / step ) * step;
	view_pos.y	=	floor( view_pos.y / step ) * step;

	//
	//	Make grid :
	//
	IPxTriMesh	mesh	=	sea_mesh->Clone();
	uint n = mesh->GetVertexNum();
	
	for (uint i=0; i<n; i++) {
		EVertex	v;
		
		float	wave_factor		=	1;
		
		v			=	mesh->GetVertex( i );
		float gx	=	v.position.x;
		float gy	=	v.position.y;
		
		float x		=	gx * view_area_scale + view_pos.x;
		float y		=	gy * view_area_scale + view_pos.y;
		
		wave_factor	=	clamp<float>(1 - (gx*gx + gy*gy), 0, 1);
		wave_factor	=	pow( wave_factor, 2 );
		
		//
		//	write vertex :
		//		
		v.position.x	=	x;
		v.position.y	=	y;
		v.position.z	=	0;
		
		float	dist	=	Vec3Length( v.position - Vec4ToVec3(view_pos) );

		v.position.z	=	GetPosition( Vec3ToVec4(v.position) ).z * wave_factor;
		/*if (dist < view_area_scale*0.1 || fr.IsPointInside( EVec4(x,y,0,1), 0.1 * dist )) {
		}  */

		mesh->SetVertex( i, v );
	}
	
	//
	//	post process mesh :
	//
	for (uint i=0; i<n; i++) {
		EVertex	v	=	mesh->GetVertex( i );
		v.uv0.x		=	v.position.x/4;
		v.uv0.y		=	v.position.y/4;
		mesh->SetVertex( i, v );
	}
	
	r_ent->SetMesh( mesh );
	//r_ent->SetFlag( RSE_HIDDEN );  
}