fix(physics): reset velocity on move + update property so that joints break
This commit is contained in:
parent
4005bf1cb5
commit
4b799f75d4
5 changed files with 18 additions and 0 deletions
|
@ -90,6 +90,11 @@ Part::Part(PartConstructParams params): Instance(&TYPE), cframe(Data::CFrame::Fr
|
|||
.codec = cframeRotationCodec(),
|
||||
.updateCallback = memberFunctionOf(&Part::onUpdated, this),
|
||||
.flags = PropertyFlags::PROP_NOSAVE
|
||||
}}, { "Velocity", {
|
||||
.backingField = &velocity,
|
||||
.type = &Vector3::TYPE,
|
||||
.codec = fieldCodecOf<Data::Vector3>(),
|
||||
.updateCallback = memberFunctionOf(&Part::onUpdated, this),
|
||||
}}, { "CFrame", {
|
||||
.backingField = &cframe,
|
||||
.type = &Data::CFrame::TYPE,
|
||||
|
@ -180,6 +185,10 @@ void Part::OnAncestryChanged(std::optional<std::shared_ptr<Instance>> child, std
|
|||
}
|
||||
|
||||
void Part::onUpdated(std::string property) {
|
||||
// Reset velocity
|
||||
if (property != "Velocity")
|
||||
velocity = Data::Vector3::ZERO;
|
||||
|
||||
if (workspace())
|
||||
workspace().value()->SyncPartPhysics(std::dynamic_pointer_cast<Part>(this->shared_from_this()));
|
||||
|
||||
|
|
|
@ -43,6 +43,7 @@ protected:
|
|||
public:
|
||||
const static InstanceType TYPE;
|
||||
|
||||
Data::Vector3 velocity;
|
||||
Data::CFrame cframe;
|
||||
glm::vec3 size;
|
||||
Data::Color3 color;
|
||||
|
|
|
@ -94,6 +94,7 @@ void Workspace::SyncPartPhysics(std::shared_ptr<Part> part) {
|
|||
part->rigidBody->updateMassFromColliders();
|
||||
part->rigidBody->updateLocalInertiaTensorFromColliders();
|
||||
|
||||
part->rigidBody->setLinearVelocity(part->velocity);
|
||||
// part->rigidBody->setMass(density * part->size.x * part->size.y * part->size.z);
|
||||
|
||||
part->rigidBody->setUserData(&*part);
|
||||
|
@ -111,6 +112,7 @@ void Workspace::PhysicsStep(float deltaTime) {
|
|||
std::shared_ptr<Part> part = std::dynamic_pointer_cast<Part>(obj);
|
||||
const rp::Transform& transform = part->rigidBody->getTransform();
|
||||
part->cframe = Data::CFrame(transform);
|
||||
part->velocity = part->rigidBody->getLinearVelocity();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -157,6 +157,7 @@ void MainGLWidget::handleObjectDrag(QMouseEvent* evt) {
|
|||
|
||||
|
||||
gWorkspace()->SyncPartPhysics(draggingObject.lock());
|
||||
draggingObject.lock()->UpdateProperty("Position");
|
||||
sendPropertyUpdatedSignal(draggingObject.lock(), "Position", draggingObject.lock()->position());
|
||||
}
|
||||
|
||||
|
@ -244,6 +245,8 @@ void MainGLWidget::handleLinearTransform(QMouseEvent* evt) {
|
|||
playSound("./assets/excluded/switch.wav");
|
||||
|
||||
gWorkspace()->SyncPartPhysics(part);
|
||||
part->UpdateProperty("Position");
|
||||
part->UpdateProperty("Size");
|
||||
sendPropertyUpdatedSignal(part, "Position", part->position());
|
||||
sendPropertyUpdatedSignal(part, "Size", Data::Vector3(part->size));
|
||||
}
|
||||
|
@ -287,6 +290,7 @@ void MainGLWidget::handleRotationalTransform(QMouseEvent* evt) {
|
|||
part->cframe = initialFrame * Data::CFrame::FromEulerAnglesXYZ(-angles);
|
||||
|
||||
gWorkspace()->SyncPartPhysics(part);
|
||||
part->UpdateProperty("Rotation");
|
||||
sendPropertyUpdatedSignal(part, "Rotation", part->cframe.ToEulerAnglesXYZ());
|
||||
}
|
||||
|
||||
|
|
|
@ -178,6 +178,8 @@ MainWindow::MainWindow(QWidget *parent)
|
|||
|
||||
// gWorkspace()->AddChild(snap);
|
||||
gDataModel->GetService<JointsService>()->AddChild(snap);
|
||||
snap->UpdateProperty("Part0");
|
||||
snap->UpdateProperty("Part1");
|
||||
}
|
||||
|
||||
void MainWindow::closeEvent(QCloseEvent* evt) {
|
||||
|
|
Loading…
Add table
Reference in a new issue