Exemple #1
0
/*************************************************************************
 * D3DXComputeBoundingSphere
 */
HRESULT WINAPI D3DXComputeBoundingSphere(CONST D3DXVECTOR3* pfirstposition, DWORD numvertices, DWORD dwstride, D3DXVECTOR3 *pcenter, FLOAT *pradius)
{
    D3DXVECTOR3 temp, temp1;
    FLOAT d;
    unsigned int i;

    if( !pfirstposition || !pcenter || !pradius ) return D3DERR_INVALIDCALL;

    temp.x = 0.0f;
    temp.y = 0.0f;
    temp.z = 0.0f;
    temp1 = temp;
    d = 0.0f;
    *pradius = 0.0f;

    for(i=0; i<numvertices; i++)
    {
        D3DXVec3Add(&temp1, &temp, (D3DXVECTOR3*)((char*)pfirstposition + dwstride * i));
        temp = temp1;
    }

    D3DXVec3Scale(pcenter, &temp, 1.0f/((FLOAT)numvertices));

    for(i=0; i<numvertices; i++)
    {
        d = D3DXVec3Length(D3DXVec3Subtract(&temp, (D3DXVECTOR3*)((char*)pfirstposition + dwstride * i), pcenter));
        if ( d > *pradius ) *pradius = d;
    }
    return D3D_OK;
}
Exemple #2
0
bool BulletStorm::IsCollided(D3DXVECTOR3 norm, float shipx, float shipy)
{
	// prerequisite: bullets are already fetched from lua script
	// scan all points
	for (std::vector<BulletType>::iterator iter = m_vertexList.begin(); iter != m_vertexList.end(); ++iter)
	{
		float bx, by;
		bx = (*iter).position.x;
		float tmp = D3DXVec3Dot(&norm, &((*iter).position));
		D3DXVECTOR3 tnorm = -tmp * norm;
		D3DXVECTOR3 persP;
		D3DXVec3Add(&persP, &(*iter).position, &tnorm);
		D3DXVECTOR3 yvec;
		D3DXVec3Cross(&yvec, &D3DXVECTOR3(1, 0, 0), &norm);
		if (abs(yvec.y) > abs(yvec.z))
			by = persP.y / yvec.y;
		else by = persP.z / yvec.z;

		float dis = (sqrt((by - shipy) * (by - shipy) + (bx - shipx) * (bx - shipx)));
		if (dis < 7.0f)
		{
			return true;
		}
	}
	return false;
}
Exemple #3
0
static inline Vector* findNearestPointOnLine(Vector* result, Vector* point, Vector* start, Vector* end)
{
    float  mu;
    Vector line;

    D3DXVec3Subtract( &line, end, start );
    mu = D3DXVec3Dot( point, &line ) - D3DXVec3Dot( start, &line );
    if( mu <= 0 )
    {
        *result = *start;
    }
    else
    {
        float lineLength2;
        lineLength2 = D3DXVec3Dot( &line, &line );
        if( mu < lineLength2 )
        {
            mu /= lineLength2;
            D3DXVec3Scale( result, &line, mu );
            D3DXVec3Add( result, result, start );
        }
        else
        {
            *result = *end;
        }
    }
    return result;
}
Exemple #4
0
void GameObject::AddForce(float power, D3DXVECTOR3& dir)
{
	//dir muss vorher schon normiert sein!
	//D3DXVec3Normalize(&dir, &dir);
	dir*= power;
	D3DXVec3Add(&velocity, &velocity, &dir);
}
Exemple #5
0
void GameObject::AddForce(float power, float& x, float& y, float& z)
{
	D3DXVECTOR3 dir(x,y,z);
	D3DXVec3Normalize(&dir, &dir);
	dir*= power;
	D3DXVec3Add(&velocity, &velocity, &dir);
}
Exemple #6
0
/////////////////////////////////////////////////////////////////////////
//  ’e”­ŽË
/////////////////////////////////////////////////////////////////////////
void MoveShell(Shell &shell, Tank(&tank)[myTANKNUM], float speed, float cd)
{

	D3DXVECTOR3 vec;
	if (shell.fireOK == false)
	{
		if (shell.time < cd / FPS * 60.f)
		{
			shell.time += fpTimeDelta;
		}
		else
		{
			shell.fireOK = true;
			shell.time = cd / FPS * 60.f;
		}
	}
	if (shell.fireOK &&key&mLbtn)
	{
		shell.hitmax += 1;
		shell.active = true;
		shell.fireOK = false;
		shell.time = 0.f;
	}
	if (shell.active == false)
	{
		shell.position = tank[myGUN].position;
		shell.prePos = tank[myGUN].position;

		D3DXMatrixRotationYawPitchRoll(&shell.mat, D3DXToRadian(tank[myGUN].direction), tank[myGUN].pitch, tank[myGUN].roll);
		D3DXMatrixMultiply(&shell.mat, &shell.mat, &tank[myGUN].mat);
		D3DXVec3TransformCoord(&vec, &D3DXVECTOR3(0.f, 0.f, 0.5f), &shell.mat);
		D3DXVec3Add(&shell.position, &tank[myGUN].position, &vec);
	}

	else if (shell.active)
	{
		shell.prePos = shell.position;
		D3DXVECTOR3 dist(0.0f, 0.0f, speed / 150.f);
		D3DXVec3TransformCoord(&dist, &dist, &shell.mat);
		D3DXVec3Add(&shell.position, &shell.position, &dist);

		if (D3DXVec3Length(D3DXVec3Subtract(&vec, &shell.position, &tank[myGUN].position)) > 600.f)
		{
			shell.active = false;
		}
	}
}
/**
* CParticleEffect::updateParticle
* @date Modified Jun 01, 2006
*/
bool CParticleEffect::updateParticle(float fTime, CParticleManager::SParticle* pParticle)
{
	CParticleManager::SParticle* p = pParticle;
	p->LivingTime += fTime;

	// Check for dead particles
	if(p->LivingTime >= m_fEffectLength)
	{
		return false;
	}
	else
	{
		D3DXVECTOR3 vVel = p->Velocity * fTime;
		D3DXVECTOR3 vAccel = p->Acceleration * fTime;
		D3DXVec3Add(&p->Position, &p->Position, &vVel);
		D3DXVec3Add(&p->Velocity, &p->Velocity, &vAccel);

		if(m_bBounce && p->Position.y < 0.0f)
		{
			p->Position.y = 0.0f;
			p->Velocity.x /= m_fInvRestitution;
			p->Velocity.y = (-p->Velocity.y) / m_fInvRestitution;
			p->Velocity.z /= m_fInvRestitution;
		}
	}

	// Update attributes
	BYTE cR = GET_RED(p->Color), cG = GET_GREEN(p->Color), cB = GET_BLUE(p->Color), cA = GET_ALPHA(p->Color);
	for(size_t j = 0; j < m_vAttributes.size(); ++j)
	{
		float fScale = m_vAttributes[j]->getValue(p->LivingTime / m_fEffectLength);
		switch(m_vAttributes[j]->getType())
		{
		case CParticleAttribute::ATR_COLORRED: cR = (BYTE)(fScale * 255.0f); break;
		case CParticleAttribute::ATR_COLORGREEN: cG = (BYTE)(fScale * 255.0f); break;
		case CParticleAttribute::ATR_COLORBLUE: cB = (BYTE)(fScale * 255.0f); break;
		case CParticleAttribute::ATR_COLORALPHA: cA = (BYTE)(fScale * 255.0f); break;
		case CParticleAttribute::ATR_SIZE: p->Size = fScale; break;
		case CParticleAttribute::ATR_ROTATION: p->Rotation = degreesToRadians(fScale); break;
		case CParticleAttribute::ATR_ACCELX: p->Acceleration.x = fScale; break;
		case CParticleAttribute::ATR_ACCELY: p->Acceleration.y = fScale; break;
		case CParticleAttribute::ATR_ACCELZ: p->Acceleration.z = fScale; break;
		}
	}
	p->Color = D3DCOLOR_ARGB(cA, cR, cG, cB);
	return true;
}
Exemple #8
0
static inline void calcSphereTriangleDistance(
    Sphere* sphere,
    Vector* normal,
    Vector* v0, Vector* v1, Vector* v2,
    Vector* collPoint,
    float* distance 
)
{
    // project sphere center onto plane of triangle.
    Vector* center = &sphere->center;
    Vector  projPoint;
    float   dist2plane = D3DXVec3Dot( v0, normal ) - D3DXVec3Dot( center, normal );
    D3DXVec3Scale( &projPoint, normal, dist2plane );
    D3DXVec3Add( &projPoint, &projPoint, center );

    // does the projected point lie within the collision triangle?
    Vector* vertices[3];
    vertices[0] = v0, vertices[1] = v1, vertices[2] = v2;
    if( isPointWithinTriangle( &projPoint, vertices, normal ) )
    {
        *distance = fabs( dist2plane );
        *collPoint = projPoint;
        return;
    }
    // projected point lies outside the triangle, so find the nearest
    // point on the triangle boundary...
    else
    {
        float currdist;
        Vector closestPoint, temp;
    
        findNearestPointOnLine( &closestPoint, &projPoint, v0, v1 );
        D3DXVec3Subtract( &temp, center, &closestPoint );
        *distance = D3DXVec3Length(&temp), *collPoint = closestPoint;

        findNearestPointOnLine( &closestPoint, &projPoint, v1, v2 );
        D3DXVec3Subtract(&temp, center, &closestPoint);
        currdist = D3DXVec3Length(&temp);
        if( *distance > currdist ) *distance = currdist, *collPoint = closestPoint;

        findNearestPointOnLine( &closestPoint, &projPoint, v0, v2 );
        D3DXVec3Subtract(&temp, center, &closestPoint);
        currdist = D3DXVec3Length(&temp);
        if( *distance > currdist ) *distance = currdist, *collPoint = closestPoint;
    }
}
Exemple #9
0
void MoveEmyShell(Shell &shell, Enemy(&enemy)[eTANKNUM], float speed, float cd)
{
	D3DXVECTOR3 vec;
	if (shell.fireOK == false)
	{
		if (shell.time < cd / FPS * 60.f)
		{
			shell.time += fpTimeDelta;
		}
		else
		{
			shell.fireOK = true;
		}
	}
	else if (shell.fireOK)
	{
		shell.active = true;
		shell.fireOK = false;
		shell.time = 0.f;
	}
	if (shell.active == false)
	{
		shell.position = enemy[eGUN].position;
		shell.prePos = enemy[eGUN].position;

		D3DXMatrixRotationYawPitchRoll(&shell.mat, D3DXToRadian(enemy[eGUN].direction), enemy[eGUN].pitch, enemy[eGUN].roll);
		D3DXMatrixMultiply(&shell.mat, &shell.mat, &enemy[eGUN].mat);
	}
	else if (shell.active)
	{
		shell.prePos = shell.position;
		D3DXVECTOR3 dist;
		dist = { 0.0f, 0.0f, speed / 150.f };
		D3DXVec3TransformCoord(&dist, &dist, &shell.mat);
		D3DXVec3Add(&shell.position, &shell.position, &dist);

		if (D3DXVec3Length(D3DXVec3Subtract(&vec, &shell.position, &enemy[eGUN].position)) > 600.f)
		{
			shell.active = false;
		}
	}
}
/**
* CAIStatePathFollow::followPath
* @dateModified May 4, 2006
*/
void CAIStatePathFollow::followPath(CAIEntity* poAIEntity, CCharacter* poCharacter, float fSpeed)
{
	// check for a valid path, one with nodes in it
	if (poAIEntity->m_loPath.empty())
	{
		// this is bad that there are no nodes to go to
		// remove our influence
		poCharacter->setVelocity(D3DXVECTOR3(0.0f, 0.0f, 0.0f));
		// but not for now
		((CEnemy*)(poCharacter))->setAIState(NULL);
		return;
	}

	// if it isn't moving to a node yet, start the movement
	if (memcmp(&m_vVelocity, &poCharacter->getVelocity(), sizeof(D3DXVECTOR3)) == 0)
	{
		// get vector from current position to next node
		D3DXVECTOR3 vNextNode;
		D3DXVec3Subtract(&vNextNode, &PATH_BACK->getPosition(), &poCharacter->getBV().centerPt);
		D3DXVec3Normalize(NULL, &vNextNode, &vNextNode);

		// scale by speed
		poCharacter->setOrientation(vNextNode);
		D3DXVec3Scale(&vNextNode, &vNextNode, fSpeed);

		// set velocity
		poCharacter->setVelocity(vNextNode);
	}
	// check to see if we should target to the next node
	else if (computeDistanceSquared(poCharacter->getBV().centerPt, PATH_BACK->getPosition()) < (PATH_BACK->m_fRadius * PATH_BACK->m_fRadius))
	{
		// we need to go to the next node
		poAIEntity->m_loPath.pop_back();

		// are we now at the goal
		if (poAIEntity->m_loPath.empty())
		{
			// we are at the goal
			// remove influence
			poCharacter->setVelocity(D3DXVECTOR3(0.0f, 0.0f, 0.0f));
			// but not for now
			((CEnemy*)(poCharacter))->setAIState(NULL);
			return;
		}

		// loose movement around the nodes
		//////////////////////////////////

		D3DXVECTOR3 vNextNode, vGoalNode;
		// get vector from entity to next node
		D3DXVec3Subtract(&vNextNode, &PATH_BACK->getPosition(), &poCharacter->getBV().centerPt);
		// get vector from entity to goal node
		D3DXVec3Subtract(&vGoalNode, &PATH_FRONT->getPosition(), &poCharacter->getBV().centerPt);

		D3DXVec3Normalize(NULL, &vGoalNode, &vGoalNode);

		// project vector from entity to next node onto vector from entity to goal node
		float fProjection = D3DXVec3Dot(&vNextNode, &vGoalNode);
		// store the position there now
		vNextNode = PATH_BACK->getPosition();
		// scale vector from entity to goal node by the projection value
		D3DXVec3Scale(&vGoalNode, &vGoalNode, fProjection);
		// add vector from entity to goal node to get point closest to next node
		D3DXVec3Add(&vGoalNode, &poCharacter->getBV().centerPt, &vGoalNode);
		// get the vector from the next node to the closest point
		D3DXVec3Subtract(&vGoalNode, &vGoalNode, &vNextNode);

		// if the magnitude of the vector between the next node and the closest point
		// is greater than the radius of the node, do some randomization
		if (D3DXVec3Length(&vGoalNode) > PATH_BACK->m_fRadius)
		{
			D3DXVec3Normalize(NULL, &vGoalNode, &vGoalNode);
			
			int nRandInt = (int)(PATH_BACK->m_fRadius * 10);
			float fRandFloat = (float)(rand()%nRandInt) / 10.0f;
			D3DXVec3Scale(&vGoalNode, &vGoalNode, fRandFloat);
		}

		// add vector from next node to point within radius
		D3DXVec3Add(&vNextNode, &vNextNode, &vGoalNode);
		// get vector from entity to point within radius to be new velocity
		D3DXVec3Subtract(&vGoalNode, &vNextNode, &poCharacter->getBV().centerPt);

		// normalize and scale by speed
		poCharacter->setOrientation(vGoalNode);
		D3DXVec3Normalize(NULL, &vGoalNode, &vGoalNode);
		D3DXVec3Scale(&vGoalNode, &vGoalNode, fSpeed);

		vGoalNode.y = 0.0f;

		// set velocity
		poCharacter->setVelocity(vGoalNode);
		return;
	}

	// have we been following for too long that the goal may have moved
	if (poAIEntity->getCurrentStateTime() > 0.25f)
	{
		// remove our influence
		poCharacter->setVelocity(D3DXVECTOR3(0.0f, 0.0f, 0.0f));
		// should probably path again
		((CEnemy*)(poCharacter))->setAIState(CAIStatePathPlan::getInstancePtr());
	}
}
void CActorInstance::OnRender()
{
	D3DMATERIAL8 kMtrl;
	STATEMANAGER.GetMaterial(&kMtrl);

	kMtrl.Diffuse=D3DXCOLOR(m_dwMtrlColor);	
	STATEMANAGER.SetMaterial(&kMtrl);

	// 현재는 이렇게.. 최종적인 형태는 Diffuse와 Blend의 분리로..
	// 아니면 이런 형태로 가되 Texture & State Sorting 지원으로.. - [levites]
	STATEMANAGER.SaveRenderState(D3DRS_CULLMODE, D3DCULL_NONE);	

	switch(m_iRenderMode)
	{
		case RENDER_MODE_NORMAL:
			BeginDiffuseRender();
				RenderWithOneTexture();
			EndDiffuseRender();
			BeginOpacityRender();
				BlendRenderWithOneTexture();
			EndOpacityRender();
			break;
		case RENDER_MODE_BLEND:
			if (m_fAlphaValue == 1.0f)
			{
				BeginDiffuseRender();
					RenderWithOneTexture();
				EndDiffuseRender();
				BeginOpacityRender();
					BlendRenderWithOneTexture();
				EndOpacityRender();
			}
			else if (m_fAlphaValue > 0.0f)
			{
				BeginBlendRender();
					RenderWithOneTexture();
					BlendRenderWithOneTexture();
				EndBlendRender();
			}
			break;
		case RENDER_MODE_ADD:
			BeginAddRender();
				RenderWithOneTexture();
				BlendRenderWithOneTexture();
			EndAddRender();
			break;
		case RENDER_MODE_MODULATE:
			BeginModulateRender();
				RenderWithOneTexture();
				BlendRenderWithOneTexture();
			EndModulateRender();
			break;
	}

	STATEMANAGER.RestoreRenderState(D3DRS_CULLMODE);

	kMtrl.Diffuse=D3DXCOLOR(0xffffffff);
	STATEMANAGER.SetMaterial(&kMtrl);

	if (ms_isDirLine)
	{
		D3DXVECTOR3 kD3DVt3Cur(m_x, m_y, m_z);

		D3DXVECTOR3 kD3DVt3LookDir(0.0f, -1.0f, 0.0f);
		D3DXMATRIX kD3DMatLook;
		D3DXMatrixRotationZ(&kD3DMatLook, D3DXToRadian(GetRotation()));
		D3DXVec3TransformCoord(&kD3DVt3LookDir, &kD3DVt3LookDir, &kD3DMatLook);
		D3DXVec3Scale(&kD3DVt3LookDir, &kD3DVt3LookDir, 200.0f);
		D3DXVec3Add(&kD3DVt3LookDir, &kD3DVt3LookDir, &kD3DVt3Cur);

		D3DXVECTOR3 kD3DVt3AdvDir(0.0f, -1.0f, 0.0f);
		D3DXMATRIX kD3DMatAdv;
		D3DXMatrixRotationZ(&kD3DMatAdv, D3DXToRadian(GetAdvancingRotation()));
		D3DXVec3TransformCoord(&kD3DVt3AdvDir, &kD3DVt3AdvDir, &kD3DMatAdv);
		D3DXVec3Scale(&kD3DVt3AdvDir, &kD3DVt3AdvDir, 200.0f);
		D3DXVec3Add(&kD3DVt3AdvDir, &kD3DVt3AdvDir, &kD3DVt3Cur);

		static CScreen s_kScreen;

		STATEMANAGER.SaveTextureStageState(0, D3DTSS_COLORARG1,	D3DTA_DIFFUSE);
		STATEMANAGER.SaveTextureStageState(0, D3DTSS_COLOROP,	D3DTOP_SELECTARG1);
		STATEMANAGER.SaveTextureStageState(0, D3DTSS_ALPHAOP,	D3DTOP_DISABLE);
		STATEMANAGER.SaveRenderState(D3DRS_ZENABLE, FALSE);
		STATEMANAGER.SetRenderState(D3DRS_LIGHTING, FALSE);

		s_kScreen.SetDiffuseColor(1.0f, 1.0f, 0.0f);
		s_kScreen.RenderLine3d(kD3DVt3Cur.x, kD3DVt3Cur.y, kD3DVt3Cur.z, kD3DVt3AdvDir.x, kD3DVt3AdvDir.y, kD3DVt3AdvDir.z);

		s_kScreen.SetDiffuseColor(0.0f, 1.0f, 1.0f);
		s_kScreen.RenderLine3d(kD3DVt3Cur.x, kD3DVt3Cur.y, kD3DVt3Cur.z, kD3DVt3LookDir.x, kD3DVt3LookDir.y, kD3DVt3LookDir.z);

		STATEMANAGER.SetRenderState(D3DRS_LIGHTING, TRUE);
		STATEMANAGER.RestoreRenderState(D3DRS_ZENABLE);

		STATEMANAGER.RestoreTextureStageState(0, D3DTSS_COLORARG1);
		STATEMANAGER.RestoreTextureStageState(0, D3DTSS_COLOROP);
		STATEMANAGER.RestoreTextureStageState(0, D3DTSS_ALPHAOP);
		STATEMANAGER.RestoreVertexShader();
	}
}