en:tutorials:force_points

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

Next revision | Previous revision | ||

en:tutorials:force_points [2018/05/18 12:09 (10 months ago)] sausage created |
en:tutorials:force_points [2018/05/22 12:31 (10 months ago)] (current) sausage |
||
---|---|---|---|

Line 1: | Line 1: | ||

====== Applying a force at a position point ====== | ====== 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. | + | 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 is 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: | + | I came across a scenario where I wanted to apply force always at the left hand edge, and always in the direction that the object was facing. |

- | 'orxObject_ApplyForce (orxOBJECT *_pstObject, const orxVECTOR *_pvForce, const orxVECTOR *_pvPoint)' | + | Here is the signature for the function: |

+ | | ||

+ | ''orxObject_ApplyForce (orxOBJECT *_pstObject, const orxVECTOR *_pvForce, const orxVECTOR *_pvPoint)'' | ||

+ | | ||

+ | The first two 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. Nor is the vector relative to the object rotation. | ||

+ | | ||

+ | Turned out to be mathematically trickier than I expected. | ||

- | 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. | 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. | + | That way I could provide this relative point to the ''orxObject_ApplyForce'' function. |

Here's what I came up with: | Here's what I came up with: | ||

Line 55: | Line 60: | ||

</code> | </code> | ||

+ | |||

+ | I'll leave it to you, the reader to pick through the ''Thrust()'' function. But in short, you provide a vector, which is relative to the centre of the object. In this case, ''{ -25, 0, 0 }'' which means 25 pixels to the left of the object, and 0 vertically in the middle (my test object was 50 x 50 pixels). | ||

+ | |||

+ | The second parameter is the strength of the force to apply. | ||

+ | |||

+ | The routine checks the rotation of the object in order to work out which direction to apply the force. Then it uses the rotation, location, and the passed in vector to work out the relative offset position of the object and direction. | ||

+ | |||

+ | Finally, a small debug is placed on top of this position to ensure all is working as expected. | ||

+ | |||

+ | When all is done, applying force to the relative point, will give the following result: | ||

+ | |||

+ | {{ :en:tutorials:point-force.gif |}} |

en/tutorials/force_points.1526645343.txt.gz · Last modified: 2018/05/18 12:09 (10 months ago) by sausage