Draw straight line between 2 point OntouchMoved

Hi ,
I want to draw straight line between 2 point OnTouchMoved Event .
I tried With Following

touchListener->onTouchBegan = [&](Touch* touch, Event* event)
        {
            lastTouchPos =  drawNode->convertTouchToNodeSpace(touch);
         
        };
      

        
        touchListener->onTouchMoved = [&](Touch* touch, Event* event)
        {
            Vec2 touchPos = drawNode->convertTouchToNodeSpace(touch);
          
            float distance = lastTouchPos.distance(touchPos);
            
          

            drawNode->setLineWidth(30);
            drawNode->drawLine(lastTouchPos, touches, Color4F(Color3B::BLUE));
            lastTouchPos = touchPos;
            lastRadius = radius;
            
        };

This draw curve type line ontouch moved Not straight line .

Any help ???

A little unconventional but would the logic behind a ray cast help: http://cocos2d-x.org/docs/programmers-guide/physics/index.html

drawLine does draw a straight line between 2 points. It will look curved because your code draws lots of small lines that together look curved.

What exactly are you trying to do? Once the touch has ended, should there be only straight line, which is straight? Or should the path of the touch look like many lots of straight lines?

1 Like

By onTouchBegin save firstPosition,
By onTouchMove delete last line, and draw from firstPosition.

You could clear the DrawNode every time touch moves before you draw the line. It will clear the entire DrawNode, but if the line is the only thing you are drawing then that shouldn’t matter.

touchListener->onTouchMoved = [&](Touch* touch, Event* event)
{
    Vec2 touchPos = drawNode->convertTouchToNodeSpace(touch);
    drawNode->clear();
    drawNode->drawLine(lastTouchPos, touchPos, Color4F(Color3B::BLUE));
};

Yes @grimfate . I think Its great Idea but i already done that .For my Work this is not good because We have dots in grid like structure and I want to connects all when touch move from one dot to another.
drawnode->clear() clears everything.

I want to connect numbers when we move as shown above.

Anyway thanks for your reply .

Hey @grimfate You are right I will try to do this.

Thanks

You could use multiple DrawNodes. Use the first DrawNode for drawing the line and clear it whenever the touch moves, and use the second DrawNode for drawing that you don’t want cleared.

If you want the line to remain after the touch ends, draw the line onto the second DrawNode when the touch ends and then clear the first DrawNode.

Great @grimfate I did it with 2 draw node.

Thanks.