Collision Detection and manual rotated shapes

Official forum for the Chipmunk2D Physics Library.
extraz
Posts: 10
Joined: Mon Jul 05, 2010 6:41 am
Contact:

Collision Detection and manual rotated shapes

Post by extraz »

Hi,

I am quite new with Chipmunk, I am currently having a problem with collisionHandler and callbacks.

I have a simple rectangle 5x10 in which I manually set the angle using cpBodySetAngle() when the user does something, (I update the image base on the body->a variable and it shows up correctly so I assume the angle value is correct.

Say the user turn the rectangle 45 degree, when another object collide with it and the callback event is called, the collision point is not where the object hits.

Hence, ideally I want the collision point to be like at point X in the diagram below

Code: Select all

  
    ___
   /  /
  /__x



But I am getting this

Code: Select all

  
    ___
   /  /
  /__/ x

Is this happening because I am setting the angle manually?

Appreciate any help.

Thank you
extraz
extraz
Posts: 10
Joined: Mon Jul 05, 2010 6:41 am
Contact:

Re: Collision Detection and manual rotated shapes

Post by extraz »

Help please?
mobilebros
Posts: 90
Joined: Tue Aug 04, 2009 9:53 am
Contact:

Re: Collision Detection and manual rotated shapes

Post by mobilebros »

I would guess your rotation calculation is incorrect..... the 'a' variable is in radians, so keep that in mind.
extraz
Posts: 10
Joined: Mon Jul 05, 2010 6:41 am
Contact:

Re: Collision Detection and manual rotated shapes

Post by extraz »

Thanks for the reply but I believe the rotation is correct and I am using radians. The reason why I believe it is correct because the display image rotation is based on Chipmunk body->a and it displayed correctly.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Collision Detection and manual rotated shapes

Post by slembcke »

Just got home from a long weekend. How far out of the shape is the collision point? Collision points aren't necessarily on the surface of the shapes. Sometimes you have to estimate a point based on how much they are overlapping. The more the shapes overlap, the worse the estimate will be.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
extraz
Posts: 10
Joined: Mon Jul 05, 2010 6:41 am
Contact:

Re: Collision Detection and manual rotated shapes

Post by extraz »

Hi Slembcke,

Thanks for the reply, I hope you had a good long weekend.

if I do not rotate the rectangle then the collision point is perfect, however when I rotate it say by 45 degree then the collision point would be at the longest extended point.

In the picture below, it will starting to push the blue circular object to the left, if the rectangle is moving towards left (sorry about the drawing, the dotted line is suppose to be a straight line :P)

Image

What I am currently doing is when the user does something then the application will return angle value then I translate that angle value to the shape using cpBodySetAngle().

The offset seems to be incorrect after rotation... do I need to do anything special when I set the angle manually?

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

Re: Collision Detection and manual rotated shapes

Post by slembcke »

This is from a collision between a square and a circle shape? You are getting a point that lies way outside of either shape like that?

Are you treating the collision point as being in absolute coordinates or are you trying to rotate it relative to the body or something?

It's possible that the collision point is returning like that if they are deeply embedded. I guess I can look, but I sort of doubt it could be that bad.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
extraz
Posts: 10
Joined: Mon Jul 05, 2010 6:41 am
Contact:

Re: Collision Detection and manual rotated shapes

Post by extraz »

Hello,

Sorry Slem, I should've been more clear, I am doing all on the iphone. All shapes are non-static poly. The offset seems to be wrong after I rotate them (without rotation the shapes collide perfectly). I believe I am missing some steps to adjust the offset value after rotation, so one side of my rectangle would have the collision point too early and another side would have the collision point too late.

I initialize the shape like this:

Code: Select all

cpVect rectVects[] = { cpv(0.0f,0.0f),cpv(25.0f,0.0f),cpv(25.0f,-150.0f),cpv(0.0f,-150.0f)};

cpBody *rectBody = cpBodyNew(100, cpMomentForPoly(100, 4, rectVects , cpvzero ));

rectBody->p = cpv(240.0f-(25.0f/2), 220.0f);

rectBody->f = cpvmult(space->gravity, -rectBody->m);

cpSpaceAddBody(space, rectBody);

cpShape *rectShape = cpPolyShapeNew(rectBody, 4, rectVects, cpv(-25.0f/2.0f , 150.0f/2.0f) );

rectShape->e = 0.1 ; rectShape->u = 1.0; rectShape->collision_type=1; rectShape->data = myRect;

cpSpaceAddShape(space, rectShape);
cpSpaceAddCollisionHandler(space, 0, 1, NULL, collisionMethod, NULL, NULL, myRect);
When I rotate my shape, I do the following

Code: Select all

cpBodySetAngle(body, body->a + degreesToRadians(angle));
Thank you.
extraz
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Collision Detection and manual rotated shapes

Post by slembcke »

So you are rotating a static shape? By adding a shape to Chipmunk as being static, you are telling Chipmunk to assume that it will never move. Either add the shape as a non-static shape, or you'll need to call the function that updates the collision data for the shape and update the spatial hash. The first option is much simpler.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
extraz
Posts: 10
Joined: Mon Jul 05, 2010 6:41 am
Contact:

Re: Collision Detection and manual rotated shapes

Post by extraz »

HI,

THanks for the reply, however, all the shapes are normal in normal shape, they are not static.

extraz
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests