Table of Contents

Keep an Object facing the Mouse Position

Good for weapons that fire in the direction of the mouse

Resource

Config

[Arrow]
Graphic         = @
Texture         = arrow.png
Pivot           = center
Position        = (0, 0, 0)

Code

orxVECTOR arrowPosition = orxVECTOR_0;
orxObject_GetPosition(arrow, &arrowPosition);
 
orxVECTOR mousePosition = orxVECTOR_0;
orxMouse_GetPosition(&mousePosition);
orxRender_GetWorldPosition(&mousePosition, orxNULL, &mousePosition);
 
orxVECTOR direction = orxVECTOR_0;
orxVector_Add(&direction, &arrowPosition, &mousePosition);
 
orxFLOAT angle = orxMath_ATan(direction.fY, direction.fX);
 
orxObject_SetRotation(arrow, angle);

See also

Convert a Vector to an Angle (radians)