• 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!

Draw a D3D sprite in the correct 2D Room coordinates

A

alexandervrs

Guest
I am trying to have a card-like sprite rotate around its axis with a small perspective to it, however my game is 2D and I'd like it placed at the correct coordinates (wherever I place it in the Room Editor)



I've flipped the y for the projection (I think) and everything seems it works that is until I move the view, especially across the Y axis, it looks like it causes the D3D-drawn sprites to glitch out/disappear like,



How could I fix this?

You can get a GMZ of this issue here
 
I think this should pretty much cover things:

First you'll probably want to know how to set up a perspective projection to match your view position:

you'll want to move the camera to the center of your view, have it located above it on the z axis, looking back down, and then make sure the camera's up direction is -1 on the y axis.

you'll notice the camera position is actually offset from the center of the view by a tiny ammount (when view port size = view size, this distance is 1/2 pixel). That will keep your images from becoming too blurry when texture interpolation is in use. (assuming the images are drawn at pixel integer positions and have an integer scale.) You stll have the option of turning off texture interpolation to get perfectly crisp images.

the camera should be located, on the z axis, a distance equal to half the view height divided by the tangent of half the field of view angle.

the field of view angle can be anything greater than 0 and less than 180. Smaller angles mean the camera will be farther away (from zero on z axis). Bigger angles mean the camera will be closer. The effect of changing this angle will be that stuff that is 3d (like your rotating sprite) will apear to "pop out" of the screen more (with big angles), and less (with small angles).
Code:
var _angle = 60;
var _offset = view_wview[0] / view_wport[0] / 2
var _x = view_xview[0] + view_wview[0] / 2 + _offset;
var _y = view_yview[0] + view_hview[0] / 2 + _offset;
var _z = view_hview[0]/2 / dtan(_angle/2);
var _aspect = view_wport[0] / view_hport[0];

d3d_set_projection_ext(
    _x,
    _y,
    _z,
    _x,
    _y,
    0,
    0,
    -1,
    0,
    _angle,
    _aspect,
    1,
    16000
);
The other thing you'll need to know is how to draw something at the correct coordinates when a d3d transformation is in effect.
What you want to do is after setting your rotation, add a translation equal to the room position where the thing should be drawn.
Then draw the thing at coordinates (0,0).
Code:
d3d_transform_set_rotation_y(rot++);
d3d_transform_add_translation(x,y,0);
draw_sprite_ext(spr_rotating_thing,0,0,0,4,6,0,c_white,1);
d3d_transform_set_identity();
 
Last edited:
Top