求用C语言模拟简单台球运动的源代码,不需要图形化界面

要求如题,只要能输出过程和结果就行。C++的话也可以。模拟过程不需要考虑摩擦力和三球以上同时碰撞的问题。请在3月20号之前给出能够运行的代码,或者将其发送到sakuraj... 要求如题,只要能输出过程和结果就行。
C++的话也可以。
模拟过程不需要考虑摩擦力和三球以上同时碰撞的问题。
请在3月20号之前给出能够运行的代码,或者将其发送到sakuraja1@foxmail.com
谢谢。
......额,就是一开始有10个球随机分布,然后其中一个开始运动,通过各种碰撞,最后入袋的问题

只要记录下入袋的球和剩下球的个数就好了...

谢谢...
展开
 我来答
471007827
2009-03-18 · 超过12用户采纳过TA的回答
知道答主
回答量:84
采纳率:0%
帮助的人:0
展开全部
这源代码应该有个桌面类(Table),球类(Sphere),游戏类等等。我用C++
#pragma once (Table.h)
#endif // _MSC_VER > 1000
#include "Base.h"
#define MESH_D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)
class CTable:public CBase
{
public:
DWORD Render();
CTable(LPDIRECT3DDEVICE8 pD3DDevice,LPSTR pFilename);
virtual ~CTable();
LPD3DXMESH GetMeshTablePointer();
private:
void TransformTable();
LPDIRECT3DDEVICE8 m_pD3DDevice;
DWORD m_dwNumMaterials;
LPD3DXMESH m_pMeshTable;
D3DMATERIAL8 *m_pMeshTableMaterials;
LPDIRECT3DTEXTURE8 *m_pMeshTableTextures;
};#endif

#include "Table.h" (Table.cpp)
CTable::CTable(LPDIRECT3DDEVICE8 pD3DDevice,LPSTR pFilename)
{
LPD3DXBUFFER pMaterialsBuffer=NULL;
LPD3DXMESH pMeshTable=NULL;

m_pD3DDevice=pD3DDevice;

if(FAILED(D3DXLoadMeshFromX(pFilename,D3DXMESH_MANAGED,m_pD3DDevice,NULL,
&pMaterialsBuffer,&m_dwNumMaterials,&pMeshTable)))
{
m_pMeshTable=NULL;
m_pMeshTableMaterials=NULL;
m_pMeshTableTextures=NULL;

LogError("<li>Table Mesh '%s' failed to load",pFilename);
return;
}
D3DXMATERIAL *matMaterials=(D3DXMATERIAL*)pMaterialsBuffer->GetBufferPointer();
//Create two arrays. One to hold the materials and one to hold the textures
m_pMeshTableMaterials=new D3DMATERIAL8[m_dwNumMaterials];
m_pMeshTableTextures=new LPDIRECT3DTEXTURE8[m_dwNumMaterials];
for(DWORD i=0;i<m_dwNumMaterials;i++)
{
//Copy the material
m_pMeshTableMaterials[i]=matMaterials[i].MatD3D;

//Set the ambient color for the material(D3DX does not do this)
m_pMeshTableMaterials[i].Ambient=m_pMeshTableMaterials[i].Diffuse;
D3DCOLORVALUE rgbaSpecular={0.0f,0.0f,0.0f,0.0f};
m_pMeshTableMaterials[i].Specular=rgbaSpecular;
m_pMeshTableMaterials[i].Power=50.0f;
//Create the texture
char buffer[255];
sprintf(buffer,"textures/%s",matMaterials[i].pTextureFilename);
if(FAILED(D3DXCreateTextureFromFile(m_pD3DDevice,
buffer, &m_pMeshTableTextures[i])))
{
m_pMeshTableTextures[i]=NULL;
}
}
//finished with the material buffer,so release it
SafeRelease(pMaterialsBuffer);
//Make sure that the normals are setup for mesh
pMeshTable->CloneMeshFVF(D3DXMESH_MANAGED,MESH_D3DFVF_CUSTOMVERTEX,m_pD3DDevice,&m_pMeshTable);
SafeRelease(pMeshTable);
// D3DXComputeNormals(m_pMesh);
LogInfo("<li>Mesh '%s' loaded OK",pFilename);
}
CTable::~CTable()
{
SafeDelete(m_pMeshTableMaterials);

if(m_pMeshTableTextures != NULL)
{
for(DWORD i=0;i<m_dwNumMaterials;i++)
{
if(m_pMeshTableTextures[i])
SafeRelease(m_pMeshTableTextures[i]);
}
}
SafeDelete(m_pMeshTableTextures);
SafeRelease(m_pMeshTable);
LogInfo("<li>Table Mesh destroyed OK");
}
DWORD CTable::Render()
{
TransformTable();
if(m_pMeshTable!=NULL)
{
for(DWORD i=0;i<m_dwNumMaterials;i++)
{
m_pD3DDevice->SetMaterial(&m_pMeshTableMaterials[i]);
m_pD3DDevice->SetTexture(0,m_pMeshTableTextures[i]);

m_pMeshTable->DrawSubset(i);
}

return m_pMeshTable->GetNumFaces();
}
else
return 0;
}
LPD3DXMESH CTable::GetMeshTablePointer()
{
return m_pMeshTable;
}
void CTable::TransformTable()
{
D3DXMATRIX matWorld;
D3DXMatrixTranslation(&matWorld,0,0,0);
m_pD3DDevice->SetTransform(D3DTS_WORLD,&matWorld);
}
(Sphere.h)
#if !defined (AFX_SPHERE_H__FC705F3B_568E_4973_B608_B8F7700D9ECE__INCLUDED_)
#define AFX_SPHERE_H__FC705F3B_568E_4973_B608_B8F7700D9ECE__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "Base.h"

#define SPHERE_D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)

class CSphere:public CBase
{
private:
struct SPHERE_CUSTOMVERTEX
{
float x,y,z; //Position of vertex in 3D space
float nx,ny,nz; //Lighting Normal
float tu,tv; //Texture coordinates
};

struct SPHERE_STATE
{
D3DXVECTOR3 sVector; //Position of Centigram in 3D space
D3DXVECTOR3 vVector; //Direction of Velocity in 3D space
float v; //Speed of Sphere
};
SPHERE_STATE *m_pSphereState;

D3DXVECTOR3 m_vecSavePosition; //Save sphere position for collision bar
D3DXVECTOR3 m_vecSavePosition2; //Save sphere position for collision sphere
public:
BOOL SetMaterial(D3DCOLORVALUE rgbaDiffuse,D3DCOLORVALUE rgbaAmbient,
D3DCOLORVALUE rgbaSpecular,D3DCOLORVALUE rgbaEmissive,float rPower);
BOOL SetTexture(const char* szTextureFilePath);
DWORD Render();
CSphere(LPDIRECT3DDEVICE8 pD3DDevice,int iRings=20,int iSegments=20);
void MoveSphere();
void MoveSphereForUser(float x,float z);
virtual ~CSphere();
inline void SetSpherePosition(float x,float y,float z)
{
m_pSphereState->sVector.x=x;
m_pSphereState->sVector.y=y;
m_pSphereState->sVector.z=z;
};
inline void GetSpherePosition(D3DXVECTOR3 &vecSpherePos)
{
vecSpherePos=m_pSphereState->sVector;
};
inline void GetSavedSpherePosition(D3DXVECTOR3 &vecSavedSpherePos)
{
vecSavedSpherePos=m_vecSavePosition;
};
inline void GetSavedSpherePosition2(D3DXVECTOR3 &vecSavedSpherePos)
{
vecSavedSpherePos=m_vecSavePosition2;
};
inline void SaveSpherePosition()
{
m_vecSavePosition=m_pSphereState->sVector;
};
inline void SaveSpherePosition2()
{
m_vecSavePosition2=m_pSphereState->sVector;
};
inline void ContradictoryZv()
{
m_pSphereState->vVector.z=-m_pSphereState->vVector.z;
};
inline void ContradictoryXv()
{
m_pSphereState->vVector.x=-m_pSphereState->vVector.x;
};
void MirrorVAoubtAxis(D3DXVECTOR3 &n);
inline void ReduceSphereVelocity(float percent)
{
m_pSphereState->v=m_pSphereState->v*percent;
};
inline float CheckSphereEnergy()
{
return m_pSphereState->v;
};
inline void SetSphereVelocityDir(const D3DXVECTOR3 &vDir)
{
m_pSphereState->vVector=vDir;
};
inline void SetSphereVelocity(const float &velocity)
{
m_pSphereState->v=velocity;
};
inline void GetSphereVelocityDir(D3DXVECTOR3 &vDir)
{
vDir=m_pSphereState->vVector;
};
inline float GetSphereVelocity()
{
return m_pSphereState->v;
};
inline void SetSphereStateToFalse()
{
m_bSphereInUse=FALSE;
};
inline void SetSphereStateToTrue()
{
m_bSphereInUse=TRUE;
};
inline BOOL GetSphereState()
{
return m_bSphereInUse;
};
void SetSphereVelocityAt_Y_NegativeAxis();
inline float GetSpherePosAt_Y_Axis()
{
return m_pSphereState->sVector.y;
};
private:
BOOL CreateIndexBuffer();
BOOL UpdateVertices();
BOOL CreateVertexBuffer();
void TransformSphere();
void TransformSphereForUser();
void UpdateSpherePosition();
void FrictionReduseVelocity();
LPDIRECT3DDEVICE8 m_pD3DDevice;
LPDIRECT3DVERTEXBUFFER8 m_pVertexBuffer;
LPDIRECT3DTEXTURE8 m_pTexture;
D3DMATERIAL8 m_matMaterial;
LPDIRECT3DINDEXBUFFER8 m_pIndexBuffer;
int m_iRings;
int m_iSegments;
float m_fTotalDis;
D3DXVECTOR3 m_vecSphereRotationAxis;
BOOL m_bSphereInUse;
DWORD m_dwNumOfVertices;
DWORD m_dwNumOfIndices;
DWORD m_dwNumOfPolygons;
};#endif
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
695021304
2009-03-18
知道答主
回答量:3
采纳率:0%
帮助的人:0
展开全部
I NO KNOW
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式