Skip to content

Instantly share code, notes, and snippets.

@blewert
Created February 15, 2019 09:01
Show Gist options
  • Save blewert/72c9e443812cb26b8a28b75d2b110886 to your computer and use it in GitHub Desktop.
Save blewert/72c9e443812cb26b8a28b75d2b110886 to your computer and use it in GitHub Desktop.
A clock in PhysX using revolute joints + motors
//Make the minute and hour hands, the shapes
Capsule* minuteHand = new Capsule(PxTransform(PxVec3(0, 8, 0)), PxVec2(0.1f, 2));
Capsule* hourHand = new Capsule(PxTransform(PxVec3(0, 8, 0)), PxVec2(0.1f, 1));
//Turn off gravity
hourHand->Get()->setActorFlag(PxActorFlag::eDISABLE_GRAVITY, true);
minuteHand->Get()->setActorFlag(PxActorFlag::eDISABLE_GRAVITY, true);
//Add them to the scene
Add(hourHand);
Add(minuteHand);
//Set the local position to by offset by the length of the capsule (so the pivot point
//for rotation is the bottom, not the centre) and rotate this by 90 deg on the y axis
hourHand->GetShape(0)->setLocalPose(PxTransform(PxVec3(0, 0, 1), PxQuat(PxPiDivTwo, PxVec3(0, 1, 0))));
minuteHand->GetShape(0)->setLocalPose(PxTransform(PxVec3(0, 0, 2), PxQuat(PxPiDivTwo, PxVec3(0, 1, 0))));
//The first frame: connect joint to fixed point (0, 8, 0), and the joint rotation axis will
//90 degrees on the y axis too
PxTransform frame0 = PxTransform(PxVec3(0, 8, 0), PxQuat(PxPiDivTwo, PxVec3(0, 1, 0)));
//The second frame: the offset point / distance to rotate around.. we dont want to orbit sooo
//lets leave this as (0, 0, 0) - rotate AROUND the origin point
PxTransform frame1 = PxTransform(PxVec3(0, 0, 0));
//Make the joints with the frames
RevoluteJoint* jointHour = new RevoluteJoint(NULL, frame0, hourHand, frame1);
RevoluteJoint* jointMinute = new RevoluteJoint(NULL, frame0, minuteHand, frame1);
//Set both of the motors to run at the same speed
jointHour->DriveVelocity(PxPi*5);
jointMinute->DriveVelocity(PxPi*5);
//But set the hour joint to be 60 times faster than the second hand
jointHour->SetGearRatio(60);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment