掉落的球体

来源:互联网 发布:淘宝的评论在哪里 编辑:程序博客网 时间:2024/04/27 21:02
#ifndef CBall_H_#define CBall_H_#pragma once#include "element.h"//枚举掉落小球的材质:金属球,篮球,乒乓球enum BallType{ BT_METALBALL ,  BT_BASKETBALL ,  BT_PINGBANGBALL  };class CBall :public CElement{public:BallType mBallType;static int const m_Grivate = 100;float m_fStartVelocity;float m_fEndVelocity;float m_fCoefficient;//弹性碰撞系数public:CBall(BallType type);~CBall(void);void Logic(float fElapsedTime);};#endif

#include "Utility.h"#include "Ball.h"CBall::CBall(BallType type){mBallType = type;//将小球的类型记录下来m_fStartVelocity = 0.0f;m_fEndVelocity = 0.0f;}CBall::~CBall(void){}void CBall::Logic(float fElapsedTime){//---------------根据物理公式来更新小球的位置-----------------------------//         Vt =        V0+    a *    tm_fEndVelocity = m_fStartVelocity + m_Grivate * fElapsedTime;//St  = S0+ V0 *  tm_vPos.y = m_vPos.y   + m_fEndVelocity * fElapsedTime;//m_vPos.x += 30.0f *fElapsedTime;m_fStartVelocity = m_fEndVelocity;//上次计算得到的末速度 = 下次计算需要用到的初速度//--------------------更新矩形区域位置------------------------m_ScreenRect.left = (LONG)m_vPos.x;m_ScreenRect.top = (LONG)m_vPos.y;m_ScreenRect.right = m_ScreenRect.left+m_iWidth;m_ScreenRect.bottom = m_ScreenRect.top+m_iHeight;//判定是否碰撞地面,这个判断条件要注意://避免数据精度的损失导致球体不间断在地面弹动if(  m_vPos.y > float(m_ClientRect.bottom - m_iHeight)  ){switch(mBallType)//解析小球的类型,进行不同的处理{case BT_METALBALL: m_fCoefficient = 0.2f;//规定金属球的弹性碰撞系数break;case BT_BASKETBALL: m_fCoefficient = 0.5f;//规定篮球的弹性碰撞系数break;case BT_PINGBANGBALL: m_fCoefficient = 0.8f;//规定乒乓球的弹性碰撞系数break;}m_vPos.y = float(m_ClientRect.bottom - m_iHeight);m_fStartVelocity = m_fEndVelocity * m_fCoefficient * -1.0f ;}}

0 0