Drawing rope

Post Reply
cobolt_dink
Posts: 72
Joined: Fri Apr 04, 2008 6:07 pm

Drawing rope

Post by cobolt_dink »

Going through the soft body demo code I think I've figured out what I need to change my current rigid body world over to soft/rigid, and tweaking the code, attach a rope to two rigid bodies and drop them. But I honestly can't make heads or tails of the rendering code. The demo seems to go through the physics bodies in a completely different way as well. Though I have seem some code where you can just go through the collision list and upscale to soft body. Still I have no idea how to get the data from the rope body in order to draw it. Any help on the subject would be great, and DirectX related would even be better.

Soft body demos with better comments and clearer code would be a great addition to the wiki. Get rigid bodies working is one thing but the soft bodies seem to be another level.
Nathanael
Posts: 78
Joined: Mon Nov 13, 2006 1:44 am

Re: Drawing rope

Post by Nathanael »

Here's a code snippet to 'parse' soft bodies for rendering purposes,
given your btSoftRigidWorld* as "world":

Code: Select all

/* Dummy 'graphic driver'	*/ 
struct	{
		void DrawPoint(const btVector3&) {}
		void DrawLine(const btVector3&,const btVector3&) {}
		void DrawTriangle(const btVector3&,const btVector3&,const btVector3&) {}
		}			*mygfx=0;
/* Get a ref to the array of softbodies	*/ 
btSoftBodyArray&			softbodies(world->getSoftBodyArray());
/* For each soft bodies					*/ 
for(int i=0;i<softbodies.size();++i)
	{
	btSoftBody*				softbody(softbodies[i]);
	/* Each soft body contain an array of vertices (nodes/particles_mass)	*/ 
	btSoftBody::tNodeArray&	nodes(softbody->m_nodes);
	/* And edges (links/distances constraints)								*/ 
	btSoftBody::tLinkArray&	links(softbody->m_links);
	/* And finally, faces (triangles)											*/ 
	btSoftBody::tFaceArray&	faces(softbody->m_faces);
	/* Then, you can draw vertices...		*/ 
	/* Node::m_x => position				*/ 
	/* Node::m_n => normal (if meaningful)	*/ 
	for(int j=0;j<nodes.size();++j)
		{
		mygfx->DrawPoint(nodes[j].m_x);
		}
	/* Or edges (for ropes)					*/ 
	/* Link::m_n[2] => pointers to nodes	*/ 
	for(int j=0;j<links.size();++j)
		{
		btSoftBody::Node*	node_0=links[j].m_n[0];
		btSoftBody::Node*	node_1=links[j].m_n[1];
		mygfx->DrawLine(node_0->m_x,node_1->m_x);
		/* Or if you need indices...		*/ 
		const int			indices[]={	int(node_0-&nodes[0]),
										int(node_1-&nodes[0])};
		}
	/* And even faces						*/ 
	/* Face::m_n[3] -> pointers to nodes	*/ 
	for(int j=0;j<faces.size();++j)
		{
		btSoftBody::Node*	node_0=faces[j].m_n[0];
		btSoftBody::Node*	node_1=faces[j].m_n[1];
		btSoftBody::Node*	node_2=faces[j].m_n[2];
		mygfx->DrawTriangle(node_0->m_x,node_1->m_x,node_2->m_x);
		/* Or if you need indices...		*/ 
		const int			indices[]={	int(node_0-&nodes[0]),
										int(node_1-&nodes[0]),
										int(node_2-&nodes[0])};
		}
	}
I know that soft bodies source is not especially easy to read, but learning to read code from different sources, format, and language is a very, very valuable skill, really worth trying to acquire. If the compiler can do it, so do you :wink:

Hope it help, do not hesitate to ask more questions, I'll be happy to help.

Nathanael.
cobolt_dink
Posts: 72
Joined: Fri Apr 04, 2008 6:07 pm

Re: Drawing rope

Post by cobolt_dink »

I did see the thread about softbodies and OGRE and kind of figured I had to go through the nodes/faces to render. The number of faces for rope is always zero (but that is probably expected since none are passed in when creating). Nodes as far as I can tell is the anchor point to each piece of the rope. And lines are just groups of nodes that are next to each other.

I wanted to be able to render more then just a line for the rope and actually give it some width and depth. Am I right in assuming if I want to do something like that I will have to just extrapolate from the node data?

If it makes any difference I am still playing with the softbody demo code. Only things I've changes is I added another block in Init_RopeAttach() and put a anchor point to each block. Then in the create rope helper I changed the fixeds parameter to zero as that seems to make the start point not fixed.
Nathanael
Posts: 78
Joined: Mon Nov 13, 2006 1:44 am

Re: Drawing rope

Post by Nathanael »

cobolt_dink wrote:I did see the thread about softbodies and OGRE and kind of figured I had to go through the nodes/faces to render. The number of faces for rope is always zero (but that is probably expected since none are passed in when creating). Nodes as far as I can tell is the anchor point to each piece of the rope. And lines are just groups of nodes that are next to each other.
You have to understand the soft bodies are not graphic objects, they just contains enough data for simulation no more, the drawing part is up to you or the graphic engine that you use.
cobolt_dink wrote: I wanted to be able to render more then just a line for the rope and actually give it some width and depth. Am I right in assuming if I want to do something like that I will have to just extrapolate from the node data?
A rope is a one dimensional object (no twist), if you want to draw something like a scaled mesh for each links, you will have to extract an orthogonal basis, consistent from link to link. or alternatively, you could create a 'ribbon' made of triangles instead, then use theses triangle as basis for drawing.

Hope it help,
Nathanael.
cobolt_dink
Posts: 72
Joined: Fri Apr 04, 2008 6:07 pm

Re: Drawing rope

Post by cobolt_dink »

Thanks, guess billboarded lines it will have to be.
Post Reply