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
239 Views Asked by user3050371 At
1
There are 1 best solutions below
Related Questions in COCOS2D-IPHONE
- Loop an animation programatically in Cocos2d, like chaining the animation to itself in Spritebuilder
- timing issues while creating replay of game (ghost for racing)
- Cocos2D 2.x: Running CCWave action makes sprite disappear
- Cocos2d : Shooting a ball in the direction of the mouse click/Touch
- I want to use cocos2d-iphone 2.0 to draw a line
- Cocos2d Node not accepting Touches
- UIView cocos2d Support
- trouble getting collision point
- How do we enumerate and modify positions array of CCsprite inside Update method?
- CCActionDelay not working as expected
- How to perform action on "Touch inside" on button in Cocos2d
- CCLabelTTF universal device issue
- Sharing Property information between classes in Obj C
- Coco2d 2.1 and Xcode 7 iOS 9 crash ccShader
- How can I create a pop-up window using Cocos2d, SpriteBuilder and Objective-c?
Related Questions in ANCHOR
- Anchoring controls within a panel
- PHP Header Location - redirect to URL with HTML anchor
- change anchor href with jquery
- Two different anchors to expand/collapse the same div
- Python Sphinx anchor on arbitrary line
- JQuery toggle a OL list with headings
- Anchor tag not working
- Upload multiple images on anchor click
- How to define custom shape, as click-able area for anchor tag? (cross-browser)
- DataGridView anchors
- angularjs what is the best way to rewrite span <span> as anchor <a>
- Jump to a specific entry not working for several browser
- Anchor tags not working
- Reading URL fragments on page load
- How to make PDF from GitHub open in new tab?
Related Questions in BOX2D
- timing issues while creating replay of game (ghost for racing)
- Cocos2d : Shooting a ball in the direction of the mouse click/Touch
- adobe pro cc (AS3) not recognizing box2d classpath
- I have an Array of 2 objects, when I use a for loop, both of the object gets the change that only one of them should have
- Libgdx Android game Crash after short time
- Set up wraparound effect in libGDX with Box2D
- Java/Android - libGDX/box2d - body has low velocity
- AndEngine remove physics body from screen
- libGDX Stage input handling
- Andengine Collision Not detected
- Android libgdx scene2d - button disappears
- SKEffectNode shader bug
- Ambiguous reference to b2FilterData
- Delete all box 2D bodies from world in libGDX
- Which Box2D-like physics engine parameters need pixel conversion?
Related Questions in COORDINATE
- How to get the position of a dynamically added view relative to its parent
- What is this algorithm mapping coordinates to numbers called?
- LibGDX Camera and Coordinates
- How to make a GET request with latitude and longitude paired a structure using AFNetworking?
- how to output coordinate values of feature class to text file
- Coordinate API OAuth2 authentication with refresh token
- R: Need to populate a matrix with counts of coordinates (from data frame with list of coordinates, some of which are duplicates)
- Detect click in a diamond
- Generate a random point in space (x, y, z) with a boundary
- Confused with the anchor and local node space origin of the body in Box2D
- algorithm - hit detection of rectangle inside rotated rectangle
- Access to QR code coordinate in ZXing on Android
- How to continuously update MKAnnotation coordinates on the MKMap
- Finding orientation of a point with respect to another point in xy plane
- Converting a coordinate from one space to another
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)
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?
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?