Jump to content


Photo

Adding rotation to skewed coordinates


  • Please log in to reply
2 replies to this topic

#1 ryguydavis

ryguydavis

    GMC Member

  • New Member
  • 277 posts
  • Version:Unknown

Posted 04 March 2012 - 11:23 PM

I need a little help with some math wizardry for a script which skews sprites. I want the function to do everything draw_sprite_ext does but with the option to skew horizontally and vertically. Currently the scripts works very well at skewing sprites, but is unable to rotate them. Is there someone with a better knowledge of primitives and manipulation of geometry in general who could help add in the image_angle argument?

//(sprite,subimg,x,y,xscale,yscale,rotation,color,alpha,hskew,vskew)

_x=argument2-sprite_get_xoffset(argument0)*argument4
_y=argument3-sprite_get_yoffset(argument0)*argument5

_xscale=argument4
_yscale=argument5

_angle=argument6

draw_set_color(argument7)
draw_set_alpha(argument8)

hskew=argument9
vskew=argument10

var tex;
tex = sprite_get_texture(argument0,argument1);
draw_primitive_begin_texture(pr_trianglestrip, tex);
draw_vertex_texture(_x-hskew,_y+sprite_get_height(argument0)*_yscale+vskew, 0, 1);
draw_vertex_texture(_x+sprite_get_width(argument0)*_xscale-hskew, _y+sprite_get_height(argument0)*_yscale-vskew, 1, 1);
draw_vertex_texture(_x+hskew,_y+vskew,0, 0);
draw_vertex_texture(_x+sprite_get_width(argument0)*_xscale+hskew, _y-vskew,1, 0);
draw_primitive_end();

texture_set_blending(true)
draw_set_alpha(1)

Credit goes to Medusar for the original code (which can be found here.) Any help will be credited. Also, anyone can feel free to use the code, which can be plugged into any project with a simple copy and paste.

Thanks,

Ryan Davis
  • 0

#2 Medusar

Medusar

    GMC Member

  • GMC Member
  • 1228 posts
  • Version:GM:Studio

Posted 05 March 2012 - 11:07 AM

You'll have to rotate each of the four corners individually. Below is a snippet showing how to rotate the point (xx, yy) around the point(xcen, ycen) by an angle a.

var tx;
// Translate so that (xcen, ycen) becomes the origin
xx -= xcen;
yy -= ycen;
// Rotate around the origin
tx = xx; // need to store this
xx = cos(a)*xx - sin(a)*yy;
yy = sin(a)*tx + cos(a)*yy;
// Translate back
xx += xcen;
yy += ycen;

You will have to do this piece of code four times, once for each corner of the image. Of course the code can be sped up by precalculating sin(a) and cos(a). I decided not to do these things in this post for clarity.

By the way, there's no need to give credit for anything I write on this forum;). It is appreciated though.
  • 0

#3 brac37

brac37

    GMC Member

  • GMC Member
  • 765 posts
  • Version:GM7

Posted 08 March 2012 - 09:34 PM

See http://gmc.yoyogames...howtopic=493529 for a script that does every transformation matrix by way of d3d_transforms.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users