3D [3D FPS] Sprite rotation in relation to direction and camera

D

devvhogg

Guest
Hello everyone.

I'm working on a FPS that uses sprites instead of 3D models to represent NPC's (like it was done in Doom, Wolfenstein 3D, Hexen etc). The sprites of one NPC represent 4 different directions. The basic formula for calculating what sprite to show the camera is the following:

NPC step event
Code:
angle = point_direction(x, y, obj_character.x,obj_character.y);
spridedir= angle*4/360; // "spritedir" is the image index, basically
This works flawlessly. The problem is adding the NPC's facing direction into the equation. My NPC's move independently in 8 directions, sometimes at random. Currently I have the following code worked out, which works when walking towards and away from the player (i.e. the camera), but not when walking to the left or right (see comment in code snippet):

NPC step event
Code:
worldangle = point_direction(x,y,x+xspeed,y+yspeed);
angle = point_direction(x, y, obj_player.x, obj_player.y);

x1 = lengthdir_x(1,worldangle);
y1 = lengthdir_y(1,worldangle);
x2 = lengthdir_x(1,angle);
y2 = lengthdir_y(1,angle);

angledif = (dot_product_normalised(x1,y1,x2,y2))*180;

if (angledif > 0 and angledif < 90)
{
    spritedir = 0;
}
else if (angledif > 90 and angledif < 180 )
{
    spritedir = 1;
}
else if (angledif < 0 and angledif > -90) // THE difference between >0 and <0 makes it so these change between left and right sprite constantly
{
    spritedir = 2;
}
else if (angledif < -90 and angledif > -180)
{
    spritedir = 3;
}
I'm at a loss. Has anyone made this system before? My terrible math skills are causing me a lot of grief here.

For reference about what I'm talking about here, see this video (this is the exact type of system I need, with actors moving independently):

Thank you
 

TheSnidr

Heavy metal viking dentist
GMC Elder
Try this:
Code:
angle = angle_difference(direction, point_direction(x, y, obj_character.x,obj_character.y)) + 180;
spridedir= angle*4/360; // "spritedir" is the image index, basically
 
D

devvhogg

Guest
Thanks for the reply!
It's better, but I can't get it to work properly. If an NPC is walking from the left side of the camera to the right side, the sprite will change when passing the middle of the camera view to the next direction.

So if the character is watching from the south, the NPC is walking east in from of the camera, the correct sprite is showing (NPC walking right). But when it passes the middle of the camera view it changes to the next direction, which is facing towards the camera (but it's still walking east, making the NPC look towards the player in south but walking east).

If you want me to provide more info I'll gladly do it
 

TheSnidr

Heavy metal viking dentist
GMC Elder
Ah, try using "round":
Code:
angle = angle_difference(direction, point_direction(x, y, obj_character.x,obj_character.y)) + 180;
spridedir= round(angle*4/360); // "spritedir" is the image index, basically]/code]
This rounds it to the closest integer, so that it switches frame at the correct angle!
 
Last edited:
D

devvhogg

Guest
Still the same problem :(
I used "floor" earlier, because when it rounds up it can round up to 4, of which there is no sprite, only 0 to 3. I'm not quite sure what's wrong, but maybe it is the way I calculate the NPC's walking direction? Since I don't use the built in "direction", I calculate it with
Code:
point_direction(x,y,x+xspeed,y+yspeed);
Could this be the issue?

EDIT
So it seems to be working, but it's almost like the sprite rotation is 45 degrees turned the wrong way (difficult to see with 4 sprites, I know) at all times. Here's a GIF of the problem:
http://i.imgur.com/aavdIWT.gifv
 
Last edited by a moderator:
Hi,
I'm a novice and I don't know how to fix your problem but could you upload your project here or send it to me please I want to do a old school doom with game maker and it would help me a lot for the npc ^^
 
Top