User Tools

Site Tools


en:tutorials:force_points

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

en:tutorials:force_points [2018/05/18 12:09 (3 days ago)] (current)
sausage created
Line 1: Line 1:
 +====== Applying a force at a position point ======
  
 +The most easy way to use the '​orxObject_ApplyForce'​ function is the pass an '​orxNULL'​ value for the last parameter which means to apply force to the centre of the object. Most of the time that s what you probably want.
 +
 +I came across a scenario where I wanted to apply force at the left hand edge, and always in the direction that the object was facing. Turned out to be mathematically trickier than I expect. Here is the signature for the function:
 +
 +'​orxObject_ApplyForce (orxOBJECT *_pstObject,​ const orxVECTOR *_pvForce, const orxVECTOR *_pvPoint)'​
 +
 +The first parameters are the object and force vector to apply. But the last parameter is the position to apply the force. This position is not a local coordinate, but rather, a world position vector.
 +
 +What I really wanted, was the ability to provide a point vector relative to the centre of the object, who's position and direction would remain constantly relative to the objects position and rotation.
 +
 +That way I could provide this relative point to the '​orxObject_ApplyForce'​ function.
 +
 +Here's what I came up with:
 +
 +<code cpp>
 +
 +void PostDebugSpot(orxVECTOR position) {
 + orxObject_SetPosition(debugSpot,​ &​position);​
 +}
 +
 +void Thrust(orxVECTOR localInfluencePoint,​ orxFLOAT power) {
 + orxFLOAT FORCE = power;
 + orxVECTOR thrust = { 0, -FORCE, 0 };
 +
 + orxVECTOR thrustDirection = { 0,0,0 };
 + orxVECTOR thrustPosition = orxVECTOR_0;​
 +
 + orxVECTOR objectPosition = orxVECTOR_0;​
 + orxObject_GetPosition(object,​ &​objectPosition);​
 +
 + orxFLOAT objectRadians = orxObject_GetRotation(object);​
 + orxFLOAT facingRotation = (orxMATH_KF_RAD_TO_DEG * objectRadians) + 0;
 +
 + orxVector_2DRotate(&​thrustDirection,​ &​thrust,​ objectRadians);​
 +
 + orxVector_2DRotate(&​localInfluencePoint,​ &​localInfluencePoint,​ objectRadians);​
 +
 + orxVector_Add(&​thrustPosition,​ &​objectPosition,​ &​localInfluencePoint);​
 +
 + orxObject_ApplyForce(object,​ &​thrustDirection,​ &​thrustPosition);​
 + PostDebugSpot(thrustPosition);​
 +}
 +
 +void DoInputs()
 +{
 + if (orxInput_IsActive("​Drive"​)) {
 + orxVECTOR localInfluencePoint = { -25, 0, 0 }; //relative position from object centre
 + orxFLOAT power = 0.5;
 + Thrust(localInfluencePoint,​ power);
 + }
 +}
 +
 +
 +</​code>​
en/tutorials/force_points.txt ยท Last modified: 2018/05/18 12:09 (3 days ago) by sausage