试图制作一个简单的欧拉公式

时间:2017-11-20 04:23:25

标签: c# physics monogame particle-system

https://gamedev.stackexchange.com/questions/13178/why-is-my-velocity-decaying

我正在使用该线程中的建议答案来尝试我的粒子系统的基本物理公式。出于某种原因,它甚至不再移动我的粒子了。有人可以更好地学习数学,然后我看看并确保我做正确的公式吗?这些东西超出了我的脑海,但我想对我的粒子施加一些重力。

Particle.cs

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Game1
{
    public class Particle
    {
        public Texture2D Texture { get; set; }
        public Vector2 Position { get; set; }
        public Vector2 Velocity { get; set; }
        public float Mass = 1.0f;
        public Vector2 Acceleration { get; set; }
        public float Angle { get; set; }
        public float AngularVelocity { get; set; }
        public Color Color { get; set; }
        public float Size { get; set; }
        public int TTL { get; set; } //Time to live

        public Particle(Texture2D texture, Vector2 position, Vector2 velocity, Vector2 acceleration, float angle, float angularVelocity, Color color, float size, int ttl)
        {
            Texture = texture;
            Position = position;
            Velocity = velocity;
            Acceleration = acceleration;
            Angle = angle;
            AngularVelocity = angularVelocity;
            Color = color;
            Size = size;
            TTL = ttl;
        }

        public void Update(GameTime gameTime)
        {
            var dt = gameTime.ElapsedGameTime.TotalSeconds;

            TTL--;
            var t = Vector2.Multiply(Velocity, (float)dt);
            var r = Vector2.Multiply(Acceleration, 0.5f);
            var q = Vector2.Multiply(t + r, (float)dt);
            var f = Vector2.Multiply(q, (float)dt);

            Position += f;
            Velocity += Vector2.Multiply(Acceleration, (float)dt);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            Rectangle sourceRectangle = new Rectangle(0, 0, Texture.Width, Texture.Height);
            Vector2 origin = new Vector2(Texture.Width / 2, Texture.Height / 2);
            spriteBatch.Draw(Texture, Position, sourceRectangle, Color, Angle, origin, Size, SpriteEffects.None, 0f);
        }
    }
}

创建粒子

private Particle GenerateNewParticle(GameTime gameTime)
        {
            Texture2D texture = textures[random.Next(textures.Count)];
            Vector2 position = EmitterLocation;

            Vector2 velocity = new Vector2(1f * (float)(random.NextDouble() * 2 - 1), (1f * (float)(random.NextDouble() * 2 - 1)));
            Vector2 acceleration = new Vector2(20f, 20f);
            float angle = 0;
            float angularVelocity = 0.1f * (float)(random.NextDouble() * 2 - 1);
            Color color = new Color((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble());
            float size = (float)random.NextDouble();
            int ttl = 20 + random.Next(40);

            return new Particle(texture, position, velocity, acceleration, angle, angularVelocity, color, size, ttl);
        }

1 个答案:

答案 0 :(得分:0)

看起来你的q词不正确。运算符优先级将首先执行乘法运算,然后执行加法运算。要实现您引用的等式,

q应为Vector2.Multiply(r, (float)dt);

然后,您对位置的更新需要变为Position += (t+f);