AddPointForce
This function adds a force at a specific position on an entity, transforming the force into both a force and torque value. This can be used to simulate realistic impacts like bullets hitting an object.
Syntax
- void AddPointForce(float fx, float fy, float fz, float px, float py, float pz, bool global=true)
- void AddPointForce(const Vec3& force, const Vec3& position, const bool global=true)
Parameters
- fx: the x component of the force to add.
- fy: the y component of the force to add.
- fz: the z component of the force to add.
- px: the x component of the position to add the force at.
- py: the y component of the position to add the force at.
- pz: the z component of the position to add the force at.
- force: the force to add.
- position: the position to add the force at.
- global: if set to true, the force will be added in global coordinates. Set this to false to add the force in local coordinates.
Example
#include "Leadwerks.h"
using namespace Leadwerks;
int main(int argc, const char *argv[])
{
Leadwerks::Window* window = Leadwerks::Window::Create();
Context* context = Context::Create(window);
World* world = World::Create();
Camera* camera = Camera::Create();
camera->SetRotation(35, 0, 0);
camera->Move(0, 0, -6);
Light* light = DirectionalLight::Create();
light->SetRotation(35, 35, 0);
//Create the ground
Model* ground = Model::Box(10, 1, 10);
ground->SetPosition(0, -0.5, 0);
ground->SetColor(0.0, 1.0, 0.0);
//Create a shape
Shape* shape = Shape::Box(0, 0, 0, 0, 0, 0, 10, 1, 10);
ground->SetShape(shape);
shape->Release();
//Create a model
Model* model = Model::Box(10, 1, 1);
model->SetPosition(0, 0.5, 0);
model->SetColor(0.0, 0.0, 1.0);
model->SetMass(1);
//Create a shape
shape = Shape::Box(0, 0, 0, 0, 0, 0, 10, 1, 1);
model->SetShape(shape);
shape->Release();
//We're going to add a force (0,0,100) at the position (5,0,0)
//This will push a point on the right side of the object away from the camera
model->AddPointForce(0, 0, 100, 5, 0, 0);
while (true)
{
if (window->Closed() || window->KeyDown(Key::Escape)) return false;
Leadwerks::Leadwerks::Time::Update();
world->Update();
world->Render();
context->Sync();
}
}