| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- //
- // Urho3D Engine
- // Copyright (c) 2008-2011 Lasse Öörni
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "Precompiled.h"
- #include "Joint.h"
- #include "Log.h"
- #include "PhysicsWorld.h"
- #include "ReplicationUtils.h"
- #include "RigidBody.h"
- #include "Scene.h"
- #include "StringUtils.h"
- #include <ode/ode.h>
- #include "DebugNew.h"
- static const std::string typeNames[] =
- {
- "none",
- "ball",
- "hinge",
- };
- Joint::Joint(PhysicsWorld* world, const std::string& name) :
- Component(name),
- mWorld(world),
- mType(JOINT_NONE),
- mJoint(0),
- mNetDirty(false)
- {
- if (!mWorld)
- EXCEPTION("Null physics world for Joint");
- }
- Joint::~Joint()
- {
- reset();
- }
- void Joint::save(Serializer& dest)
- {
- // Write Component properties
- Component::save(dest);
-
- // Write Joint properties
- dest.writeUByte((unsigned char)mType);
-
- ComponentRef bodyARef(mBodyA);
- ComponentRef bodyBRef(mBodyB);
- bodyARef.write(dest);
- bodyBRef.write(dest);
-
- switch (mType)
- {
- case JOINT_BALL:
- dest.writeVector3(getPosition());
- break;
-
- case JOINT_HINGE:
- dest.writeVector3(getPosition());
- dest.writeVector3(getAxis());
- break;
- }
- }
- void Joint::load(Deserializer& source, ResourceCache* cache)
- {
- // Read Component properties
- Component::load(source, cache);
-
- // Read Joint properties
- mType = (JointType)source.readUByte();
- mBodyARef.read(source);
- mBodyBRef.read(source);
-
- switch (mType)
- {
- case JOINT_BALL:
- mPosition = source.readVector3();
- break;
-
- case JOINT_HINGE:
- mPosition = source.readVector3();
- mAxis = source.readVector3();
- break;
- }
- }
- void Joint::saveXML(XMLElement& dest)
- {
- // Write Component properties
- Component::saveXML(dest);
-
- // Write Joint properties
- XMLElement jointElem = dest.createChildElement("joint");
- jointElem.setString("type", typeNames[mType]);
-
- XMLElement bodyAElem = dest.createChildElement("body_a");
- XMLElement bodyBElem = dest.createChildElement("body_b");
- ComponentRef bodyARef(mBodyA, true);
- ComponentRef bodyBRef(mBodyB, true);
- bodyARef.writeXML(bodyAElem);
- bodyBRef.writeXML(bodyBElem);
-
- switch (mType)
- {
- case JOINT_BALL:
- jointElem.setVector3("pos", getPosition());
- break;
-
- case JOINT_HINGE:
- jointElem.setVector3("pos", getPosition());
- jointElem.setVector3("axis", getAxis());
- break;
- }
- }
- void Joint::loadXML(const XMLElement& source, ResourceCache* cache)
- {
- // Read Component properties
- Component::loadXML(source, cache);
-
- // Read Joint properties
- XMLElement jointElem = source.getChildElement("joint");
- XMLElement bodyAElem = source.getChildElement("body_a");
- XMLElement bodyBElem = source.getChildElement("body_b");
-
- mType = (JointType)getIndexFromStringList(jointElem.getStringLower("type"), typeNames, 3, 0);
- mBodyARef.readXML(bodyAElem);
- mBodyBRef.readXML(bodyBElem);
-
- switch (mType)
- {
- case JOINT_BALL:
- mPosition = jointElem.getVector3("pos");
- break;
-
- case JOINT_HINGE:
- mPosition = jointElem.getVector3("pos");
- mAxis = jointElem.getVector3("axis");
- break;
- }
- }
- void Joint::postLoad(ResourceCache* cache)
- {
- RigidBody* bodyA = static_cast<RigidBody*>(mEntity->getScene()->getComponent(mBodyARef));
- RigidBody* bodyB = static_cast<RigidBody*>(mEntity->getScene()->getComponent(mBodyBRef));
-
- switch (mType)
- {
- case JOINT_BALL:
- if (!setBall(mPosition, bodyA, bodyB))
- mType = JOINT_NONE;
- break;
-
- case JOINT_HINGE:
- if (!setHinge(mPosition, mAxis, bodyA, bodyB))
- mType = JOINT_NONE;
- break;
- }
- }
- bool Joint::writeNetUpdate(Serializer& dest, Serializer& destRevision, Deserializer& baseRevision, const NetUpdateInfo& info)
- {
- unsigned char bits = 0;
-
- ComponentRef bodyARef(mBodyA);
- ComponentRef bodyBRef(mBodyB);
- JointType type = mType;
-
- checkUByte((unsigned char)type, (unsigned char)JOINT_NONE, baseRevision, bits, 1);
- checkComponentRef(bodyARef, ComponentRef(), baseRevision, bits, 2);
- checkComponentRef(bodyBRef, ComponentRef(), baseRevision, bits, 2);
-
- switch (type)
- {
- case JOINT_BALL:
- checkVector3(getPosition(), baseRevision, bits, 4);
- // If whole joint will be recreated, also send position
- if (bits & 3)
- bits |= 4;
- break;
-
- case JOINT_HINGE:
- checkVector3(getPosition(), baseRevision, bits, 4);
- checkVector3(getAxis(), baseRevision, bits, 8);
- // If whole joint will be recreated, also send position and axis
- if (bits & 3)
- bits |= 12;
- break;
- }
-
- // Update replication state fully, and network stream by delta
- dest.writeUByte(bits);
- writeUByteDelta((unsigned char)type, dest, destRevision, bits & 1);
- writeComponentRefDelta(bodyARef, dest, destRevision, bits & 2);
- writeComponentRefDelta(bodyBRef, dest, destRevision, bits & 2);
- writeVector3Delta(getPosition(), dest, destRevision, bits & 4);
- writeVector3Delta(getAxis(), dest, destRevision, bits & 8);
-
- return bits != 0;
- }
- void Joint::readNetUpdate(Deserializer& source, ResourceCache* cache, const NetUpdateInfo& info)
- {
- unsigned char bits = source.readUByte();
- if (bits & 1)
- mType = (JointType)source.readUByte();
- if (bits & 2)
- {
- mBodyARef.readPacked(source);
- mBodyBRef.readPacked(source);
- }
- if (bits & 4)
- mPosition = source.readVector3();
- if (bits & 8)
- mAxis = source.readVector3();
-
- // If joint type or body refs changed, recreate whole joint
- if (bits & 3)
- mNetDirty = true;
- else
- {
- // Otherwise just reset the position or axis as applicable
- if (bits & 4)
- setPosition(mPosition);
- if (bits & 8)
- setAxis(mAxis);
- }
- }
- void Joint::postNetUpdate(ResourceCache* cache)
- {
- if (mNetDirty)
- {
- postLoad(cache);
- mNetDirty = false;
- }
- }
- void Joint::getComponentRefs(std::vector<ComponentRef>& dest)
- {
- if (mBodyA)
- dest.push_back(ComponentRef(mBodyA));
- if (mBodyB)
- dest.push_back(ComponentRef(mBodyB));
- }
- void Joint::reset()
- {
- if (mJoint)
- {
- dJointDestroy(mJoint);
- mJoint = 0;
- }
-
- mBodyA.reset();
- mBodyB.reset();
- mType = JOINT_NONE;
- }
- bool Joint::setBall(const Vector3& position, RigidBody* bodyA, RigidBody* bodyB)
- {
- reset();
-
- if ((!bodyA) && (!bodyB))
- return false;
-
- mJoint = dJointCreateBall(mWorld->getWorld(), 0);
- dJointSetData(mJoint, this);
- dJointSetBallAnchor(mJoint, position.mX, position.mY, position.mZ);
- dJointAttach(mJoint, bodyA ? bodyA->getBody() : 0, bodyB ? bodyB->getBody() : 0);
-
- mType = JOINT_BALL;
- mBodyA = bodyA;
- mBodyB = bodyB;
-
- return true;
- }
- bool Joint::setHinge(const Vector3& position, const Vector3& axis, RigidBody* bodyA, RigidBody* bodyB)
- {
- reset();
-
- if ((!bodyA) && (!bodyB))
- return false;
-
- Vector3 normalizedAxis = axis.getNormalized();
-
- mJoint = dJointCreateHinge(mWorld->getWorld(), 0);
- dJointSetData(mJoint, this);
- dJointSetHingeAnchor(mJoint, position.mX, position.mY, position.mZ);
- dJointSetHingeAxis(mJoint, normalizedAxis.mX, normalizedAxis.mY, normalizedAxis.mZ);
- dJointAttach(mJoint, bodyA ? bodyA->getBody() : 0, bodyB ? bodyB->getBody() : 0);
-
- mType = JOINT_HINGE;
- mBodyA = bodyA;
- mBodyB = bodyB;
-
- return true;
- }
- void Joint::setPosition(const Vector3& position)
- {
- switch (mType)
- {
- case JOINT_BALL:
- dJointSetBallAnchor(mJoint, position.mX, position.mY, position.mZ);
- break;
-
- case JOINT_HINGE:
- dJointSetHingeAnchor(mJoint, position.mX, position.mY, position.mZ);
- break;
- }
- }
- void Joint::setAxis(const Vector3& axis)
- {
- switch (mType)
- {
- case JOINT_HINGE:
- dJointSetHingeAxis(mJoint, axis.mX, axis.mY, axis.mZ);
- break;
- }
- }
- Vector3 Joint::getPosition() const
- {
- dVector3 pos;
-
- switch (mType)
- {
- case JOINT_BALL:
- dJointGetBallAnchor(mJoint, pos);
- return Vector3(pos[0], pos[1], pos[2]);
-
- case JOINT_HINGE:
- dJointGetHingeAnchor(mJoint, pos);
- return Vector3(pos[0], pos[1], pos[2]);
- }
-
- return Vector3::sZero;
- }
- Vector3 Joint::getAxis() const
- {
- dVector3 axis;
-
- switch (mType)
- {
- case JOINT_HINGE:
- dJointGetHingeAxis(mJoint, axis);
- return Vector3(axis[0], axis[1], axis[2]);
- }
-
- return Vector3::sZero;
- }
|