how to draw a line?

Hi im sorta new to the whole box2d idea and having some trouble. What im trying to do is when the person puts their finger and moves around, a b2body will form in the shape from when the person first tap til the person lets go. So far i did a work around which is every time move is made i just spawn lots of small circles as it moves but thats not the effect time trying to go for.

Also can someone tell me how to keep something in a constant velocity every time an object bounce... i've played around with the restitution and friction but after a while the object either stops moving or goes insanely fast. Any tips on that too?

All thanks in advance!!
I have done something similar by just drawing a line while the finger is still down, and then creating a box when they let go.
But if you want the box to be solid, and moving around, and also changing shape, you will find it more difficult.
You could try destroying the body and recreating it in a new position every time the finger moves, and see how that works out.
Maybe you can use edges (like very thin boxes). Here's a code that allows the user to draw static edges on the screen (using Cocos2D):

Code: Select all

- (void)ccTouchMoved:(UITouch*)touch withEvent:(UIEvent *)event
{
CGPoint start = [touch locationInView: [touch view]];   
start = [[CCDirector sharedDirector] convertToGL: start];
CGPoint end = [touch previousLocationInView:[touch view]];
end = [[CCDirector sharedDirector] convertToGL:end];
   
float distance = ccpDistance(start, end);
if (distance > 1)
{
   int d = (int)distance;
      
   b2Vec2 s(start.x/PTM_RATIO, start.y/PTM_RATIO);
   b2Vec2 e(end.x/PTM_RATIO, end.y/PTM_RATIO);
      
   b2BodyDef bd;
   bd.type = b2_staticBody;
   bd.position.Set(0, 0);
   b2Body* body = world->CreateBody(&bd);
      
   for (int i = 0; i < d; i++)
   {         
      b2PolygonShape shape;
      shape.SetAsEdge(b2Vec2(s.x, s.y), b2Vec2(e.x, e.y));
      body->CreateFixture(&shape, 0.0f);
   }
}
}


If you want the user to draw dynamic bodies you'll need to keep the coordinates in an array and draw the body when the touch ends.

As for your second question, I think it has already been discussed before: viewtopic.php?f=3&t=6790
Wow this is perfect thanks soooo much yyy!!!
I also need to draw a line that box2d bodies can interact with, but the "static edge" solution posted by yyy above causes performance issues when too much drawing is done.

Is there any other way to draw a line that can interact with box2d bodies?

Thanks
nice 😀 thanks for these tips.
That was truly great help. Thanks anyway.

App Developers Melbourne
Is there any way to increase the thickness of the line?
I'm using the same code. It's working perfect. But if we run it in the device the framerate is going very low. And the game is getting crashed. How to optimize this?

Thanks
Where does it crash, that would be the first thing to find out...
Just looking at the code above, I can't see why it has that loop there, I mean the contents of the loop will be the same every time. If you used that code exactly, I think you will be making way too many fixtures.

Original thread: https://box2d.org/forum/viewtopic.php?t=7115