<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-gb">
	<link rel="self" type="application/atom+xml" href="https://pybullet.org/Bullet/phpBB3/app.php/feed/topic/9582" />

	<title>Real-Time Physics Simulation Forum</title>
	
	<link href="https://pybullet.org/Bullet/phpBB3/index.php" />
	<updated>2013-11-27T11:02:58+00:00</updated>

	<author><name><![CDATA[Real-Time Physics Simulation Forum]]></name></author>
	<id>https://pybullet.org/Bullet/phpBB3/app.php/feed/topic/9582</id>

		<entry>
		<author><name><![CDATA[stevet]]></name></author>
		<updated>2013-11-27T11:02:58+00:00</updated>

		<published>2013-11-27T11:02:58+00:00</published>
		<id>https://pybullet.org/Bullet/phpBB3/viewtopic.php?p=32261#p32261</id>
		<link href="https://pybullet.org/Bullet/phpBB3/viewtopic.php?p=32261#p32261"/>
		<title type="html"><![CDATA[btGeneric6DofConstraint : rotation of local transforms ?]]></title>

		
		<content type="html" xml:base="https://pybullet.org/Bullet/phpBB3/viewtopic.php?p=32261#p32261"><![CDATA[
Hello everyone.<br>Let me first thank the authors for this great engine that I'm currently<br>discovering, and then apologize for the long post.<br><br>I'm trying to create a chain of constrained rigid bodies, given a definition<br>inputed in a text file.<br><br>For instance if my definition file looks like this:<br><div class="codebox"><p>Code: </p><pre><code>ROOT ID 0 OFFSET 0 1 0JOINT ID 1PARENT ID 0OFFSET -1 0 0MINANGLES 0 0 0MAXANGLES 0 0 0JOINT ID 2PARENT ID 1OFFSET 1 1 0MINANGLES 0 0 0MAXANGLES 0 0 0</code></pre></div>As a result I obtain something like this :<div class="inline-attachment"><dl class="file"><dt class="attach-image"><img src="https://pybullet.org/Bullet/phpBB3/download/file.php?id=1147" class="postimage" alt="good.png" onclick="viewableArea(this);" /></dt></dl></div>Each joint except for the root has a parent, and an offset from this parent that defines<br>the length of the next rigid body to create. MINANGLES and MAXANGLES determine, as their name suggest,<br>the angular limits of a btGeneric6DofConstraint I want to create.<br><br>The code that follows successfully creates the rigid bodies, but my problems appear when I create the constraints.<br><br>I would like to set the rotations of the constraints so that the initial world position and orientation of each<br>rigid body matches with initial angle values equal to 0 for X, Y and Z.<br><br>Working code:<div class="codebox"><p>Code: </p><pre><code>void BuildOneJoint2(const btTransform&amp; currentTransform, const btVector3&amp; origin, const joint_def_t&amp; joint, btDynamicsWorld* m_ownerWorld, btCollisionShape** m_shapes, btRigidBody** m_bodies, btTypedConstraint** m_joints, unsigned int&amp; id, int parentId){const btVector3 vUp(0,1,0);const btVector3 vForward(1,0,0);const btVector3 vBack(0,0,1);// retrieving joint offset from parent                // definitions from text files are contained in struct joint btVector3 offSetVector(btScalar(joint.offset[0]), btScalar(joint.offset[1]),  btScalar(joint.offset[2]));btVector3 parentOffset;//computing radius and length of capsule; radius is more or less equal to offsetbtScalar radius = offSetVector.length() * btScalar(0.1); btScalar length = offSetVector.length() - 2 * radius;if(parentId == 0){// if current joint is root, for now juste create a small capsule representing rootbtVector3 rootOffSet(offSetVector); rootOffSet.normalize();parentOffset = rootOffSet * btScalar(0.05);offSetVector += parentOffset;}else{parentOffset = btVector3(btScalar(joint.parent-&gt;offset[0]), btScalar(joint.parent-&gt;offset[1]),  btScalar(joint.parent-&gt;offset[2]));}// Setup the geometry//The total height of a btCapsuleShape is height+2*radiusm_shapes[id] = new btCapsuleShapeX(radius, length - btScalar(0.1));// Setup the rigid bodybtTransform offset;offset.setIdentity();  offset.setOrigin(origin + offSetVector / 2);// computing rotation necessary for capsulebtVector3 vToBone(offSetVector);vToBone.normalize();btVector3 vAxis = vToBone.cross(vForward);btScalar dot = vToBone.dot(vForward);btScalar angle = acos(vToBone.dot(vForward));if(angle != 0){offset.setRotation(btQuaternion(vBack, angle));}m_bodies[id] = localCreateRigidBody(m_ownerWorld, btScalar(1.), offset, m_shapes[id]);btVector3 newOrigin(origin + offSetVector);btTransform newTransform; newTransform.setIdentity();newTransform.setRotation(currentTransform.getRotation() * offset.getRotation());newTransform.setOrigin(newOrigin);int newParentId = id;// method is recursive, call it with childrenfor(unsigned int i=0; i&lt; joint.nbChildren_; ++i){  BuildOneJoint2(newTransform, newOrigin, *(joint.children[i]), m_ownerWorld, m_shapes, m_bodies, m_joints, ++id, newParentId);}}</code></pre></div>The code for <div class="codebox"><p>Code: </p><pre><code> localCreateRigidBody </code></pre></div> is taken from the demos :<div class="codebox"><p>Code: </p><pre><code>btRigidBody* localCreateRigidBody (btDynamicsWorld* m_ownerWorld, btScalar mass, const btTransform&amp; startTransform, btCollisionShape* shape){btVector3 localInertia(0,0,0);if (mass != 0.f) // is dynamicshape-&gt;calculateLocalInertia(mass,localInertia);btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);btRigidBody::btRigidBodyConstructionInfo rbInfo(mass, myMotionState, shape, localInertia);btRigidBody* body = new btRigidBody(rbInfo);m_ownerWorld-&gt;addRigidBody(body);return body;}</code></pre></div> <br><br>Considering the previous code, I now try to create my constraints:<br><div class="codebox"><p>Code: </p><pre><code>btGeneric6DofConstraint* constraint;btTransform localA; localA.setIdentity();//localA.setRotation(???);localA.setOrigin(btVector3(parentOffset.length() / 2, btScalar(0.), btScalar(0.)));btTransform localB; localB.setIdentity();//localB.setRotation(???);localB.setOrigin(btVector3(-length / 2, btScalar(0.), btScalar(0.)));constraint =  new btGeneric6DofConstraint(*m_bodies[parentId], *m_bodies[id], localA, localB, false);m_joints[id] = constraint;constraint-&gt;setDbgDrawSize(0.5f);btVector3 limitsDown(btScalar(joint.minAngleValues[0] * DegreesToRadians),                                 btScalar(joint.minAngleValues[1] * DegreesToRadians),                                 btScalar(joint.minAngleValues[2] * DegreesToRadians));btVector3 limitsUp(btScalar(joint.maxAngleValues[0] * DegreesToRadians),                             btScalar(joint.maxAngleValues[1] * DegreesToRadians),                             btScalar(joint.maxAngleValues[2] * DegreesToRadians));constraint-&gt;setAngularLowerLimit(limitsDown);constraint-&gt;setAngularUpperLimit(limitsUp);m_ownerWorld-&gt;addConstraint(m_joints[id], true);</code></pre></div>As I said,  I would like to set the rotations of the constraints so that the initial world position and orientation of each<br>rigid body matches with initial angle values equal to 0 for X, Y and Z for each constraint.<br><br>I've tried several possibilities without achieving the desired goal, and adding constraints gives me something like that in the end:<br><div class="inline-attachment"><dl class="file"><dt class="attach-image"><img src="https://pybullet.org/Bullet/phpBB3/download/file.php?id=1148" class="postimage" alt="bad.png" onclick="viewableArea(this);" /></dt></dl></div>It is not really easy to write on a forum, and I hope I am clear enough.<br>Thank you very much for your time,<br><br>Steve<p>Statistics: Posted by <a href="https://pybullet.org/Bullet/phpBB3/memberlist.php?mode=viewprofile&amp;u=10582">stevet</a> — Wed Nov 27, 2013 11:02 am</p><hr />
]]></content>
	</entry>
	</feed>
