如何找到box2d中形状内部的触摸位置?

发布于 2024-11-28 13:10:20 字数 1234 浏览 1 评论 0原文

请任何人帮助我,

我在我的项目中创建了边缘。我知道如何检查触摸位置是否在形状内部,

if (fix->TestPoint(locationWorld))
    {
        CCLOG(@"****touch inside shape***");
    }

但我想创建六边形形状,并且我需要球创建内部六边形形状并在六边形内部移动。

我使用以下代码将球移动到六边形形状内: b2BodyDef 地面BodyDef; groundBodyDef.position.Set(0, 0); b2Body* groundBody = world->CreateBody(&groundBodyDef);

int countVal=[pos count];
// Define the ground box shape.
b2PolygonShape groundBox;
int range=[pos count]-2;

CCLOG(@"countVal=%d,range=%d",countVal,range);

for (int i=0; i<=range; i=i+2)
{
    float x1,y1,x2,y2;

    x1=[[pos objectAtIndex:i] floatValue];
    y1=[[pos objectAtIndex:i+1] floatValue];

    if (i==range)
    {
        x2=[[pos objectAtIndex:0] floatValue];
        y2=[[pos objectAtIndex:1] floatValue];

    }
    else 
    {
        x2=[[pos objectAtIndex:i+2] floatValue];
        y2=[[pos objectAtIndex:i+3] floatValue];

    }

            CCLOG(@"x1=%f,y1=%f,x2=%f,y2=%f",x1,y1,x2,y2);

    groundBox.SetAsEdge(b2Vec2(x1/PTM_RATIO,y1/PTM_RATIO),b2Vec2(x2/PTM_RATIO,y2/PTM_RATIO));
    groundBody->CreateFixture(&groundBox, 0);

}

但我不知道如何找到多边形内部的触摸位置。我使用 cocos2d 和 box2d

请任何人帮助我

pls any one help me

i have create the edges in my project.i know how to check touch position is inside the shape

if (fix->TestPoint(locationWorld))
    {
        CCLOG(@"****touch inside shape***");
    }

but i want create hexagon shape and i need to ball create inside hexagon shape and move inside hexagon.

i did ball move inside hexagon shape using below code:
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0, 0);
b2Body* groundBody = world->CreateBody(&groundBodyDef);

int countVal=[pos count];
// Define the ground box shape.
b2PolygonShape groundBox;
int range=[pos count]-2;

CCLOG(@"countVal=%d,range=%d",countVal,range);

for (int i=0; i<=range; i=i+2)
{
    float x1,y1,x2,y2;

    x1=[[pos objectAtIndex:i] floatValue];
    y1=[[pos objectAtIndex:i+1] floatValue];

    if (i==range)
    {
        x2=[[pos objectAtIndex:0] floatValue];
        y2=[[pos objectAtIndex:1] floatValue];

    }
    else 
    {
        x2=[[pos objectAtIndex:i+2] floatValue];
        y2=[[pos objectAtIndex:i+3] floatValue];

    }

            CCLOG(@"x1=%f,y1=%f,x2=%f,y2=%f",x1,y1,x2,y2);

    groundBox.SetAsEdge(b2Vec2(x1/PTM_RATIO,y1/PTM_RATIO),b2Vec2(x2/PTM_RATIO,y2/PTM_RATIO));
    groundBody->CreateFixture(&groundBox, 0);

}

but i dont know how to find touch position is inside polygon shape.i use cocos2d with box2d

pls any one help me

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

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

发布评论

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

评论(1

定格我的天空 2024-12-05 13:10:20

最后我得到了答案:
我正在检查该点是否在多边形内部:

int pnpoly(int npol, float *xp, float *yp, float x, float y)
{
  int i, j, c = 0;
  for (i = 0, j = npol-1; i < npol; j = i++) 
{
    if ((((yp[i] <= y) && (y < yp[j])) ||
         ((yp[j] <= y) && (y < yp[i]))) &&
        (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i]))
      c = !c;
  }
  return c;
}

Finally i got the answer:
i am checking if the point is inside polygon:

int pnpoly(int npol, float *xp, float *yp, float x, float y)
{
  int i, j, c = 0;
  for (i = 0, j = npol-1; i < npol; j = i++) 
{
    if ((((yp[i] <= y) && (y < yp[j])) ||
         ((yp[j] <= y) && (y < yp[i]))) &&
        (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i]))
      c = !c;
  }
  return c;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文