Drawing boxes correctly and no displaced.

Official forum for the Chipmunk2D Physics Library.
Post Reply
.teri
Posts: 5
Joined: Wed Sep 29, 2010 7:12 am
Contact:

Drawing boxes correctly and no displaced.

Post by .teri »

Hello. Please, I need help.
I create a box (a shape) with cpBoxShapeNew. Ok, fine. The problem is when I draw it with SFML. The graphic is displaced right-bottom from the body. Very odd. With circles, poly and segments all ok. Any advice? Thanks.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Drawing boxes correctly and no displaced.

Post by slembcke »

How are you drawing them? Need a bit more to go on then that.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
.teri
Posts: 5
Joined: Wed Sep 29, 2010 7:12 am
Contact:

Re: Drawing boxes correctly and no displaced.

Post by .teri »

In each step in a custom UpdatePosition from body position is setted the image position. Inside the custom UpdatePosition is called before the next code the chipmunk UpdatePosition.

Code: Select all

//...
pos = cpBodyGetPos(body);
sfShape_SetPosition(shape, pos.x, pos.y);
User avatar
pat
Posts: 53
Joined: Tue Jul 20, 2010 7:55 pm
Contact:

Re: Drawing boxes correctly and no displaced.

Post by pat »

not sure if this is it, But cpBoxShapeNew will make the box centered. So if its like the bottom right of the image is drawing at the origin (so cpBoxShapeNew(20,20) would be -10 to +10 in each direction), then when your calling sfShape_SetPosition or whatever, you'd need to do pos.x - givenWidth/2, pos.y - givenHeight/2 . Or maybe i'm not understanding the issue.
.teri
Posts: 5
Joined: Wed Sep 29, 2010 7:12 am
Contact:

Re: Drawing boxes correctly and no displaced.

Post by .teri »

You are right Pat. Aparently your solution works until the body start to rotate... When the body rotate the image starts to displace. Anyway thanks.

I've attached two images for see better the issue. bd2.png is the object rotated 180 º. bd1 a few degrees.
jason_rogers
Posts: 81
Joined: Fri Aug 27, 2010 1:00 am
Contact:

Re: Drawing boxes correctly and no displaced.

Post by jason_rogers »

if your having issues with the other shapes you can look at the drawObject method in the examples

if your using opengl or any matix based display you can use this coed snipet
http://code.google.com/p/chipmunk-physi ... ulSnippets
User avatar
pat
Posts: 53
Joined: Tue Jul 20, 2010 7:55 pm
Contact:

Re: Drawing boxes correctly and no displaced.

Post by pat »

.teri wrote:You are right Pat. Aparently your solution works until the body start to rotate... When the body rotate the image starts to displace. Anyway thanks.

I've attached two images for see better the issue. bd2.png is the object rotated 180 º. bd1 a few degrees.
so you could then do

Code: Select all

cpVect vOffset = cpvsub(body->pos, cpvrotate(cpvmult(cpv(givenWidth, givenHeight), 0.5f), body->rot));
sfShape_SetPosition(shape, vOffset.x, vOffset.y);
and if that still looks off try replacing cpvrotate with cpvunrotate
.teri
Posts: 5
Joined: Wed Sep 29, 2010 7:12 am
Contact:

Re: Drawing boxes correctly and no displaced.

Post by .teri »

Solved. Reading this thread: http://www.sfml-dev.org/forum/viewtopic ... er+utility i found the solution. I've deleted all the code that fixes the shape position for a box. Then i've wrote just this line in the box constructor:

Code: Select all

sfShape_SetCenter(shape, width/2, height/2);
Im finally happy :D. Thanks to everyone. Maybe I should have put this thread in SFML forums.
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests