Segment shape doubts

Official forum for the Chipmunk2D Physics Library.
Post Reply
Baxnie
Posts: 9
Joined: Fri May 10, 2013 7:54 pm
Contact:

Segment shape doubts

Post by Baxnie »

Hello, i've got a few doubts about segment shapes:

I've been trying to understand segment shape radius property.
I took a look at ChipmunkDebugDraw.c function:

Code: Select all

void ChipmunkDebugDrawFatSegment(cpVect a, cpVect b, cpFloat radius, cpSpaceDebugColor outlineColor, cpSpaceDebugColor fillColor)
however i couldn't understand much of it.

Then I tried to make my own segment draw function, based on the shown bounding box.

Code: Select all

case CP_SEGMENT_SHAPE: {
    cpSegmentShape *shape = (cpSegmentShape*)m_shape;
    if(shape->r == 0) {
        PointF p1 = PointF(shape->a.x - position.x, -(shape->a.y - position.y));
        PointF p2 = PointF(shape->b.x - position.x, -(shape->b.y - position.y));
        g_painter->drawLine(p1 * ppu, p2 * ppu);
    }
    else {
        float angle = std::atan2(shape->b.y - shape->a.y, shape->b.x - shape->a.x);
        PointF p1 = PointF(shape->a.x - position.x, -(shape->a.y - position.y));
        PointF p2 = PointF(shape->b.x - position.x, -(shape->b.y - position.y));
        PointF dR = PointF(shape->r * sin(angle), shape->r * cos(angle));
        g_painter->drawLine((p1 - dR) * ppu, (p2 - dR) * ppu);
        g_painter->drawLine((p1 + dR) * ppu, (p2 + dR) * ppu);
        g_painter->drawCircle(p1 * ppu, shape->r * ppu, 20, angle + stdext::pi / 2., angle + stdext::pi / 2. + stdext::pi);
        g_painter->drawCircle(p2 * ppu, shape->r * ppu, 20, angle - stdext::pi / 2., angle - stdext::pi / 2. + stdext::pi);
    }
    break;
}
Some images to show what's been draw:

ImageImageImage
(Gray is the bounding box, Red is the shape)

Is this how radius is supposed to be?
The collision is not exactly as I expected, and that's why I'm creating this topic.

Another question about them,
I'm adding segment shapes to my 'background' objects (they wont collide ever) so it's bounding box can fit the whole texture.

Image to exemplify the use of segment shapes on 'background' objects.
Image

Is this the right approach to do this?
I couldnt figure another way to adjust their bounding box so they'll be detected by queryBB function.

Also,

Code: Select all

typedef struct cpSegmentShape {
	cpShape shape;
	
	cpVect a, b, n;
	cpVect ta, tb, tn;
	cpFloat r;
	
	cpVect a_tangent, b_tangent;
} cpSegmentShape;
a,b are the endpoints of segment shapes. n is the normal?
r is the radius, and what's ta, tb, tn?

Thanks a lot !
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Segment shape doubts

Post by slembcke »

Assuming you mean the ChipmunkDebugDrawFatSegment() function in 6.x, it's using the z-coordinate to set the segment's length using a transform matrix. That way I could draw everything from the same vertex data without having to generate new data for each draw call.

The radius on the segment shape just turns it into a pill shape. As if you centered a circle with the same radius over each endpoint and drew around them. It looks like your debug draw function is correct. Maybe a screenshot of a collision would help debug it?

If you only want to put a segment shape on your background objects so you can query for their bounding boxes, then putting a segment across the diagonal will work fine yes. Is this for culling renderable objects or something? You might find a more hierarchical structure to be more efficient, but I suppose BB queries will probably have perfectly acceptable performance too.

ta/tb/tn are the a/b/n values transformed to global coordinates. I would not generally recommend reading directly out of those.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Baxnie
Posts: 9
Joined: Fri May 10, 2013 7:54 pm
Contact:

Re: Segment shape doubts

Post by Baxnie »

Firstly, thanks for your quick answer.

The collision was weird while testing the game, maybe I've made a mistake somewhere else. I'll make a better debugging so I can properly test collisions.

My camera class is pretty simple, despite keep aspect ratio and stuff used for parallax, it's just a cpBB.
I'm adding shapes to the space and, before drawing, calling queryBB at camera's area to retrieve shapes -> objects.
As you said, BB queries gave me a good performance. (until now)
I don't get what you mean by a more hierarchical structure. How would that work?

ta/tb might be what I was looking for!
I was having a small trouble when decided to add static shapes to Space's static body (instead of creating a single body for every object).
As the static body position is (0,0), all my shapes were drawn there.
I've spent a lot of time trying to figure that out, however I ended up creating a 'position' variable used by static objects, which is passed to drawDebugShape function, as shown in the previous post.

So, how does a/b/ta/tb work on static and non-static bodies exactly?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Segment shape doubts

Post by slembcke »

"I don't get what you mean by a more hierarchical structure. How would that work? "

the idea is that you could group your sprites into a tree like structure. Like a building might be composed of multiple sprites. So you check the bounding box of the building as a whole, if that is in the camera's view, then check start checking each sprite it is made from. Then you can group a bunch of buildings in a block into a group and apply the same logic. The idea is that you add a few extra bounding box checks, but when you find one that fails, you only had to try one bounding box instead of a dozen. Chipmunk's spatial index is based on a BB tree that guesses how to best organize objects into groups. If you have a lot of static objects, you might be able to get better performance if they are grouped by hand.

Of course you should only make a big change like that if the profiler makes it seem like a good idea. ;)
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests