void VRPhysics::setGravity(OSG::Vec3f v) { Lock lock(mtx()); if (body) body->setGravity(btVector3 (v.x(),v.y(),v.z())); }
void LuaCore::setupRsFunctionsAndTw(QTreeWidget* tw) { int top; std::string namespc; // namespace (with '.' at the end) // two namespaces tw->setColumnCount(2); // setup tree widget (after ColumnCount was set!) // no headers to not to confuse the user tw->setHeaderHidden(true); // make it big enough to show the full functionnames tw->setColumnWidth(0,250); // second col is only a "container" for the paste value, therefore it needs no width tw->setColumnWidth(1,0); // we need to mess around with lua RsStackMutex mtx(_mutex); /******* LOCKED MUTEX *****/ // rs namespace namespc = "rs."; QTreeWidgetItem* rs = new QTreeWidgetItem(tw); rs->setText(0, QString::fromStdString(namespc)); rs->setText(1, QString::fromStdString(namespc)); lua_newtable(L); top = lua_gettop(L); addFunctionToLuaAndTw(top, namespc, rs, rs_clear, "clear()", QObject::tr("clears the output")); addFunctionToLuaAndTw(top, namespc, rs, rs_print, "print()", QObject::tr("prints to output")); addFunctionToLuaAndTw(top, namespc, rs, rs_luaFolder, "luaFolder()", QObject::tr("path to Lua4RS folder")); lua_setglobal(L, "rs"); // peers namespace namespc = "peers."; QTreeWidgetItem* peers = new QTreeWidgetItem(tw); peers->setText(0, QString::fromStdString(namespc)); peers->setText(1, QString::fromStdString(namespc)); lua_newtable(L); top = lua_gettop(L); addFunctionToLuaAndTw(top, namespc, peers, peers_getOwnId, "getOwnId()", QObject::tr("returns own SSL id (peer id)")); addFunctionToLuaAndTw(top, namespc, peers, peers_getOnlineList, "getOnlineList()", QObject::tr("returns list of online friends (SSL id)")); addFunctionToLuaAndTw(top, namespc, peers, peers_getFriendList, "getFriendList()", QObject::tr("returns list of all friends (SSL id)")); addFunctionToLuaAndTw(top, namespc, peers, peers_getPeerCount, "getPeerCount()", QObject::tr("returns number of all friends and online friends")); addFunctionToLuaAndTw(top, namespc, peers, peers_isFriend, "isFriend()", QObject::tr("returns if a peer is a friend")); addFunctionToLuaAndTw(top, namespc, peers, peers_isGPGAccepted, "isGPGAccepted()", QObject::tr("returns is a PGP key is accepted")); addFunctionToLuaAndTw(top, namespc, peers, peers_isOnline, "isOnline()", QObject::tr("returns if a peer is online")); addFunctionToLuaAndTw(top, namespc, peers, peers_getGPGName, "getGPGName()", QObject::tr("returns the PGP name for a given PGP id")); addFunctionToLuaAndTw(top, namespc, peers, peers_getPeerName, "getPeerName()", QObject::tr("returns the name for a given SSL/PGP id")); addFunctionToLuaAndTw(top, namespc, peers, peers_getPeerDetails, "getPeerDetails()", QObject::tr("returns peer details as a table for a given SSL id")); addFunctionToLuaAndTw(top, namespc, peers, peers_getGPGOwnId, "getGPGOwnId()", QObject::tr("returns own PGP id")); addFunctionToLuaAndTw(top, namespc, peers, peers_getGPGId, "getGPGId()", QObject::tr("returns the PGP id for a given SSL/PGP id")); //groups addFunctionToLuaAndTw(top, namespc, peers, peers_addGroup, "addGroup()", QObject::tr("creates a new group")); addFunctionToLuaAndTw(top, namespc, peers, peers_editGroup, "editGroup()", QObject::tr("edits an existing group")); addFunctionToLuaAndTw(top, namespc, peers, peers_removeGroup, "removeGroup()", QObject::tr("removes the group with the given groupd id")); addFunctionToLuaAndTw(top, namespc, peers, peers_getGroupInfo, "getGroupInfo()", QObject::tr("returns group info for a given group id")); addFunctionToLuaAndTw(top, namespc, peers, peers_getGroupInfoList, "getGroupInfoList()", QObject::tr("returns an array with all groups and their group infos")); addFunctionToLuaAndTw(top, namespc, peers, peers_assignPeerToGroup, "assignPeerToGroup()", QObject::tr("returns the PGP id for a given SSL/PGP id")); lua_setglobal(L, "peers"); // server config namespc = "config."; QTreeWidgetItem* config = new QTreeWidgetItem(tw); config->setText(0, QString::fromStdString(namespc)); config->setText(1, QString::fromStdString(namespc)); lua_newtable(L); top = lua_gettop(L); addFunctionToLuaAndTw(top, namespc, config, config_getOperatingMode, "getOperatingMode()", QObject::tr("returns the current operation mode as int and string")); addFunctionToLuaAndTw(top, namespc, config, config_setOperatingMode, "setOperatingMode()", QObject::tr("sets the openration mode (takes int or string)")); addFunctionToLuaAndTw(top, namespc, config, config_setMaxDataRates, "setMaxDataRates()", QObject::tr("sets max down-/upload bandwidth in kB")); addFunctionToLuaAndTw(top, namespc, config, config_getMaxDataRates, "getMaxDataRates()", QObject::tr("gets max down-/upload bandwidth in kB")); addFunctionToLuaAndTw(top, namespc, config, config_getCurrentDataRates, "getCurrentDataRates()",QObject::tr("gets current down-/upload bandwidth in kB")); lua_setglobal(L, "config"); // discovery namespc = "disc."; QTreeWidgetItem* discovery = new QTreeWidgetItem(tw); discovery->setText(0, QString::fromStdString(namespc)); discovery->setText(1, QString::fromStdString(namespc)); lua_newtable(L); top = lua_gettop(L); addFunctionToLuaAndTw(top, namespc, discovery, disc_getDiscFriends, "getDiscFriends()", QObject::tr("gets discovery infos for a SSLID")); addFunctionToLuaAndTw(top, namespc, discovery, disc_getDiscPgpFriends, "getDiscPgpFriends()", QObject::tr("gets discovery infos for a PGPID")); addFunctionToLuaAndTw(top, namespc, discovery, disc_getPeerVersion, "getPeerVersion()", QObject::tr("gets RS version from a given peer")); addFunctionToLuaAndTw(top, namespc, discovery, disc_getWaitingDiscCount,"getWaitingDiscCount()",QObject::tr("gets current pending discovery packets (down und up)")); lua_setglobal(L, "disc"); // chat namespc = "chat."; QTreeWidgetItem* chat = new QTreeWidgetItem(tw); chat->setText(0, QString::fromStdString(namespc)); chat->setText(1, QString::fromStdString(namespc)); lua_newtable(L); top = lua_gettop(L); addFunctionToLuaAndTw(top, namespc, chat, chat_sendChat, "sendChat()", QObject::tr("send a chat message (ChatId, msg)")); lua_setglobal(L, "chat"); // files namespc = "files."; QTreeWidgetItem* files = new QTreeWidgetItem(tw); files->setText(0, QString::fromStdString(namespc)); files->setText(1, QString::fromStdString(namespc)); lua_newtable(L); top = lua_gettop(L); addFunctionToLuaAndTw(top, namespc, files, file_fileRequest, "fileRequest()", QObject::tr("request a download (params: *name*, *hash*, *size*")); lua_setglobal(L, "files"); }
btPairCachingGhostObject* VRPhysics::getGhostBody() { Lock lock(mtx()); return ghost_body; }
btCollisionShape* VRPhysics::getCollisionShape() { Lock lock(mtx()); return shape; }
btCollisionObject* VRPhysics::getCollisionObject() { Lock lock(mtx()); return ghost ? (btCollisionObject*)ghost_body : (btCollisionObject*)body; }
btRigidBody* VRPhysics::getRigidBody() { Lock lock(mtx()); return body; }
void VRPhysics::addConstantTorque(OSG::Vec3f i) { Lock lock(mtx()); constantTorque = toBtVector3(i); }
void VRPhysics::setTransformation(btTransform t) { if (body == 0) return; Lock lock(mtx()); body->setWorldTransform(t); }
void VRPhysics::addForce(OSG::Vec3f i) { if (body == 0 || mass == 0) return; Lock lock(mtx()); body->applyForce(toBtVector3(i), btVector3(0.0,0.0,0.0)); }
void VRPhysics::addTorque(OSG::Vec3f i) { if (body == 0 || mass == 0) return; Lock lock(mtx()); body->applyTorque(toBtVector3(i)); }
void VRPhysics::applyImpulse(OSG::Vec3f i) { if (body == 0) return; if (mass == 0) return; Lock lock(mtx()); body->setLinearVelocity(btVector3(i[0]/mass, i[1]/mass, i[2]/mass)); }
void VRPhysics::update() { OSG::VRScene* scene = OSG::VRSceneManager::getCurrent(); if (scene == 0) return; if (world == 0) world = scene->bltWorld(); if (world == 0) return; Lock lock(mtx()); scene->unphysicalize(vr_obj); if (body != 0) { for (auto j : joints) { if (j.second->btJoint != 0) { VRPhysicsJoint* joint = j.second; world->removeConstraint(joint->btJoint); delete joint->btJoint; joint->btJoint = 0; } } for (auto j : joints2) { if (j.first->joints.count(this) == 0) continue; VRPhysicsJoint* joint = j.first->joints[this]; if (joint->btJoint != 0) { world->removeConstraint(joint->btJoint); delete joint->btJoint; joint->btJoint = 0; } } world->removeRigidBody(body); delete body; body = 0; } if (ghost_body != 0) { world->removeCollisionObject(ghost_body); delete ghost_body; ghost_body = 0; } if (shape != 0) { delete shape; shape = 0; } if (motionState != 0) { delete motionState; motionState = 0; } if (!physicalized) return; motionState = new btDefaultMotionState(fromVRTransform( vr_obj, scale )); if (physicsShape == "Box") shape = getBoxShape(); if (physicsShape == "Sphere") shape = getSphereShape(); if (physicsShape == "Convex") shape = getConvexShape(); if (physicsShape == "Concave") shape = getConcaveShape(); if (shape == 0) return; btVector3 inertiaVector(0,0,0); float _mass = mass; if (!dynamic) _mass = 0; if (_mass != 0) shape->calculateLocalInertia(_mass, inertiaVector); btRigidBody::btRigidBodyConstructionInfo rbInfo( _mass, motionState, shape, inertiaVector ); if (ghost) { ghost_body = new btPairCachingGhostObject(); ghost_body->setCollisionShape( shape ); ghost_body->setUserPointer( vr_obj ); ghost_body->setCollisionFlags( btCollisionObject::CF_KINEMATIC_OBJECT | btCollisionObject::CF_NO_CONTACT_RESPONSE ); world->addCollisionObject(ghost_body, collisionGroup, collisionMask); } else { body = new btRigidBody(rbInfo); body->setActivationState(activation_mode); world->addRigidBody(body, collisionGroup, collisionMask); } scene->physicalize(vr_obj); updateConstraints(); }
vector<VRCollision> VRPhysics::getCollisions() { Lock lock(mtx()); vector<VRCollision> res; if (!physicalized) return res; if (!ghost) { int numManifolds = world->getDispatcher()->getNumManifolds(); for (int i=0;i<numManifolds;i++) { btPersistentManifold* contactManifold = world->getDispatcher()->getManifoldByIndexInternal(i); //btCollisionObject* obA = (btCollisionObject*)(contactManifold->getBody0()); //btCollisionObject* obB = (btCollisionObject*)(contactManifold->getBody1()); int numContacts = contactManifold->getNumContacts(); for (int j=0;j<numContacts;j++) { btManifoldPoint& pt = contactManifold->getContactPoint(j); if (pt.getDistance()<0.f) { VRCollision c; c.obj1 = vr_obj; // c.obj2 = // TODO c.pos1 = toVec3f( pt.getPositionWorldOnA() ); c.pos2 = toVec3f( pt.getPositionWorldOnB() ); c.norm = toVec3f( pt.m_normalWorldOnB ); c.distance = pt.getDistance(); res.push_back(c); } } } return res; } // --------- ghost object -------------- btManifoldArray manifoldArray; btBroadphasePairArray& pairArray = ghost_body->getOverlappingPairCache()->getOverlappingPairArray(); int numPairs = pairArray.size(); for (int i=0;i<numPairs;i++) { manifoldArray.clear(); const btBroadphasePair& pair = pairArray[i]; //unless we manually perform collision detection on this pair, the contacts are in the dynamics world paircache: btBroadphasePair* collisionPair = world->getPairCache()->findPair(pair.m_pProxy0,pair.m_pProxy1); if (!collisionPair) continue; if (collisionPair->m_algorithm) collisionPair->m_algorithm->getAllContactManifolds(manifoldArray); for (int j=0;j<manifoldArray.size();j++) { btPersistentManifold* manifold = manifoldArray[j]; btScalar directionSign = manifold->getBody0() == ghost_body ? btScalar(-1.0) : btScalar(1.0); for (int p=0;p<manifold->getNumContacts();p++) { const btManifoldPoint&pt = manifold->getContactPoint(p); if (pt.getDistance()<0.f) { VRCollision c; c.pos1 = toVec3f( pt.getPositionWorldOnA() ); c.pos2 = toVec3f( pt.getPositionWorldOnB() ); c.norm = toVec3f( pt.m_normalWorldOnB*directionSign ); c.distance = pt.getDistance(); res.push_back(c); } } } } return res; }
OSG::Vec3f VRPhysics::getTorque() { Lock lock(mtx()); return toVec3f(body->getTotalTorque());}
void VRPhysics::setDamping(float lin, float ang) { Lock lock(mtx()); body->setDamping(btScalar(lin),btScalar(ang)); }