如何使球弹跳

发布于 2025-01-30 19:50:41 字数 199 浏览 4 评论 0 原文

我有一个游戏,您可以在平台上弹跳球,您必须摧毁所有盒子,但是我的反弹球的代码不像我想要的那样工作。

它的作用就像真正的球弹跳一样,但我希望球像乒乓球一样弹跳,直到它碰到墙壁或platfotm,但我的球在现实生活曲线中才会掉下来。

我的曲线事物就像当您在空中踢球并进入曲线时,我希望它在平坦的线上沿墙壁无限,并随机方向。

有人有任何代码想法吗?

I have a game where u bounce a ball with platform and u have to destroy all boxes but my code for bounceing ball its not working like I want.

It works like a real ball bounce but I want the ball to bounce like a ball in pong where ball don't fall down until it touches wall or platfotm but my ball goes in real life curve.

What I thing with curve is like when u kick a ball in the air and goes in curve but I want it to go unlimitly in flat line to the wall andbounce in a random direction.

Someone have any code ideas?

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

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

发布评论

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

评论(2

时光是把杀猪刀 2025-02-06 19:50:41

制作像下面的物理材料,并将其放置在任何弹跳的表面上。它足以让您的球拥有一个 strigbody 带有0。

“在此处输入图像描述”


也请取消选中的重力以获得以下结果:

​“ https://i.sstatic.net/4a5wi.gif” rel =“ nofollow noreferrer”>


基本力量脚本:

将刚性体添加到球中后,您可以将以下脚本用于初始力量。显然,您需要更高级的算法才能进一步开发。

public Vector3 force = new Vector3(80, 40);
void Start() => GetComponent<Rigidbody>().AddForce(force);

Make a physics material like the one below and place it on any surface that bounces. It is enough for your ball to have a Rigidbody with drag 0.

enter image description here


Also uncheck gravity to get the following result:

enter image description here

enter image description here


Basic Force Script:

After adding Rigidbody to the ball, you can use the following script for the initial force. Obviously you need more advanced algorithms for further development.

public Vector3 force = new Vector3(80, 40);
void Start() => GetComponent<Rigidbody>().AddForce(force);
§普罗旺斯的薰衣草 2025-02-06 19:50:41

乒乓球风格的物理学比您想象的要简单。
球没有对它们应用重力,因此您要做的就是在球上施加恒定的速度。
当它与物体碰撞时,您会在其相撞的轴上翻转速度。

例如:
Ball以60px/sec的速度向上行驶,并以120px/sec的水平行驶,以60fps的速度向上行驶。
您将(60/60)= 1个像素垂直添加每个帧,(120/60)= 2水平。
它撞到墙壁,因此您将水平速度翻转为-120px/sec。
现在它仍在上升,但水平方向相反。
如果碰到天花板,则可以将垂直速度组件翻转。

如果您想朝随机方向发展:

  • 计算球速度的大小 sqrt(vx^2+vy^2)
  • 找到墙壁面向的角度
  • 在180度内挑选一个随机角度该角度
  • 使用三角学来获取该角度的X/y组分,并乘以大小。

不要在每个帧中减去/添加恒定值,因为这样您就可以应用重力,这不是您想要的。

Pong/breakout style physics is simpler than you might think.
The balls have no gravity applied to them, so all you need to do is apply a constant velocity to the ball.
When it collides with an object, you would flip the velocity on the axis it collided.

For example:
ball is travelling up at 60px/sec, and horizontally at 120px/sec, at 60fps.
You add (60/60)=1 pixels vertically every frame, and (120/60)=2 horizontally.
It hits a wall, so you flip the horizontal velocity to -120px/sec.
Now it's still going up, but in the opposite direction horizontally.
If it hits a ceiling, same thing but flip the vertical velocity component.

If you'd rather it go in a random direction:

  • calculate the magnitude of the ball's velocity sqrt(vx^2+vy^2)
  • find the angle the wall is facing
  • pick a random angle within 180 degrees of that angle
  • use trigonometry to get the x/y components of that angle, and multiply by the magnitude.

Don't subtract/add a constant value to the velocity every frame, because then you'd be applying gravity, which isn't what you're looking for.

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