GML Rotate Object On Different Axis

11clock

Member
I have my weapon's sprite origin to be around the handle position for weapon holding purposes. Now I want to be able to rotate the sprite based on the center of the sprite, rather than from the origin. I was wondering how to accomplish this.
 
W

Wayfarer

Guest
Edit: @the_dude_abides approach is better. My approach is probably too much code for a simple task. I've put it into a spoiler tag if you still want to see it.

The rotationOffset is where you specify the rotation point.

Create
Code:
// Settings
// --------
rotationOffsetX = 32;
rotationOffsetY = 24;

imageAngle = 0;
imageScaleX = 1;
imageScaleY = 1;


// Calculations
// ------------
// 1. Store normal offset
normalOffsetX = sprite_get_xoffset(sprite_index);
normalOffsetY = sprite_get_yoffset(sprite_index);

// 2. Work out the differences between normal and rotation offsets
normalAndRotationOffsetDifX = rotationOffsetX - normalOffsetX;
normalAndRotationOffsetDifY = rotationOffsetY - normalOffsetY;
Draw
Code:
imageAngle += 1;

// 1. Update position if sprite is being flipped horizontally or vertically
var scaleMultiplierX = ((imageScaleX == 1) ? 1 : -2),
    scaleMultiplierY = ((imageScaleY == 1) ? 1 : -2);
 
// 2. Render sprite
sprite_set_offset(sprite_index, rotationOffsetX, rotationOffsetY);
draw_sprite_ext(sprite_index, -1, x + (normalAndRotationOffsetDifX * scaleMultiplierX), y + (normalAndRotationOffsetDifY * scaleMultiplierY), imageScaleX, imageScaleY, imageAngle, c_white, 1);
sprite_set_offset(sprite_index, normalOffsetX, normalOffsetY);
 
Last edited:
You would get half the sprite_width, and half the sprite_height, and then find out how far that is from your origin point. Set this distance to a variable called 'length' Also find out the angle from origin to intended point, and set that to 'angle'. But only find those measurements once, and keep them as a permanent variable (in the create event for example)

//// in a step event
new_x = x + lengthdir_x(length, angle + image_angle) // image_angle of weapon
new_y = y + lengthdir_y(length, angle + image_angle) // " "

If I'm thinking that through properly it should add the direction of the weapon to the original angle, and give you the center of the sprite.
 

CMAllen

Member
You could also change the sprite's origin point with sprite_set_offset(). Unless that's not really what you mean.
 
Top