flash中物体运动基础之八---------反弹

接着第七部分碰撞检测,检测到碰撞后球体要反弹,根据障碍物的角度,球体的反弹角度也不同。下面是各个角度间的关系图:

修改前面的代码如下:

_count = 0;
			var sumX:Number = 0;
			var sumY:Number = 0;
			_ball.vy += GRAVITY;
			for (var i:int = 0; i < precision;i++ )
			{
				var x:Number = _ball.x+radius * Math.cos(360*i/precision);
				var y:Number = _ball.y+radius * Math.sin(360*i/precision);
				
				if (_roadBlock.hitTestPoint(x,y,true))
				{
					sumX += x;
					sumY += y;
					_count++;
                        textField.text = "" + _count; } } if (_count>0) { //球体的入射角 var ballDir:Number = Math.atan2(_ball.vy, _ball.vx) * 180 / Math.PI; x = sumX / _count; y = sumY / _count; var dx:Number = x - _ball.x; var dy:Number = y - _ball.y; //垂直于障碍物的方向相对于水平面的角度 var tempDir:Number = Math.atan2(dy, dx) * 180 / Math.PI; //障碍物的角度 var groundDir:Number = tempDir - 90; //球体的反弹的角度 var bounceDir:Number = 180 - ballDir - 2 * groundDir; var speed:Number = Math.sqrt(_ball.vx * _ball.vx + _ball.vy * _ball.vy); _ball.vx = speed * Math.cos(bounceDir * Math.PI / 180); _ball.vy = speed * Math.sin(bounceDir * Math.PI / 180); lineMC.graphics.clear(); lineMC.graphics.lineStyle(1); lineMC.graphics.moveTo(x, y); lineMC.graphics.lineTo(_ball.x, _ball.y); } _ball.y += _ball.vy; _ball.x += _ball.vx;

效果:

 

posted @ 2010-07-31 22:16  ywxgod  阅读(679)  评论(0编辑  收藏  举报