Box2D Forums

It is currently Sat May 25, 2013 3:35 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Fri Mar 28, 2008 5:17 am 
Offline

Joined: Thu Nov 15, 2007 3:23 am
Posts: 24
hello again,

i'm currently trying to get box2d 2.0 work with the dark gdk sdk.

since the position of a simple sprite is always on the left corner, serialization with a b2body-coordinate turned out pretty easy:

Code:
void PaperSprite::SetPositionCenter(b2Vec2& position)
{
   m_position = position;
   m_position.x -= dbSpriteWidth(m_spriteNumber) / 2;
   m_position.y -= dbSpriteHeight(m_spriteNumber) / 2;
   dbSprite(m_spriteNumber, (int)m_position.x, (int)m_position.y, m_pictureNumber);
}


but unfortunately dark sdk isn't supporting any function to set the pivot point. i need to find a way to "calculate" the point to the center of the sprite.

currently my serialization with the b2body's rotation looks like that:

Code:
void PaperSprite::SetRotate(float angle)
{
   angle *= -180 / b2_pi;
   dbWrapValue(angle);

   dbRotateSprite(m_spriteNumber, angle);
}


...but the rolling ball looks pretty awkward, since the pivot point of dbRotateSprite isn't in the center, but on the left corner of the sprite...

i know that there must be a way to calculate the center of the rotated sprite and set the position correctly, but unfortunately i wasn't able to get decent results...

and that's why i'm asking you: anybody here to help me out? :)


Attachments:
File comment: a little screenshot illustrating what i want ;)
problem.png
problem.png [ 26.83 KiB | Viewed 981 times ]
Top
 Profile  
 
PostPosted: Fri Mar 28, 2008 7:44 am 
Offline

Joined: Mon Jan 07, 2008 10:51 am
Posts: 1911
Rotating the sprite about a different point involves moving it's position, so it's best to merge your two functions:
Code:
void PaperSprite::SetXForm(b2Vec2& position, float angle)
{
   m_position = position;
   float offsetx=dbSpriteWidth(m_spriteNumber) / 2;
   float offsety=dbSpriteHeight(m_spriteNumber) / 2;
   m_position.x -= cos(angle)*offsetx + sin(angle)*offsety;
   m_position.y -= -sin(angle)*offsetx + cos(angle)*offsety;
   dbSprite(m_spriteNumber, (int)m_position.x, (int)m_position.y, m_pictureNumber);

   angle *= -180 / b2_pi;
   dbWrapValue(angle);
   dbRotateSprite(m_spriteNumber, angle);
}

This code may need some sign changes, etc. You could still have seperate functions, but I wasn't sure how to translate a sprite in your sdk.
Even better would be to pass the rotation matrix from box2d (body.GetXForm().R), and use the values from that to replace the calls to sin and cos.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group