When to use convert to nodespace or convert to worldspace

hey i am confused about converttonodespace and converttoworldspace please tell me

Let me explain a bit,
Put a paper P(Sprite/Node) on a table T(Layer), now put a cup C(Node/Sprite) on paper P, notice we have a parent-child relationship(Hierarchy).
Now move P to any random position. Table has a coordinate system of its own which is dependent on its positioning in Room R(i call it SCENE), and defaulted to vec2(0,0), similarly paper P also have its own coordinate system and so does cup C. Now for some obvious reasons if you want to find out the positioning of cup C against table T then you would do T->convertToNodeSpace(C->getPosition()), Notice here its not mandatory for T to be grand-parent of cup C to find out its position according to its coordinate, any node can find out other nodes children’s position according to its coordinate. Now if some one is generous enough and put a chart(CH) between paper and table, and we need to find the coordinates of cup C against room R**, then i would do **this->convertToWorldSpace(c->getPosition())**. But for **convertToWorldSpace()** to work its mandetory that **T**,**P**,or **C** be in same room **R`

Please note i haven’t considered anchor point in order to keep example simple ans short. Also i haven’t considered coordinate system of cocos and real device. Corrections are more then welcome.

Thanks

12 Likes

Plus 1000. This is an excellent explanation!

2 Likes

Thank you Lazy_Gamer, for your kind replay well

HOW
this->img intersect this->layer->img ( where layer=768*10000) o …convert to nodespace or convert to worldspace Please Tell me

Can you explain a little bit of your requirements here, excuse me but i don’t understand your question above. Do you want to know if a Image i1 (this--->i1) intersects with another image i2(this--->layer/node/sprite--->i2)?

thanks for replay Lazy_Gamer

i actually want to intersect with two images

ex img_1 that is the child of a scene
img_2 That is the child of a layer which sized (768*10000)

now i want to intersect img_1 and img_2

@game_zone check if this works out for you, i haven’t tested it, its just over top of my head

Sprite *i1, *i2;
Layer *layer;
this->addChild(i1);
this->addChild(layer);
layer->addChild(i2);
///////////// Finding if i1 intersects with i2 ?
////
Vec2 i1Position = i1->getPosition();
Vec2 i2Position = i2->getParent()->getParent()->convertToNodeSpace(i2->getPosition()); // will return position in this(Layer's) coordinate system

Rect i1Rect = i1->getBoundingBox();
Rect i2Rect = i2->getBoundingBox(); // gettig untransformed Rect of i2.

i2Rect = Rect(i2Position.x - i2Rect.size.width/2, i2Position.y - i2Rect.size.height/2, i2Position.x + i2Rect.size.width/2, i2Position.y + i2Rect.size.height/2); // Manually transforming coordinates, dun know ifcocos2d-x provide any inbuild function for it?    

if(i1Rect.intersectsRect(i2Rect)){
       // items collided...
}

EDIT:- 1 remove abs() from code, it will make things behave unexpected for sprites out of view port.

Thanks