In cocos2D,if you give a CCSprite a position,it actually means the position of the anchor of the CCSprite which is usually different from this sprite's local node space origin(the bottom-left of the texture),Right? But when I make a body with a custom polygon from a texture in Box2D and give the body a position,I found that it means the position of local node space origin(0,0) of this body which seems the the bottom-left of the texture where I get my polygon from.I am wondering if there is not an anchor in Box2D body like in cocos2D sprite?Does the position of body mean the position body's local coordinate origin?If I get the body's shape from a texture(picture),does it mean that the body's local coordinate origin is the the bottom-left of the texture? I hope that I express my confusion clearly....
Confused with the anchor and local node space origin of the body in Box2D
179 views Asked by user3050371 At
1
There are 1 answers
Related Questions in COCOS2D-IPHONE
- Delay in loading Html Page(WebView) from assets folder in real android device
- MPAndroidChart method setWordWrapEnabled() not found
- Designing a 'new post' android activity
- Android :EditText inside ListView always update first item in the listview
- Android: Transferring Data via ContentIntent
- Wrong xml being inflated android
- AsyncTask Class
- Unable to receive extras in Android Intent
- Website zoomed out on Android default browser
- Square FloatingActionButton with Android Design Library
Related Questions in ANCHOR
- Delay in loading Html Page(WebView) from assets folder in real android device
- MPAndroidChart method setWordWrapEnabled() not found
- Designing a 'new post' android activity
- Android :EditText inside ListView always update first item in the listview
- Android: Transferring Data via ContentIntent
- Wrong xml being inflated android
- AsyncTask Class
- Unable to receive extras in Android Intent
- Website zoomed out on Android default browser
- Square FloatingActionButton with Android Design Library
Related Questions in BOX2D
- Delay in loading Html Page(WebView) from assets folder in real android device
- MPAndroidChart method setWordWrapEnabled() not found
- Designing a 'new post' android activity
- Android :EditText inside ListView always update first item in the listview
- Android: Transferring Data via ContentIntent
- Wrong xml being inflated android
- AsyncTask Class
- Unable to receive extras in Android Intent
- Website zoomed out on Android default browser
- Square FloatingActionButton with Android Design Library
Related Questions in COORDINATE
- Delay in loading Html Page(WebView) from assets folder in real android device
- MPAndroidChart method setWordWrapEnabled() not found
- Designing a 'new post' android activity
- Android :EditText inside ListView always update first item in the listview
- Android: Transferring Data via ContentIntent
- Wrong xml being inflated android
- AsyncTask Class
- Unable to receive extras in Android Intent
- Website zoomed out on Android default browser
- Square FloatingActionButton with Android Design Library
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Popular Tags
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
When you create a Box2D body, you give it an initial position. This is NOT the center of mass, but the position of the "container" for all the fixtures attached to that body. All the fixtures are attached relative to that point.
For example, in the body defined in this function, I am attaching several polygons (squares) to the body. The overall effect is to create a giant letter "T".
Note that the vertices for each polygon are relative to the body position, which is (0,0).
A sprite is displayed relative to its position by its anchor. The relative position is expressed in the range [0,1] in both (x,y) coordinates. Unless there is pretty good reason to do otherwise (for effects), I usually set the anchor to (0.5, 0.5) so that sprite's x,y display center shows up in the same position as the sprite pixel position. See this post for more details on this.
So to create sprites for all those fixtures:
void MainScene::CreateSprites() { Viewport& vp = Viewport::Instance(); float32 ptmRatio = vp.GetPTMRatio();
for(int idx = 0; idx < _fixturePositions.size(); idx++) { CCSprite* sprite = CCSprite::create("arrow.png"); // The default sprite anchor is (0.5,0.5). This // is being done to drive home the point. sprite->setAnchorPoint(ccp(0.5,0.5)); AdjustNodeScale(sprite, 1.0, ptmRatio); _fixtureSprites.push_back(sprite); addChild(sprite); } }
You will notice the use of the viewport. That maps the position in the Box2D "meter" space to the pixel space of the screen. You can see a post with more info on that here or by looking at other posts I have done on SO for Box2d (this topic is a common one). You need to scale the sprite size by the physical dimension you want it to be (meters) and the pixel to meter ratio (PTM Ratio):
If you just have a single sprite, you can make its position coincident with the position of the center of the body and lay it on top. If you have multiple sprites, you have to place and rotate each one:
And the result looks like this:
You can find the code for this entire project on github.
Was this helpful?