模拟类似于灰尘颗粒的运动

发布于 2024-12-18 12:35:14 字数 589 浏览 1 评论 0原文

我尝试过使用 css 和 animate 进行 setInterval 循环。两种运动方式都由 oldpos1 -> 的微小运动组成。 newpos1 没有随机曲线移动,但是 jQuery animate 发生了缓动,但仅在随机生成的 1-3 个像素之间发生,这不是我想要的 。 问题是否出在 setInterval 的时钟上,其中只有线性时间单位流动?

我应该从哪里开始,让下面的图像存在于 jQuery 中?

我想要做什么:

  1. 躲避行为:

    A、B - 粒子

    AB1 - 普通闪避区域,仅限一定量

在此处输入图像描述

2 运动:

Av、Bv - 随机圆周运动

Aacc、Bacc - 发生微小随机加速度的位置(在标记为更浓缩的虚线的图像上)

在此处输入图像描述

I've tried a setInterval loop with css and animate. Both ways of movement consists of tiny movement from oldpos1 -> newpos1 with no random curve movement, easing however occured with jQuery animate but only between randomly generated 1-3 pixels, which is not what I want
.
Does the problem lies in setInterval's clock, where only linear time units flow?

Where should I start, to make below images exist in jQuery?

What I would like to do:

  1. Dodge behaviour:

    A, B - particle

    AB1 - common dodge area, only certain amount

enter image description here

2 Movement:

Av, Bv - random circular movement

Aacc, Bacc - where the tiny random acceleration occurs (on image marked as more condenced dashed lines)

enter image description here

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

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

发布评论

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

评论(1

软的没边 2024-12-25 12:35:14

我不会依赖 jQuery 的 animate 来实现这一点,因为你的情况相当特殊......相反,使用“游戏循环模式”:有一个游戏对象,它保存一组移动的粒子(并碰撞......)然后定期绘制。

这是一个基本结构:

function Particle(x, y) {
    this.x = x;
    this.y = y;
    this.speed = 0; // in pixels per second
    this.direction = 0; // in radians per second
}

Particle.prototype.move = function(d_time) {
    this.x += Math.cos(this.direction) * this.speed;
    this.y += Math.sin(this.direction) * this.speed;
}

Particle.prototype.draw = function() {
    // either set the position of a DOM object belonging to this particle
    // or draw to a canvas
}

function Game() {
    this.particles = Array();

    this.MS_PER_FRAME = 20; // in milliseconds
    this.D_TIME = 1000.0 / this.MS_PER_FRAME;
}

Game.prototype.tick = function() {
    $.each(this.particles, function(_, particle) {
        particle.move(this.D_TIME);
        particle.draw();
    })
}

Game.prototype.go = function() {
    setInterval(this.tick, this.MS_PER_FRAME)
})

然后您可以根据需要操纵粒子的速度和方向,也许可以通过引入附加成员d_speed(加速度)和d_direction等。

I would not rely on jQuery's animate for this as your case is rather special ... instead, use the "game loop pattern": Have a game object which keeps a collection of particles, which are moved (and collided ...) and then drawn in regular intervals.

Here's a basic structure:

function Particle(x, y) {
    this.x = x;
    this.y = y;
    this.speed = 0; // in pixels per second
    this.direction = 0; // in radians per second
}

Particle.prototype.move = function(d_time) {
    this.x += Math.cos(this.direction) * this.speed;
    this.y += Math.sin(this.direction) * this.speed;
}

Particle.prototype.draw = function() {
    // either set the position of a DOM object belonging to this particle
    // or draw to a canvas
}

function Game() {
    this.particles = Array();

    this.MS_PER_FRAME = 20; // in milliseconds
    this.D_TIME = 1000.0 / this.MS_PER_FRAME;
}

Game.prototype.tick = function() {
    $.each(this.particles, function(_, particle) {
        particle.move(this.D_TIME);
        particle.draw();
    })
}

Game.prototype.go = function() {
    setInterval(this.tick, this.MS_PER_FRAME)
})

Then you can manipulate speed and direction of particles as you like, maybe by introducing additional members d_speed (acceleration) and d_direction or so.

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