Răsfoiți Sursa

Reliable network data fixes, physics will now set entity position and quaternion rotation properties instead of by pure matrix, added convenience methods to PhysicsSceneEntity

Ivan Safrin 12 ani în urmă
părinte
comite
0b9a735110

+ 15 - 10
Core/Contents/Source/PolyPeer.cpp

@@ -27,11 +27,14 @@ THE SOFTWARE.
 
 using namespace Polycode;
 
-void PeerConnection::ackPackets(unsigned int ack) {
-	for(int i=0; i < reliablePacketQueue.size(); i++) {
-		if(reliablePacketQueue[i].packet->header.sequence == ack) {
-			delete reliablePacketQueue[i].packet;
-			reliablePacketQueue.erase(reliablePacketQueue.begin()+i);
+void PeerConnection::ackPackets(unsigned int ack) {	
+	std::vector<SentPacketEntry>::iterator it;	
+	for(it = reliablePacketQueue.begin(); it != reliablePacketQueue.end();) {
+		if((*it).packet->header.sequence == ack) {
+			delete (*it).packet;
+			it = reliablePacketQueue.erase(it);
+		} else {
+			++it;
 		}
 	}
 }
@@ -108,12 +111,16 @@ void Peer::sendReliableData(const Address &target, char *data, unsigned int size
 	Packet *packet = createPacket(target, data, size, type);
 	packet->header.reliableID = connection->reliableID;
 	connection->reliableID++;
+	
+	if(connection->reliableID == 0)
+		connection->reliableID = 1;
+
 	sendPacket(target, packet);	
 	
 	SentPacketEntry entry;
 	entry.packet = packet;
 	entry.timestamp = CoreServices::getInstance()->getCore()->getTicks();
-//	connection->reliablePacketQueue.push_back(entry);
+	connection->reliablePacketQueue.push_back(entry);
 
 }
 
@@ -163,9 +170,7 @@ bool Peer::checkPacketAcks(PeerConnection *connection, Packet *packet) {
 		}		
 	}
 	
-	for(int i=0; i < peerConnections.size(); i++) {
-		peerConnections[i]->ackPackets(packet->header.ack);
-	}
+	connection->ackPackets(packet->header.ack);
 	
 	return retVal;
 }
@@ -189,7 +194,7 @@ void Peer::handleEvent(Event *event) {
 
 void Peer::updateReliableDataQueue() {
 	for(int i=0; i < peerConnections.size(); i++) {
-		for(int j=0; j < peerConnections[i]->reliablePacketQueue.size(); j++) {
+		for(int j=0; j < peerConnections[i]->reliablePacketQueue.size(); j++) {		
 			if(peerConnections[i]->reliablePacketQueue[j].timestamp < CoreServices::getInstance()->getCore()->getTicks() - 1000) {
 				peerConnections[i]->reliablePacketQueue[j].timestamp = CoreServices::getInstance()->getCore()->getTicks(); 
 				sendPacket(peerConnections[i]->address, peerConnections[i]->reliablePacketQueue[j].packet);

+ 6 - 0
Modules/Contents/3DPhysics/Include/PolyPhysicsSceneEntity.h

@@ -22,6 +22,7 @@ THE SOFTWARE.
 
 #pragma once
 #include "PolyGlobals.h"
+#include "PolyQuaternion.h"
 #include "PolyCollisionSceneEntity.h"
 #include "btBulletCollisionCommon.h"
 #include "btBulletDynamicsCommon.h"
@@ -54,6 +55,11 @@ namespace Polycode {
 		
 		void setMass(Number mass);
 		
+		Vector3 getVelocity();
+		Vector3 getSpin();
+		
+			void setRotation(Quaternion quat);
+			
 			void setVelocity(Vector3 velocity);
 			void warpTo(Vector3 position, bool resetRotation);
 			

+ 23 - 14
Modules/Contents/3DPhysics/Source/PolyPhysicsSceneEntity.cpp

@@ -229,26 +229,35 @@ void PhysicsSceneEntity::setMass(Number mass) {
 }
 
 void PhysicsSceneEntity::Update() {		
-	Matrix4 m;
-		
-	btScalar* mat = (btScalar*) malloc(sizeof(btScalar) * 16);				
-		
-	rigidBody->getWorldTransform().getOpenGLMatrix(mat);
-	for(int i=0; i < 16; i++) {
-		m.ml[i] = mat[i];
-	}
-	
-	free(mat);
-	
-	sceneEntity->setTransformByMatrixPure(m);
-//	collisionObject->getWorldTransform().setFromOpenGLMatrix(mat);
+	btVector3 t = rigidBody->getWorldTransform().getOrigin();
+	btQuaternion q = rigidBody->getOrientation();
+	sceneEntity->setRotationQuat(q.getW(), q.getX(), q.getY(), q.getZ());
+	sceneEntity->setPosition(t.getX(), t.getY(), t.getZ());
+}
+
+void PhysicsSceneEntity::setRotation(Quaternion quat) {
+	btTransform t = rigidBody->getWorldTransform();
+	btQuaternion q;
+	q.setValue(quat.x, quat.y, quat.z, quat.w);
+	t.setRotation(q);
+	rigidBody->setWorldTransform(t);
 }
 
 void PhysicsSceneEntity::setVelocity(Vector3 velocity) {
 	rigidBody->setLinearVelocity(btVector3(velocity.x, velocity.y, velocity.z));
-//	rigidBody->applyForce(btVector3(velocity.x, velocity.y, velocity.z), btVector3(0,0,0));
 }
 
+Vector3 PhysicsSceneEntity::getVelocity() {
+	btVector3 retVec = rigidBody->getLinearVelocity();
+	return Vector3(retVec.getX(), retVec.getY(), retVec.getZ());
+}
+
+Vector3 PhysicsSceneEntity::getSpin() {
+	btVector3 retVec = rigidBody->getAngularVelocity();
+	return Vector3(retVec.getX(), retVec.getY(), retVec.getZ());
+}
+
+
 void PhysicsSceneEntity::setSpin(Vector3 spin) {
 	btVector3 angularVel = btVector3(spin.x, spin.y, spin.z);	
 	rigidBody->setAngularVelocity(angularVel);