根据速度改变碰撞时球的方向

发布于 2024-08-17 03:20:19 字数 353 浏览 5 评论 0原文

我正在创建一个乒乓球游戏。我想创造一种根据对球拍的影响来控制球方向的能力。如果球在 vy = 4 处落下; vx=4;

并且桨在 vx = -5 处向左移动;我希望球根据桨移动的速度稍微改变其路线。它可能会降低碰撞时球的 vx 速度,从而使球在向上移动时移动得更直(靠近 Y 轴)。但在我开始疯狂的试错之旅之前,我想知道是否有人知道答案或可能有任何消息来源。

我认为这样做的解决方案可能是测量桨的速度。我的问题是桨是由鼠标控制的并且没有一定的速度。我想弄清楚如何测量鼠标在 x 轴上移动的速度。

我可能会创建一个每隔几秒触发一次的计时器,以确定鼠标在哪里以及它在哪里。计算出差异,这将是速度

如果有人有任何答案,那就太好了。谢谢

I am creating a ping pong game. And I want to create the ability to control the direction of the ball based on the impact on the paddle. If the ball is coming down at vy = 4; vx = 4;

and the paddle is moving to the left at vx = -5; I want the ball to slightly change its course depending on how fast the paddle is moving. It would probably reduce the balls vx speed on collision, therefore making the ball move more straight (close to the Y axis) when it is moving back up. But before I go on a crazy trial and error journey, I was wanting to know if anyone knew the answer or probably have any sources.

I figure the solution for probably doing this would be to measure how fast the paddle is going. My problem is the paddle is controlled by the mouse and has no certain speed. I am trying to figure out how I can measure the speed of my mouse traveling on the x axis.

I am probably going to create a timer that fires every few seconds to determine where the mouse was and where it is at. figure the difference and that will be the speed

If anyone has any answers, that would be great. thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜清冷一曲。 2024-08-24 03:20:19

测量桨速度的最简单方法是保留前一帧中桨位置的缓存,因此通过 x - xPrev,您可以获得该帧中桨运动的增量,或相对速度。

从该速度,您可以将其作为修改器(可能按比例缩小)添加到球反射矢量的 x 速度中。

现在这听起来像是一个简单的游戏,帧率应该不是问题。然而,作为参考,如果您想跟踪速度,可以通过访问前一帧的总时间来进行简单的物理计算。允许在时间上保持一致的行为,与模拟的帧速率无关。

The easiest way to measure the speed of the paddle is to keep a cache of the position of the paddle in the previous frame, thus by having x - xPrev you have the delta of the paddle movement for this frame, or the relative speed.

From that speed you can add it as a modifier(scaled down probably), into the x velocity of the ball's reflected vector.

Now this sounds like a simple game, and framerate should not be a problem. For reference however, if you want to keep track of the velocity, simple Physics calculations could be made by having access to the total time of the previous frame. allowing for consistent behavior time-wise, independent of the framerate of the simulation.

水晶透心 2024-08-24 03:20:19

K,我想出了解决办法。不过,仍然需要进行碰撞检测。不是最伟大的。我的球有时会卡在桨中。我正在使用 Flash 对象命中测试。但无论如何。这是我用来让它达到我想要的效果的代码。似乎它有时支持它应该走​​的方向,有时又不支持。

它有效,但问题是我把速度调为负,所以有时方向会偏离。因为球移动的方向是由 -1 或 1 决定的。如果我将速度调为负值。它可能导致一个数字与它的预期相反。

下面是在桨类中,

public var cspeed:Number;
        private var timer:Timer;
        public var vx:Number = 0;
        public var vy:Number = 0;
        private var prevx:Number = 0;
        private var prevy:Number = 0;

        public function Paddle():void
        {
            timer = new Timer(60);
            timer.start();
            timer.addEventListener(TimerEvent.TIMER,checkSpeed);
        }


        private function checkSpeed(e:TimerEvent):void
        {
            if(prevx == 0)
            {
                prevx = x;
            }
            else
            {
                cspeed = x - prevx;
                prevx = 0;
            }
        }

我检查球类中的碰撞情况。因为它是击中一切的那个。 代码

下面是我的球类中的 。在检查墙壁类中,它检查是否撞到墙壁或桨。我将我的球拍的参考传递给了球类。

private function checkWalls():void
        {

            if(y > sRef.stageHeight || y < 0)
            {
                yDir = yDir * -1;
            }
            else if ( x > sRef.stageWidth || x < 0)
            {
                xDir = xDir * -1;
            }
             //trace(dist);
            /*var dx:Number = paddle.x - x;
            var dy:Number = paddle.y - y;
            var dist:Number = Math.sqrt(dx * dx + dy * dy);*/


             if(hitTestObject(paddle))
             {
                 xspeed = (paddle.cspeed/100) + xspeed;
                 yDir = yDir * -1;
             }
            //trace(paddle.cspeed);
        }

        private function moveBall():void
        {

            x += xspeed * xDir;
            y += yspeed * yDir;
        }

我仍然需要经常眨眼。但如果我努力的话。我想我可以让它做我想做的事。

K, I figure the solution out. Still need to work on my collision detection though. isnt the greatest. my ball sometimes get stuck in the paddle. I am using the flash object hit test. but anyhow. this is the code I used to get it to somewhat do I want it to do. It seems as though it supports the direction it suppose to go sometimes and sometimes it doesnt.

It works but the problem is I knock my speed in the negative so sometimes the directions are off. since the direction the ball moves is determined by a -1 or a 1. if I knock my speed in the negative. it can cause a number to be the opposite of what its suppose to be.

Below is in the paddle class

public var cspeed:Number;
        private var timer:Timer;
        public var vx:Number = 0;
        public var vy:Number = 0;
        private var prevx:Number = 0;
        private var prevy:Number = 0;

        public function Paddle():void
        {
            timer = new Timer(60);
            timer.start();
            timer.addEventListener(TimerEvent.TIMER,checkSpeed);
        }


        private function checkSpeed(e:TimerEvent):void
        {
            if(prevx == 0)
            {
                prevx = x;
            }
            else
            {
                cspeed = x - prevx;
                prevx = 0;
            }
        }

I check for collision within my ball class. since it is the one hitting everything. below is the code

the following is in my ball class. in the check walls class it checks to see if it hits walls or the paddle. I passed reference of my paddle to the ball class.

private function checkWalls():void
        {

            if(y > sRef.stageHeight || y < 0)
            {
                yDir = yDir * -1;
            }
            else if ( x > sRef.stageWidth || x < 0)
            {
                xDir = xDir * -1;
            }
             //trace(dist);
            /*var dx:Number = paddle.x - x;
            var dy:Number = paddle.y - y;
            var dist:Number = Math.sqrt(dx * dx + dy * dy);*/


             if(hitTestObject(paddle))
             {
                 xspeed = (paddle.cspeed/100) + xspeed;
                 yDir = yDir * -1;
             }
            //trace(paddle.cspeed);
        }

        private function moveBall():void
        {

            x += xspeed * xDir;
            y += yspeed * yDir;
        }

I still needs to be twinked ALOT. but if I work on it. i think i can get it to do what i want.

萌︼了一个春 2024-08-24 03:20:19

我能够解决这个问题。我在互联网上找到了执行以下操作的方法,并且物理看起来更好一些。球仍然时不时地卡在球拍内

将以下内容更改

xspeed = xspeed + (paddle.cspeed/10);

xspeed = xspeed + ( paddle.cspeed * .4 );

I was able to some what resolve the issue. I found on the internet to do the following and the physics look some what better. ball still gets stuck within the paddle from time to time

Changed the following from

xspeed = xspeed + (paddle.cspeed/10);

to

xspeed = xspeed + ( paddle.cspeed * .4 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文