void MouseCallback(int button, int state, int x, int y) { mx = x; my = y; if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) { Pick(x, y); // this prevents from only grabbing it when the mouse moves MotionCallback(x, y); } else if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { bLeftMouseButtonPressed = true; } if (state == GLUT_UP) { LetGo(); bLeftMouseButtonPressed = false; } }
void GrabBlock(){ MoveServo1_Degrees(0); debounce(1); OC2R = 0x115; debounce(1); MoveServo5_Degrees(5); MoveServo4_Degrees(65); debounce(10); MoveServo4_Degrees(50); debounce(10); MoveServo4_Degrees(5); debounce(50); Grab(); debounce(100); OC4R =0x2F6; debounce(50); MoveServo1_Degrees(90); MoveServo3_Degrees(45); OC2R = 0x115; MoveServo4_Degrees(45); MoveServo3_Degrees(0); MoveServo4_Degrees(0); debounce(200); OC5R =0x485; MoveServo4_Degrees(80); OC2R= 0x250; OC3R =0x250; MoveServo2_Degrees(140); debounce(200); MoveServo4_Degrees(90); LetGo(); debounce(200); OC4R =0x10E; debounce(100); SetTo90(); debounce(50); SetToInitial(); }
void ResetNx() { LetGo(); ReleaseNx(); InitNx(); }
void Pick(int x, int y) { LetGo(); NxRay ray; ViewUnProject(x, y, 0.0f, ray.orig); ViewUnProject(x, y, 1.0f, ray.dir); ray.dir -= ray.orig; ray.dir.normalize(); NxRaycastHit hit; NxShape* closestShape = gScene->raycastClosestShape(ray, NX_ALL_SHAPES, hit); NxVec3 origin = ray.orig; NxVec3 impact = hit.worldImpact; NxReal distRB = NX_MAX_REAL; if (closestShape && closestShape->getActor().isDynamic()) { distRB = (impact.x - origin.x) * (impact.x - origin.x) + (impact.y - origin.y) * (impact.y - origin.y) + (impact.z - origin.z) * (impact.z - origin.z); } NxVec3 hitcloth, hitRecord; NxU32 vertexId; NxReal distCloth, closest= NX_MAX_REAL; NxU32 indexCloth, indexClothVertex; NxCloth **cloths = gScene->getCloths(); for (NxU32 i = 0; i < gScene->getNbCloths(); i++) { if (cloths[i]->raycast(ray, hitcloth, vertexId)) { distCloth = (hitcloth.x - origin.x) * (hitcloth.x - origin.x) + (hitcloth.y - origin.y) * (hitcloth.y - origin.y) + (hitcloth.z - origin.z) * (hitcloth.z - origin.z); if(distCloth < closest) { closest = distCloth; indexCloth = i; indexClothVertex = vertexId; hitRecord = hitcloth; } } } if (distRB > closest) // Pick cloth { gHitCloth = cloths[indexCloth]; gHitClothVertex = indexClothVertex; int hitx, hity; ViewProject(hitRecord, hitx, hity, gMouseDepth); } else if (distRB < closest) // Pick actor { gHitActor = &closestShape->getActor(); gHitActor->wakeUp(); int hitx, hity; gMouseSphere = CreateSphere(hit.worldImpact, 0.1, 1); gMouseSphere->raiseBodyFlag(NX_BF_KINEMATIC); gMouseSphere->raiseActorFlag(NX_AF_DISABLE_COLLISION); ViewProject(hit.worldImpact, hitx, hity, gMouseDepth); NxDistanceJointDesc desc; desc.actor[0] = gMouseSphere; desc.actor[1] = gHitActor; gMouseSphere->getGlobalPose().multiplyByInverseRT(hit.worldImpact, desc.localAnchor[0]); gHitActor->getGlobalPose().multiplyByInverseRT(hit.worldImpact, desc.localAnchor[1]); desc.spring.damper = 1; desc.spring.spring = 200; desc.flags |= NX_DJF_MAX_DISTANCE_ENABLED | NX_DJF_SPRING_ENABLED; NxJoint* joint = gScene->createJoint(desc); gMouseJoint = (NxDistanceJoint*)joint->is(NX_JOINT_DISTANCE); } }