Collision handling of EdgeShape in Box2D

Source: Internet
Author: User
Sometimes two bodies need to be precise to an edge during collision. Therefore, when creating a body, you need to create multiFixture, that is, a combination of multiple fixture. In earlier Box2D versions, PolygonShape has the setAsEdge method to set the fixture of each edge. After v2.2.1, only the setAsBox method is available. Method 1: You can add an Edg to the position of each edge.

Sometimes two bodies need to be precise to an edge during collision. Therefore, when creating a body, you need to create multiFixture, that is, a combination of multiple fixture. In earlier Box2D versions, Polygon Shape has the setAsEdge method to set the fixture of each edge. After v2.2.1, only the setAsBox method is available. Method 1: You can add an Edg to the position of each edge.

Sometimes two bodies need to be precise to an edge during collision. Therefore, when creating a body, you need to create multiFixture, that is, a combination of multiple fixture. In earlier Box2D versions, Polygon Shape has the setAsEdge method to set the fixture of each edge. After v2.2.1, only the setAsBox method is available.

Method 1: You can add an Edge Shape to the position of each Edge, and then add the fixture of the Edge to the body and save it, when a collision occurs, you can determine which side you encounter.

Method 2: directly use the createFixture (edgeShape, density) method of the body to directly create an edge fixture, because the general edge does not require other attributes. The default side has friction and elasticity.

The edge is also based on the midpoint of the body, so the start and end points of the edge should be calculated in width and height.

In this way, the collision type after adding (the player stands on the platform) can be divided:

1. The player polygonShape and platform topEdgeShape collide.

2. The bottomEdgeShape of player and the polygonShape of platform collide.

3. The polygonShape of the player and the polygonShape of the platform collide.

4. The bottomEdgeShape of the player and the topEdgeShape of platform do not collide because they are all Edge type.

The edgeShape length is a little shorter than the length of the corresponding side of polygonShape, so that the edgeShape does not affect each other when determining whether the two sides of the left side and the top and bottom sides are collided. When a rightEdge collision occurs, bottomEdge does not collide. When a bottomEdge collision occurs, rightEdge does not.

If the edgeShape length is the same as the length of the corresponding side of polygonShape, when the rightEdge collision occurs, the right side of bottomEdge will also be directed to the wall, causing a logical error. For example, set the player status to isJump = NO when bottomShape is in collision, and isJump = YES when bottomEdge is not in collision. When the player jumps and encounters a wall, the isJump should be in the YES state, but bottomEdge checks the collision and sets the isJump to NO, so that other player behaviors and actions will be wrong.

You can also cut the right corner vertex of the rectangular polygonShape into an octagonal shape.

As shown in:


1.

    //DELTA_LENGTH = 0.5       //fixture1 with polygon shape    b2PolygonShape rectShape;    rectShape.SetAsBox(size.x/2/PTM_RATIO, size.y/2/PTM_RATIO);        b2FixtureDef fixtureDef1;    p_body->CreateFixture(&fixtureDef1);        //fixture2 with edge shape    b2EdgeShape edgeShape;    edgeShape.Set(b2Vec2((-size.x/2 + DELTA_LENGTH)/PTM_RATIO, (-size.y/2)/PTM_RATIO),                   b2Vec2((size.x/2 - DELTA_LENGTH)/PTM_RATIO, (s-size.y/2)/PTM_RATIO));                            b2FixtureDef fixtureDef2;    fixtureDef2.shape = &edgeShape;    p_bottomFixture = p_body->CreateFixture(&fixtureDef2);

2.

b2EdgeShape edgeShape;    edgeShape.Set(b2Vec2((-size.x/2 + DELTA_LENGTH)/PTM_RATIO, (-size.y/2)/PTM_RATIO),                   b2Vec2((size.x/2 - DELTA_LENGTH)/PTM_RATIO, (-size.y/2)/PTM_RATIO));    p_bottomFixture = p_body->CreateFixture(&edgeShape, 0);

3. polygonShape must be a convex polygon. The vertex is given in turn in the clockwise direction.

b2PolygonShape polygonShape;    b2Vec2 vec[] = {b2Vec2(-size.x/2/PTM_RATIO, -size.y/2/PTM_RATIO),                     b2Vec2(size.x/2/PTM_RATIO, -size.y/2/PTM_RATIO),                     b2Vec2(size.x/2/PTM_RATIO, -size.y/4/PTM_RATIO),                     b2Vec2(size.x/4/PTM_RATIO, size.y/2/PTM_RATIO),                     b2Vec2(-size.x/4/PTM_RATIO, size.y/2/PTM_RATIO),                     b2Vec2(-size.x/2/PTM_RATIO, -size.y/4/PTM_RATIO)};    polygonShape.Set(vec, 6);    b2FixtureDef fixtureDef1;    fixtureDef1.shape = &polygonShape;    fixtureDef1.density = dens;    fixtureDef1.friction = f;    fixtureDef1.restitution = rest;    p_polygonFixture = p_body->CreateFixture(&fixtureDef1);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.