C# 二维胶囊或扫描球体的碰撞检测
我试图学习如何用数学方法定义和检测2D胶囊上的“点击” 我定义并检测2D圆上的点击。这是我的班级:C# 二维胶囊或扫描球体的碰撞检测,c#,raytracing,C#,Raytracing,我试图学习如何用数学方法定义和检测2D胶囊上的“点击” 我定义并检测2D圆上的点击。这是我的班级: class Circle { private double xpos; private double ypos; private double radius; public Circle(double x, double y, double r) { xpos = x; ypos = y; radius =
class Circle
{
private double xpos;
private double ypos;
private double radius;
public Circle(double x, double y, double r)
{
xpos = x;
ypos = y;
radius = r;
}
public bool HitCircle(double x, double y)
{
bool hit = false;
double distance = Math.Pow(x - xpos, 2) + Math.Pow(y - ypos, 2);
if (distance <= radius * radius) hit = true;
return hit;
}
}
我需要帮助理解和实现capsule类中的HitCapsule
函数的数学
据我所知,胶囊就像一个圆,不同的是它不是围绕一个点有半径,而是围绕一个线段有半径
现在,我只是想把我的大脑围绕在这些几何定义上。我可能会尝试在将来把它变成一个简单的光线跟踪器,但我想直接进入这些数学部分
谢谢
我不知道这是否有用,但这是我的2d“光线跟踪器”。它使用ascii将圆圈绘制到控制台。向我展示我正确地实现了数学是很有帮助的
static void Main(string[] args)
{
double aimx = 30;
double aimy = 8;
Circle circle = new Circle(45, 13, 12);
bool hit = circle.HitCircle(aimx, aimy);
Console.WriteLine("Did we hit? {0}", hit);
for(int y = 26; y >= 0; y--)
{
for(int x = 0; x < 90; x++)
{
if(x == aimx && y == aimy) //Draw target
{
Console.Write("X");
}
else if(circle.HitCircle(x, y)) //Draw all circle hits
{
Console.Write("#");
}
else
{
Console.Write("-");
}
}
Console.Write('\n');
}
Console.Read();
}
}
static void Main(字符串[]args)
{
双aimx=30;
双目标=8;
圆圈=新圆圈(45,13,12);
bool hit=circle.hitcirle(aimx,aimy);
WriteLine(“我们击中了吗?{0}”,击中);
对于(int y=26;y>=0;y--)
{
对于(int x=0;x<90;x++)
{
if(x==aimx&&y==aimy)//绘制目标
{
控制台。写入(“X”);
}
else if(circle.hitcirle(x,y))//绘制所有圆命中
{
控制台。写(“#”);
}
其他的
{
控制台。写(“-”;
}
}
Console.Write('\n');
}
Console.Read();
}
}
点到胶囊交点是计算点到定义胶囊的线段的距离的一种情况。如果这是谢谢你的链接,我现在正在通读。看起来数学很令人兴奋。你什么都没看到。这是collisionI中比较简单的问题之一,我可以使用您提供的链接让一个胶囊工作。我仍在努力理解数学,但这肯定是正确的方向。
static void Main(string[] args)
{
double aimx = 30;
double aimy = 8;
Circle circle = new Circle(45, 13, 12);
bool hit = circle.HitCircle(aimx, aimy);
Console.WriteLine("Did we hit? {0}", hit);
for(int y = 26; y >= 0; y--)
{
for(int x = 0; x < 90; x++)
{
if(x == aimx && y == aimy) //Draw target
{
Console.Write("X");
}
else if(circle.HitCircle(x, y)) //Draw all circle hits
{
Console.Write("#");
}
else
{
Console.Write("-");
}
}
Console.Write('\n');
}
Console.Read();
}
}