求运动小球制作思路,可以反弹的那种
我想做一个小程序实现以下功能:
在一个矩形范围内随机生成一个小球,并且让小球在随机向一个方向运行,碰到边缘的时候可以按物理规律反弹
可是思路不是很清晰,希望可以得到高手的指教,谢谢!
struct Ball{ private: struct Speed{ double xSpeed; double ySpeed; }; struct Position{ double x; double y; }; Speed speed; Position position; public: Ball() { speed.xSpeed=rand(); speed.ySpeed=rand(); position.x=rand(); position.y=rand(); } void bumpLeft() { speed.xSpeed=-speed.xSpeed; } void bumpRight() { speed.xSpeed=-speed.xSpeed; } void bumpUp() { speed.ySpeed=-speed.ySpeed; } void bumpDown() { speed.ySpeed=-speed.ySpeed; } void move() { position.x+=speed.xSpeed; position.y+=speed.ySpeed; } };