【发布时间】:2019-12-28 04:33:42
【问题描述】:
我正在单游戏中为简单的形状生成动态纹理。是的,我知道这个系统的缺点,但我只是在尝试构建自己的物理引擎。我正在尝试按照here 的描述为椭圆生成纹理。
我有一个函数 PaintDescriptor,它接受一个 x 和 y 像素坐标并返回它应该是什么颜色。红色只是在我调试的时候,通常是 Color.Transparent。
public override Color PaintDescriptor(int x, int y)
{
float c = (float)Width / 2;
float d = (float)Height / 2;
return pow((x - c) / c, 2) + pow((y - d) / d, 2) <= 1 ? BackgroundColor : Color.Red;
}
现在,如果宽度 == 高度,这可以工作,所以,一个圆圈。但是,如果它们不相等,它会生成一些类似椭圆形状的纹理,但也会带有条带/条带。
我尝试查看我的宽度和高度是否被切换,并且我尝试了其他一些方法。需要注意的一点是,在desmos的法线坐标系中我有(y + d)/ d,但是由于屏幕的y轴被翻转,我必须翻转代码中的y偏移:(y - d)/ d。纹理生成和绘制的其余相关代码在这里:
public Texture2D GenerateTexture(GraphicsDevice device, Func<int, int, Color> paint)
{
Texture2D texture = new Texture2D(device, Width, Height);
Color[] data = new Color[Width * Height];
for (int pixel = 0; pixel < data.Count(); pixel++)
data[pixel] = paint(pixel / Width, pixel % Height);
texture.SetData(data);
return texture;
}
public void Draw(float scale = 1, float layerdepth = 0, SpriteEffects se = SpriteEffects.None)
{
if (SBRef == null)
throw new Exception("No reference to spritebatch object");
SBRef.Draw(Texture, new Vector2(X, Y), null, null, null, 0, new Vector2(scale, scale), Color.White, se, layerdepth);
}
public float pow(float num, float power) //this is a redirect of math.pow to make code shorter and more readable
{
return (float)Math.Pow(num, power);
}
为什么这不匹配desmos?为什么不画椭圆?
编辑:我忘了提及,但我遇到的一种可能的解决方案是始终画一个圆圈,然后将其缩放到所需的宽度和高度。这对我来说是不可接受的,因为绘图或其他伪影可能存在一些模糊,但更主要是因为我想了解我目前无法通过此解决方案获得的任何内容。
【问题讨论】:
标签: c# math textures monogame ellipse