Legacy GM [SOLVED] Math for (relative) sprite rotation/repositioning

Hello all!

Would a math-wiz lend some help please?

<IMAGE BELOW>
http://postimg.org/image/mwse7icdz/

Rotating SPRITE A with image_angle, and need SPRITE B to rotate with the same angle--no problem here, but repositioning as in the supplied image... how would one go about that? I suspect some Lengthdir stuff...

Cheers,
Nathan.
 

jo-thijs

Member
You want to rotate position (X2, Y2) around (X1, Y1) by image_angle amount of degrees counterclockwise?
Code:
var tdist = point_distance(X1, Y1, X2, Y2);
var tdir = point_direction(X1, Y1, X2, Y2) + image_angle;

NX = X1 + lengthdir_x(tdist, tdir);
NY = Y1 + lengthdir_y(tdist, tdir);
 
Hi jo-thijs, thanks for writing in!

Clockwise, or counterclockwise it doesn't matter. I'd like SPRITE B to stay "locked" to SPRITE A wherever SPRITE A goes in a rotational sense.
Put another way, imagine that SPRITE A and SPRITE B are not two sprites, but only one non-separated rectangular sprite, and we rotate this rectangular sprite. SPRITE B should 'behave' as though it were one in the same sprite, SPRITE A.

Incidentally, I plugged in your code, and it's not quite right.
 

jo-thijs

Member
It does work though.
Have you given in the correct coordinates for X1, Y1, X2, Y2?
Depending on which object executes (I'll assume object A)
and depending on the relative locations of the objects (I'll assume like in the image)
and depending on the sprite origins (I'll assume top left corner for both sprites), they should be:
X1 = x
Y1 = y
X2 = x + sprite_width
Y2 = y

The variables NX and NY are the new positions for object B.
 
It's not far off, it seems, but it's not working quite right.
OBJ_A (SPRITE_A) does whatever it wants, and OBJ_B (SPRITE_B) should be drawn as described above. Here is the step event code for OBJ_B
Code:
/// STEP EVENT FOR OBJ_B || SPRITE_B
var X1 = x;
var Y1 = y;
var X2 = obj_A.x + sprite_width;
var Y2 = obj_A.y;

var tdist = point_distance (X1, Y1, X2, Y2);
var tdir  = point_direction(X1, Y1, X2, Y2) + image_angle;

x = X1 + lengthdir_x(tdist, tdir);
y = Y1 + lengthdir_y(tdist, tdir);

image_angle = obj_A.image_angle;
 

jo-thijs

Member
X1 should be set to obj_A.x and Y1 to obj_A.y in that case.
image_angle should be set to obj_A.image_angle at the start of that code, not at the end.
And also, make sure this code is executed after obj_A has set its image_angle.
 
Top stuff jo-thijs! Currently working nicely, I really appreciate it.
For anyone else looking to achieve the same effect; here is the complete working code:

Code:
/// STEP EVENT FOR OBJ_A
// Keep the degree values between 0 and 360
if image_angle > 360
    image_angle -= 360;

if image_angle < 0
    image_angle += 360;
Code:
/// STEP EVENT FOR OBJ_B
// OBJ_B stays locked to OBJ_A, spinning and relocating like a "hand on a clock"

image_angle = obj_A.image_angle;

var X1 = obj_A.x;
var Y1 = obj_A.y;
var X2 = obj_A.x + sprite_width;
var Y2 = obj_A.y;

var tdist = point_distance (X1, Y1, X2, Y2);
var tdir  = point_direction(X1, Y1, X2, Y2) + image_angle;

x = X1 + lengthdir_x(tdist, tdir);
y = Y1 + lengthdir_y(tdist, tdir);
 
Top