Box2D Forums

It is currently Sat May 25, 2013 1:09 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: how to draw a line?
PostPosted: Sat Jun 11, 2011 11:09 pm 
Offline

Joined: Sat Jun 11, 2011 10:57 pm
Posts: 18
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!!


Top
 Profile  
 
 Post subject: Re: how to draw a line?
PostPosted: Sun Jun 12, 2011 2:19 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
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.


Top
 Profile  
 
 Post subject: Re: how to draw a line?
PostPosted: Sun Jun 12, 2011 5:22 am 
Offline

Joined: Tue Jan 18, 2011 3:27 pm
Posts: 38
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:
- (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


Top
 Profile  
 
 Post subject: Re: how to draw a line?
PostPosted: Sun Jun 12, 2011 10:05 am 
Offline

Joined: Sat Jun 11, 2011 10:57 pm
Posts: 18
Wow this is perfect thanks soooo much yyy!!!


Top
 Profile  
 
 Post subject: Re: how to draw a line?
PostPosted: Mon Oct 17, 2011 2:51 pm 
Offline

Joined: Sat Oct 15, 2011 12:12 pm
Posts: 2
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


Top
 Profile  
 
 Post subject: Re: how to draw a line?
PostPosted: Wed Nov 09, 2011 9:00 am 
Offline

Joined: Mon Nov 07, 2011 1:41 am
Posts: 3
nice :D thanks for these tips.


Top
 Profile  
 
 Post subject: Re: how to draw a line?
PostPosted: Sun Feb 19, 2012 1:35 pm 
Offline

Joined: Sun Feb 19, 2012 1:20 pm
Posts: 4
That was truly great help. Thanks anyway.

App Developers Melbourne


Last edited by Alstair on Thu Mar 01, 2012 10:57 am, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: how to draw a line?
PostPosted: Sun Feb 19, 2012 6:59 pm 
Offline

Joined: Sat Oct 15, 2011 12:12 pm
Posts: 2
Is there any way to increase the thickness of the line?


Top
 Profile  
 
 Post subject: Re: how to draw a line?
PostPosted: Mon Apr 16, 2012 11:21 pm 
Offline

Joined: Mon May 18, 2009 9:45 pm
Posts: 1
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


Top
 Profile  
 
 Post subject: Re: how to draw a line?
PostPosted: Tue Apr 17, 2012 3:06 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
cron
Powered by phpBB® Forum Software © phpBB Group