mirror of
https://github.com/overte-org/overte.git
synced 2025-04-20 03:44:02 +02:00
CharacterController: separate target velocity from parent velocity.
Also, disable damping on the rigidBody used by the CharacterController.
This commit is contained in:
parent
2667fe3c24
commit
97bcc54360
5 changed files with 56 additions and 8 deletions
41
examples/moving-platform.js
Normal file
41
examples/moving-platform.js
Normal file
|
@ -0,0 +1,41 @@
|
|||
var platform;
|
||||
|
||||
function init() {
|
||||
platform = Entities.addEntity({
|
||||
name: "platform",
|
||||
type: "Box",
|
||||
position: { x: 0, y: 0, z: 0 },
|
||||
dimensions: { x: 10, y: 2, z: 10 },
|
||||
color: { red: 0, green: 0, blue: 255 },
|
||||
gravity: { x: 0, y: 0, z: 0 },
|
||||
visible: true,
|
||||
locked: false,
|
||||
lifetime: 6000,
|
||||
velocity: { x: 1.0, y: 0, z: 0 },
|
||||
damping: 0,
|
||||
isDynamic: false
|
||||
});
|
||||
if (platform) {
|
||||
MyAvatar.position = { x: 0, y: 3, z: 0 };
|
||||
if (MyAvatar.getParentID() != platform) {
|
||||
MyAvatar.setParentID(platform);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function update(dt) {
|
||||
|
||||
}
|
||||
|
||||
function shutdown() {
|
||||
if (platform) {
|
||||
MyAvatar.setParentID(0);
|
||||
Entities.deleteEntity(platform);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
init();
|
||||
|
||||
Script.update.connect(update);
|
||||
Script.scriptEnding.connect(shutdown);
|
|
@ -1089,7 +1089,8 @@ void MyAvatar::prepareForPhysicsSimulation() {
|
|||
}
|
||||
}
|
||||
|
||||
_characterController.setTargetVelocity(getTargetVelocity() + parentVelocity);
|
||||
_characterController.setParentVelocity(parentVelocity);
|
||||
_characterController.setTargetVelocity(getTargetVelocity());
|
||||
_characterController.setPositionAndOrientation(getPosition(), getOrientation());
|
||||
if (qApp->isHMDMode()) {
|
||||
_follow.prePhysicsUpdate(*this, deriveBodyFromHMDSensor(), _bodySensorMatrix);
|
||||
|
|
|
@ -67,6 +67,7 @@ void MyCharacterController::updateShapeIfNecessary() {
|
|||
_rigidBody->setAngularFactor(0.0f);
|
||||
_rigidBody->setWorldTransform(btTransform(glmToBullet(_avatar->getOrientation()),
|
||||
glmToBullet(_avatar->getPosition())));
|
||||
_rigidBody->setDamping(0.0f, 0.0f);
|
||||
if (_state == State::Hover) {
|
||||
_rigidBody->setGravity(btVector3(0.0f, 0.0f, 0.0f));
|
||||
} else {
|
||||
|
|
|
@ -54,7 +54,7 @@ CharacterController::CharacterController() {
|
|||
|
||||
_floorDistance = MAX_FALL_HEIGHT;
|
||||
|
||||
_walkVelocity.setValue(0.0f, 0.0f, 0.0f);
|
||||
_targetVelocity.setValue(0.0f, 0.0f, 0.0f);
|
||||
_followDesiredBodyTransform.setIdentity();
|
||||
_followTimeRemaining = 0.0f;
|
||||
_jumpSpeed = JUMP_SPEED;
|
||||
|
@ -166,12 +166,12 @@ void CharacterController::playerStep(btCollisionWorld* dynaWorld, btScalar dt) {
|
|||
|
||||
const btScalar MIN_SPEED = 0.001f;
|
||||
|
||||
btVector3 actualVelocity = _rigidBody->getLinearVelocity();
|
||||
btVector3 actualVelocity = _rigidBody->getLinearVelocity() - _parentVelocity;
|
||||
if (actualVelocity.length() < MIN_SPEED) {
|
||||
actualVelocity = btVector3(0.0f, 0.0f, 0.0f);
|
||||
}
|
||||
|
||||
btVector3 desiredVelocity = _walkVelocity;
|
||||
btVector3 desiredVelocity = _targetVelocity;
|
||||
if (desiredVelocity.length() < MIN_SPEED) {
|
||||
desiredVelocity = btVector3(0.0f, 0.0f, 0.0f);
|
||||
}
|
||||
|
@ -212,7 +212,7 @@ void CharacterController::playerStep(btCollisionWorld* dynaWorld, btScalar dt) {
|
|||
break;
|
||||
}
|
||||
|
||||
_rigidBody->setLinearVelocity(finalVelocity);
|
||||
_rigidBody->setLinearVelocity(finalVelocity + _parentVelocity);
|
||||
|
||||
// Dynamicaly compute a follow velocity to move this body toward the _followDesiredBodyTransform.
|
||||
// Rather then add this velocity to velocity the RigidBody, we explicitly teleport the RigidBody towards its goal.
|
||||
|
@ -383,8 +383,11 @@ void CharacterController::getPositionAndOrientation(glm::vec3& position, glm::qu
|
|||
}
|
||||
|
||||
void CharacterController::setTargetVelocity(const glm::vec3& velocity) {
|
||||
//_walkVelocity = glmToBullet(_avatarData->getTargetVelocity());
|
||||
_walkVelocity = glmToBullet(velocity);
|
||||
_targetVelocity = glmToBullet(velocity);
|
||||
}
|
||||
|
||||
void CharacterController::setParentVelocity(const glm::vec3& velocity) {
|
||||
_parentVelocity = glmToBullet(velocity);
|
||||
}
|
||||
|
||||
void CharacterController::setFollowParameters(const glm::mat4& desiredWorldBodyMatrix, float timeRemaining) {
|
||||
|
|
|
@ -69,6 +69,7 @@ public:
|
|||
void getPositionAndOrientation(glm::vec3& position, glm::quat& rotation) const;
|
||||
|
||||
void setTargetVelocity(const glm::vec3& velocity);
|
||||
void setParentVelocity(const glm::vec3& parentVelocity);
|
||||
void setFollowParameters(const glm::mat4& desiredWorldMatrix, float timeRemaining);
|
||||
float getFollowTime() const { return _followTime; }
|
||||
glm::vec3 getFollowLinearDisplacement() const;
|
||||
|
@ -105,7 +106,8 @@ protected:
|
|||
|
||||
protected:
|
||||
btVector3 _currentUp;
|
||||
btVector3 _walkVelocity;
|
||||
btVector3 _targetVelocity;
|
||||
btVector3 _parentVelocity;
|
||||
btTransform _followDesiredBodyTransform;
|
||||
btScalar _followTimeRemaining;
|
||||
btTransform _characterBodyTransform;
|
||||
|
|
Loading…
Reference in a new issue