• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM D3D Blocks facing objects?

E

Eggyy

Guest
I'm currently working on a basic 3d dungeon crawler game, and I have a tree prop. I want it to be 2D, and it should always be able to face the player so it doesn't look terrible. How do i do this?
 

Yal

šŸ§ *penguin noises*
GMC Elder
There's a bunch of different solutions:
  • Apply a transform before drawing a sprite with draw_sprite, such that its Y axis points in the direction that is "up" in 3D space and its Z axis points towards the camera. (This means you rotate it twice in succession). The old way of doing this is as such, but it's more efficient to do the same transform using a single transformation matrix:
    Code:
    d3d_transform_add_rotation_x(90)
    d3d_transform_add_rotation_y(0)
    d3d_transform_add_rotation_z(cameradirection)
    d3d_transform_add_translation(x,y,z)
    draw_set_color(c_white)
    draw_sprite_ext(my_sprite,image_index,0,0,0.5,0.5,0,image_blend,1)
    d3d_transform_set_identity();
  • Use the sprite as a texture for a d3d_wall that is drawn between two points offset with a step perpendicular to the camera's angle. This gives it the proper UVs to work with 3D lighting, which the sprite-rotation approach does not give.
    upload_2019-10-13_12-28-41.png
 
E

Eggyy

Guest
There's a bunch of different solutions:
  • Apply a transform before drawing a sprite with draw_sprite, such that its Y axis points in the direction that is "up" in 3D space and its Z axis points towards the camera. (This means you rotate it twice in succession). The old way of doing this is as such, but it's more efficient to do the same transform using a single transformation matrix:
    Code:
    d3d_transform_add_rotation_x(90)
    d3d_transform_add_rotation_y(0)
    d3d_transform_add_rotation_z(cameradirection)
    d3d_transform_add_translation(x,y,z)
    draw_set_color(c_white)
    draw_sprite_ext(my_sprite,image_index,0,0,0.5,0.5,0,image_blend,1)
    d3d_transform_set_identity();
  • Use the sprite as a texture for a d3d_wall that is drawn between two points offset with a step perpendicular to the camera's angle. This gives it the proper UVs to work with 3D lighting, which the sprite-rotation approach does not give.
    View attachment 27019
I managed to get it working with this code:
Code:
/// Draw the wall
d3d_transform_add_rotation_x(90)
d3d_transform_add_rotation_y(0)
d3d_transform_add_rotation_z(o_player.dir + 90)
d3d_transform_add_translation(x,y,z)
draw_set_color(c_white)
draw_sprite_ext(s_tree_prop,image_index,0,0,0.5,0.5,0,image_blend,1)
d3d_transform_set_identity();
In the CREATE code, z is 16 (the height of s_tree_prop). 'dir' is the direction in which the camera is facing. With this code, the tree prop only rotates to where I look, and it rotates in a circle, not on the spot.

I'm wanting it to rotate on the spot to face the o_player, What should I do?
 
Last edited by a moderator:
The first d3d_transform, should be a "set", just in case whatever came directly before it wasn't a set_identity.

Also, make sure the sprite origin of your tree sprite is at the top center. (or bottom center, if you didn't translate the tree by z.)

You don't need to add a rotation of 0.

the tree prop only rotates to where I look.... I'm wanting it to rotate... to face the o_player.
You can do that by using point_direction from the camera to the tree. I think you'll need to subtract 90 from that.
However, in my experience, it usually does look better to have these kinds of sprites rotated to face the same direction that the camera is looking, rather than to point toward the camera.
 
E

Eggyy

Guest
Why are the transparent sections of my tree texture replaced by the background colour?
 
Top