GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2009-02-11 12:52:35

Burunduk
Member
Registered: 2009-01-31
Posts: 1

draw_sprite_origin

Sorry no long descriptions this time. Here are the two versions:

Expand/*
**  Usage:
**      draw_sprite_origin( sprite, image, x, y, angle, hoff, voff )
**
**  Arguments:
**      0-3       like in draw_sprite
**      angle     rotation
**      hoff      desired horizontal offset of the origin
**      voff      desired vertical offset of the origin
**
**  Example:
**      draw_sprite_origin( sprite_index, image_index, x, y, direction, 0, 0 )
**
**  Returns:
**      nothing
**
**  Notes:
**      Sometimes, you cannot rotate your sprites using their origins
**      Then you use this function
**      d3d does not have to be initiated
**
**  Burunduk
*/
var hoff, voff;

// calculate offsets based on origin
hoff = argument5  - sprite_get_xoffset(argument0);
voff = argument6 - sprite_get_yoffset(argument0);

// reset
d3d_transform_set_identity()

// offset
d3d_transform_add_translation(-hoff, -voff, 0)

// rotate
d3d_transform_add_rotation_z(argument4)

// move to right position in room
d3d_transform_add_translation(argument2,argument3,0)

//draw sprite
draw_sprite(argument0, argument1, 0,0);

// reset
d3d_transform_set_identity()
Expand/*
**  Usage:
**      draw_sprite_origin_ext(sprite, subimg, x, y, xscale, yscale, rot, color, alpha, hoff, voff )
**
**  Arguments:
**      0-8       same as draw_sprite_ext
**      hoff      desired horizontal offset of the origin
**      voff      desired vertical offset of the origin
**
**  Example:
**      draw_sprite_origin_ext(sprite_index, image_index, x, y, image_xscale, image_yscale, direction, c_white, 1, 0, 0 )
**
**  Returns:
**      nothing
**
**  Notes:
**      Sometimes, you cannot rotate your sprites using their origins
**      Then you use this function
**      The actual origin has to be at 0,0 for this version to work
**      d3d does not have to be initiated
**
**  Burunduk
*/
var hoff, voff;

// calculate offsets based on origin
hoff = argument9  - sprite_get_xoffset(argument0);
voff = argument10 - sprite_get_yoffset(argument0);

// reset
d3d_transform_set_identity()

// offset
d3d_transform_add_translation(-hoff, -voff, 0)

// rotate
d3d_transform_add_rotation_z(argument6)

// move to right position in room
d3d_transform_add_translation(argument2,argument3,0)

//draw sprite
draw_sprite_ext(argument0, argument1, 0,0, argument4, argument5, 0, argument7, argument8);

// reset
d3d_transform_set_identity()

Last edited by Burunduk (2009-03-05 01:13:00)

Offline

#2 2009-03-30 06:02:24

paul23
Member
Registered: 2007-10-17
Posts: 110

Re: draw_sprite_origin

You shouldn't just clear the stack like that:

if the user already uses d3d transformations BEFORE calling this script (for example to draw a whole part of the game somewhere else) this script will completely mess that up!

better remove all the transformations 1 by 1..

Offline

#3 2009-03-30 19:30:25

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: draw_sprite_origin

better remove all the transformations 1 by 1..

As opposed to pushing onto and later popping off of the transformation stack?

Expandd3d_transform_stack_push();
// offset
// rotate
// move
// draw
d3d_transform_stack_pop();

Although I work like that often, I've never actually needed it as far as I know, so my experience with the d3d transformation stack is decidedly limited.


Abusing forum power since 1986.

Offline

#4 2010-03-01 22:29:19

brac37
Member
Registered: 2010-02-12
Posts: 18

Re: draw_sprite_origin

Expand/*
**  Usage:
**      draw_sprite_origin( sprite, image, x, y, angle, hoff, voff )
**
**  Arguments:
**      0-3       like in draw_sprite
**      angle     rotation
**      hoff      desired horizontal offset of the origin
**      voff      desired vertical offset of the origin
**
**  Example:
**      draw_sprite_origin( sprite_index, image_index, x, y, direction, 0, 0 )
**
**  Returns:
**      nothing
**
**  Notes:
**      Sometimes, you cannot rotate your sprites using their origins
**      Then you use this function
**      d3d does not have to be initiated
**
**  Burunduk and brac37
*/
draw_sprite_origin_ext (argument0, argument1, argument2, argument3, 1, 1, argument4, c_white, 1, argument5, argument6);
Expand/*
**  Usage:
**      draw_sprite_origin_ext(sprite, subimg, x, y, xscale, yscale, rot, color, alpha, hoff, voff )
**
**  Arguments:
**      0-8       same as draw_sprite_ext
**      hoff      desired horizontal offset of the origin
**      voff      desired vertical offset of the origin
**
**  Example:
**      draw_sprite_origin_ext(sprite_index, image_index, x, y, image_xscale, image_yscale, direction, c_white, 1, 0, 0 )
**
**  Returns:
**      nothing
**
**  Notes:
**      Sometimes, you cannot rotate your sprites using their origins
**      Then you use this function
**      The actual origin has to be at 0,0 for this version to work
**      d3d does not have to be initiated
**
**  Burunduk and brac37
*/
var hoff, voff, xx, yy;

// calculate offsets based on origin
hoff = argument9 - sprite_get_xoffset(argument0);
voff = argument10 - sprite_get_yoffset(argument0);

// calculate coordinates
xx = argument2 [+ hoff] - lengthdir_x (hoff, argument6) + lengthdir_y (voff, argument6);
yy = argument3 [+ voff] - lengthdir_x (voff, argument6) - lengthdir_y (hoff, argument6);

//draw sprite
draw_sprite_ext(argument0, argument1, xx, yy, argument4, argument5, argument6, argument7, argument8);

Something like that. I have not tested my code.

EDIT: my code works differently. With my code, the sprite is draw as usual (i.e. on (x - sprite_get_xoffsef, y - sprite_get_yoffset)) if angle = 0, with (x + argument9 - sprite_get_xoffset, y + argument10 - sprite_get_xoffset) being the rotation center on the screen. With Burunduk's code, the rotation center on the screen is (x, y). The rotation center of the sprite is (argument9, argument10) for both scripts. You can get the same behavior if you remove the parts of my script that are between [ and ] now, i.e. [+ hoff] and [+ voff].

Last edited by brac37 (2010-03-02 16:16:11)

Offline

Board footer

Powered by FluxBB