如何创建没有实心多边形或非实心圆的圆形

编程入门 行业动态 更新时间:2024-10-11 19:17:45
本文介绍了如何创建没有实心多边形或非实心圆的圆形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在box2d中开发一个赌场轮盘游戏。你知道在这个游戏中一个球/骰子将在一个圆形的纺车。

首先我尝试了b2circleShape但是没有工作。我的意思是球不在圆形内。

然后我试着用b2EdgeShape。但是对于b2edgeShape,我需要创建尽可能多的顶点来构建一个平滑的圆形表面。

还有,我可以创建不是多边形的圆形

解决方案

如果有任何解决方案,请让我知道

div>

您可以使用轮盘的单一主体来完成此操作。使用圆形形状的内部部分和链形状的外部部分。

你应该在链形状中使用spikes来添加一些东西让球弹起。这里是主要的创建函数:

void MainScene :: CreateBody() { const float32 INNER_RADIUS = 2.50; const float32 OUTER_RADIUS = 3.0; const float32 BALL_RADIUS = 0.1; const uint32 DIVISIONS = 36; Vec2 position(0,0); //创建正文。 b2BodyDef bodyDef; bodyDef.position = position; bodyDef.type = b2_dynamicBody; _body = _world-> CreateBody(& bodyDef); assert(_body!= NULL); //现在将fixture附加到body。 FixtureDef fixtureDef; fixtureDef.density = 1.0; fixtureDef.friction = 1.0; fixtureDef.restitution = 0.9; fixtureDef.isSensor = false; //内圈。 b2CircleShape circleShape; circleShape.m_radius = INNER_RADIUS; fixtureDef.shape =& circleShape; _body-> CreateFixture(& fixtureDef); //外形。 b2ChainShape chainShape; vector< Vec2>顶点; const float32 SPIKE_DEGREE = 2 * M_PI / 180; for(int idx = 0; idx< DIVISIONS; idx ++) { float32 angle =((M_PI * 2)/ DIVISIONS)* idx; float32 xPos,yPos; xPos = OUTER_RADIUS * cosf(angle-SPIKE_DEGREE); yPos = OUTER_RADIUS * sinf(angle-SPIKE_DEGREE); vertices.push_back(Vec2(xPos,yPos)); xPos = OUTER_RADIUS * cosf(angle)*。98; yPos = OUTER_RADIUS * sinf(angle)*。98; vertices.push_back(Vec2(xPos,yPos)); xPos = OUTER_RADIUS * cosf(angle + SPIKE_DEGREE); yPos = OUTER_RADIUS * sinf(angle + SPIKE_DEGREE); vertices.push_back(Vec2(xPos,yPos)); } vertices.push_back(vertices [0]); chainShape.CreateChain(&vertices [0],vertices.size()); fixtureDef.shape =& chainShape; _body-> CreateFixture(& fixtureDef); //创建一些尖峰让球弹起。 //开始旋转 _body-> SetAngularVelocity(M_PI / 8); //现在创建一个球在内部弹跳... bodyDef.position = Vec2((INNER_RADIUS + OUTER_RADIUS)/ 2,0); _ballBody = _world-> CreateBody(& bodyDef); circleShape.m_radius = BALL_RADIUS; fixtureDef.shape =& circleShape; _ballBody-> CreateFixture(& fixtureDef); //给它一些速度,所以它开始反弹。 _ballBody-> SetLinearVelocity(Vec2(-0.5,0.5)); }

这是什么样子:

我创建了一个演示。给球一些初始速度,它会弹跳一点,但很快就卡在外面(离心力?)。要做这个工作,你可能需要保持激动的球,使它反弹一段时间,直到你使它吸引到一个井/传感器点,你想要它结束。

或者你可以在外链中使用一个第二个链夹具,这是光滑的。然后删除它,当你想要的球安定下来。不管怎样,你需要使用力量来保持球移动一点,直到你想要它安定下来,如果你想它看起来像一个轮盘赌球。

我将整个项目(Cocos2d-x,c ++)发布到github 这里。

您可以在我的网站上找到有关使用Box2d的文章这里。

这对您有帮助吗?

I am developing a casino roulette game in box2d. As you know in this game a ball/dice will be in a circular spinning wheel.

First I tried with b2circleShape but it didn't work. I mean that the ball is not sitting inside the circular shape.

Then I tried with b2EdgeShape. But for the b2edgeShape I need to create as many vertices as I can to build a smooth circular surface.

Is there anyway that I can create such circle shapes which are not polygons or which are not solid polygons?

If have any solution please let me know

解决方案

You can do this using a single body for the roulette wheel. Use a circle shape for the "inner" part of it and a chain shape for the "outer" part of it.

You should probably use "spikes" in the chain shape to add something for the ball to bounce off of. Here is the main creation function:

void MainScene::CreateBody() { const float32 INNER_RADIUS = 2.50; const float32 OUTER_RADIUS = 3.0; const float32 BALL_RADIUS = 0.1; const uint32 DIVISIONS = 36; Vec2 position(0,0); // Create the body. b2BodyDef bodyDef; bodyDef.position = position; bodyDef.type = b2_dynamicBody; _body = _world->CreateBody(&bodyDef); assert(_body != NULL); // Now attach fixtures to the body. FixtureDef fixtureDef; fixtureDef.density = 1.0; fixtureDef.friction = 1.0; fixtureDef.restitution = 0.9; fixtureDef.isSensor = false; // Inner circle. b2CircleShape circleShape; circleShape.m_radius = INNER_RADIUS; fixtureDef.shape = &circleShape; _body->CreateFixture(&fixtureDef); // Outer shape. b2ChainShape chainShape; vector<Vec2> vertices; const float32 SPIKE_DEGREE = 2*M_PI/180; for(int idx = 0; idx < DIVISIONS; idx++) { float32 angle = ((M_PI*2)/DIVISIONS)*idx; float32 xPos, yPos; xPos = OUTER_RADIUS*cosf(angle-SPIKE_DEGREE); yPos = OUTER_RADIUS*sinf(angle-SPIKE_DEGREE); vertices.push_back(Vec2(xPos,yPos)); xPos = OUTER_RADIUS*cosf(angle)*.98; yPos = OUTER_RADIUS*sinf(angle)*.98; vertices.push_back(Vec2(xPos,yPos)); xPos = OUTER_RADIUS*cosf(angle+SPIKE_DEGREE); yPos = OUTER_RADIUS*sinf(angle+SPIKE_DEGREE); vertices.push_back(Vec2(xPos,yPos)); } vertices.push_back(vertices[0]); chainShape.CreateChain(&vertices[0], vertices.size()); fixtureDef.shape = &chainShape; _body->CreateFixture(&fixtureDef); // Create some "spikes" for the ball to bounce off of. // Start it spinning _body->SetAngularVelocity(M_PI/8); // NOW create a ball to bounce around inside... bodyDef.position = Vec2((INNER_RADIUS+OUTER_RADIUS)/2,0); _ballBody = _world->CreateBody(&bodyDef); circleShape.m_radius = BALL_RADIUS; fixtureDef.shape = &circleShape; _ballBody->CreateFixture(&fixtureDef); // Give it some velocity so it starts to bounce. _ballBody->SetLinearVelocity(Vec2(-0.5,0.5)); }

This is what it looks like:

I created a demo of this. Give the ball some initial velocity and it will bounce a little, but quickly becomes stuck on the outside (centrifugal force?). To make this work, you will probably need to keep exciting the ball to make it bounce for a while, till you make it "attract" to a well/sensor point where you want it to end up.

Or you could use a second chain fixture a little inside the outer chain, which is "smooth". Then remove it when you want the ball to settle down. No matter what, you will need to use forces to keep the ball moving for a bit till you want it to settle down if you want it to look like a roulette ball in play.

I posted the entire project (Cocos2d-x, c++) to github here.

You can find articles about using Box2d on my site here.

Was this helpful?

更多推荐

如何创建没有实心多边形或非实心圆的圆形

本文发布于:2023-11-29 08:49:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1645919.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实心   多边形   圆形   或非

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!