B
B
BonBon Slick2019-12-22 21:52:42
Cocos2d
BonBon Slick, 2019-12-22 21:52:42

Different coordinate systems for events and rendering?

If you put a listener on a click and display the coordinates of the click

auto touchListener = EventListenerTouchOneByOne::create();
  touchListener->setSwallowTouches(false);
  touchListener->onTouchBegan = CC_CALLBACK_2(HelloWorldScene::onTouchBegan, this);
  Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener, this);

Then the logs and the stack trace will show that the initial coordinates are the upper left corner x=0, y = 0.
However, to draw any element
DrawNode* circleInner = DrawNode::create();
  circleInner->drawDot(
    Vec2(0, 0),
    55,
    Color4F::BLUE
  );
  this->addChild(circleInner);

The position vector x=0, y=0 will be the lower left corner.
As a result, false data
float distSquared = touch->getLocationInView().getDistance(circleInner->getPosition());
  CCLOG(" distSquared %f", distSquared);

Why is this and how to fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BonBon Slick, 2019-12-23
@BonBonSlick

A clumsy solution a day later found here

auto height = Director::getInstance()->getOpenGLView()->getFrameSize().height;
location.y = height - location.y;

Therefore, the check will be
auto height = Director::getInstance()->getOpenGLView()->getFrameSize().height;
  auto width = Director::getInstance()->getOpenGLView()->getFrameSize().width;

circleInner->getBoundingBox().containsPoint(
    Vec2(
      width - touch->getLocationInView().x,
      height - touch->getLocationInView().y
    )
  )

Dock is clumsy.
Additionally
https://discuss.cocos2d-x.org/t/incorrect-y-coordi...
https://discuss.cocos2d-x.org/t/mouse-and-touch-re...
https:/ /docs.cocos2d-x.org/creator/manual/en/ui/mu...
https://discuss.cocos2d-x.org/t/multi-resolution-c...
https://discuss.cocos2d -x.org/t/how-to-get-absolut...
https://discuss.cocos2d-x.org/t/touch-returns-inco...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question