1
\$\begingroup\$

I'm trying to get Doom-style 8 direction sprite rotation that changes its sprite depending on the camera and the forward vector of the entity just like this video here: https://www.youtube.com/watch?v=DBRR_ZVWtMk&t=13s.

I've searched around the internet for a solution and I have found some people that have been having the same issue but no one that really got it working like it should. I managed to figure out how to get the entity to change it's sprite depending on the camera view angle but I can't figure out how to account for the entity's own forward vector. I would assume you'll use the CrossProduct but I'm not sure...

Here is my code:

// Get the angles
angles = entity->parms.axis.ToAngles();

// Compute the forward and normalize
forward = angles.ToForward();

// Compute the direction and normalize
direction = rg.renderView.origin - entity->parms.origin;
direction.Normalize();

odVec3 point = forward.Cross(direction); (NOT USED)

// Compute the yaw angle
yaw = RAD2DEG(odMath::ATan(direction.y, direction.x));
yaw = odMath::AngleNormalize360(yaw);


        if (yaw >= 22.5f && yaw < 67.5f)
            entity->parms.skin = R_FindSkin("skins/sdk/rotation/angle_1");
        else if (yaw >= 67.5f && yaw < 112.5f)
            entity->parms.skin = R_FindSkin("skins/sdk/rotation/angle_2");
        else if (yaw >= 112.5f && yaw < 157.5f)
            entity->parms.skin = R_FindSkin("skins/sdk/rotation/angle_3");
        else if (yaw >= 157.0f && yaw < 202.5f)
            entity->parms.skin = R_FindSkin("skins/sdk/rotation/angle_4");
        else if (yaw >= 202.5f && yaw < 247.5f)
            entity->parms.skin = R_FindSkin("skins/sdk/rotation/angle_5");
        else if (yaw >= 247.5f && yaw < 292.5f)
            entity->parms.skin = R_FindSkin("skins/sdk/rotation/angle_6");
        else if (yaw >= 292.5f && yaw < 337.5f)
            entity->parms.skin = R_FindSkin("skins/sdk/rotation/angle_7");
        else
            entity->parms.skin = R_FindSkin("skins/sdk/rotation/angle_0");
\$\endgroup\$
3
  • \$\begingroup\$ 157.0f should be 157.5f. \$\endgroup\$ Dec 19, 2018 at 20:17
  • 1
    \$\begingroup\$ I'm voting to close this question as off-topic because it's just a typo in the code. \$\endgroup\$
    – Almo
    Dec 19, 2018 at 20:41
  • 1
    \$\begingroup\$ @Almo it doesn't look to me like that's the only issue here. The code to combine the view angle + facing angle into a resulting orientation looks to be incomplete. The current yaw value accounts only for the object's facing direction, not the viewing angle. \$\endgroup\$
    – DMGregory
    Dec 19, 2018 at 21:30

0

Browse other questions tagged .