GameMaker Attaching or pinning sprites to each other

J

jobel

Guest
hi, I have a little spaceship that moves around the screen. I'm trying to "pin" a thruster animation to it's rear.

I want to attach spr_thruster to the obj_ship so it looks like part of the ship, so when the ship rotates, the thruster will stay where it should.

I want to always keep it attached because I want it visible while the ship is moving and not visible when the ship stops.

I assume I need to make a obj_thruster? I tried using length_dir and in obj_thruster's step event, but it lagged behind and would not rotate correctly.

Is there any way to make "image points" on the sprite I want to attach it to?

obj_thruster:Step
x = obj_ship.x + lengthdir_x(-5, image_angle);
y = obj_ship.y + lengthdir_y(-5, image_angle);

image_angle = obj_Player.image_angle + 180;
 

YoSniper

Member
If the thrusters have collisions associated with them, then a separate object would be necessary.

Otherwise, you could just draw the thruster sprite in the Draw Event, in addition to the spaceship itself.
 
J

jobel

Guest
@YoSniper oh, never thought of that... they don't have any reason for collision. so it doesn't even need an obj?
 

woods

Member
another approach would be to have 2 sets of sprites for the ship.. one with thrusters active, and the other with thruster inactive.. then switch between the two while moving...
 

YoSniper

Member
Yes. If the collision mask of the thrusters is irrelevant, then you can do something like this:

Draw Event
Code:
//First, draw thrusters if applicable. This will put them BEHIND the spaceship
if thrusters_on {
    draw_sprite_ext(spr_thrusters, thruster_index, x, y, image_xscale, image_yscale, image_angle, c_white, image_alpha);
}
//Next, draw the spaceship ON TOP of the thrusters, if they were drawn
draw_self();
Note that the above did not take into account where the thruster sprite is supposed to appear relative to the ship. You would also have to define the thruster_on condition.
 
J

jobel

Guest
@YoSniper it keeps the origin the same, but as the player's ship rotates (top down view) the thruster's pos gets messed up... it has to be relative to the ship's origin. I'm trying to "pin" the Position and Angle.

So if the origin is xy200/200 and I want to attach the thruster at xy150/200 when the whip rotate and is facing 90 degrees, the origin is still xy200/200 but not the thruster needs to be at xy200/250

the Rotation happens in obj_Ship Step

(trying to do the asteroids game tutorial and give the ship a thruster! )
 
Last edited by a moderator:

YoSniper

Member
I figured origin offsets would come into play. You have to adjust the drawn x and y to accommodate that.

Given your sprite origins (and assuming the spaceship is facing to the right at image_angle = 0,) here's how to calculate them.

At image_angle = 0, the thruster is 50 pixels to the left and 0 pixels down
At image_angle = 90, the thruster is 0 pixels left and 50 pixels down

Code:
var x_prime = x - 50 * dcos(image_angle);
var y_prime = y + 50 * dsin(image_angle);

if thruster_on {
    draw_sprite_ext(spr_thruster, thruster_index, x_prime, y_prime, image_xscale, image_yscale, image_angle, c_white, image_alpha);
}
draw_self();
 
U

Ushugun

Guest
Isnt it easier to just replace the sprite of the ship with a sprite that has the truster? And switch it back to the old sprite when not moving?
 
J

jobel

Guest
@YoSinper excellent! that was it! I'm surprised there's no built-in function for this already... there should be a function like: pin(spr_image,rel_x,rel_y);
 
J

jobel

Guest
Isnt it easier to just replace the sprite of the ship with a sprite that has the truster? And switch it back to the old sprite when not moving?
well that would be a waste of resources, right? the ship has 20 frames of animation and the thruster is just one frame that I flicker the opacity... oh that reminds me.. how do I do a blend mode of "Addition"?? or Additive...
 
J

jobel

Guest
got it!
Code:
var x_prime = x - 50 * dcos(image_angle);
var y_prime = y + 50 * dsin(image_angle);

if (spinning == true)
   {
   gpu_set_blendmode(bm_add);
   draw_sprite_ext(spr_player_thruster, -1, x_prime, y_prime, image_xscale,image_yscale,      image_angle+90, c_white, random_range(0.0,1.0));
   gpu_set_blendmode(bm_normal);
   }
thanks everyone!
 
Last edited by a moderator:
J

jobel

Guest
@YoSinper I found an issue when using this:

Code:
var x_prime = x - 50 * dcos(image_angle);
var y_prime = y + 50 * dsin(image_angle);
it only let's me set position to the relative y, not x. any ideas? i,e, 32x32 sprite's origin is x16/y16 if I want to "pin" another sprite onto it at x3/y16 this code won't work. It will only let me place it along the Y axis.
 

YoSniper

Member
The equations I gave you omitted extra terms because they evaluated to zero in the given case.

If the sprite origin of the spaceship is (x1, y1) and the sprite origin of the attached sprite is meant to be at (x2, y2) when the spaceship is at image_angle = 0, then here are the full equations used to place the sprite properly.

At image_angle = 0, the x offset is x2 - x1. At image_angle = 90, the x offset is y2 - y1 (rotate an image 90 degrees to the left to see how this works.)

Similarly, at image_angle = 0, the y offset is y2 - y1. At image_angle = 90, the y offset is x1 - x2, or -1 * (x2 - x1).

Code:
var x_prime = x + (x2 - x1) * dcos(image_angle) + (y2 - y1) * dsin(image_angle);
var y_prime = y + (y2 - y1) * dcos(image_angle) - (x2 - x1) * dsin(image_angle);
To reiterate, the term (y2 - y1) was zero in your original problem, so those terms cancelled out.
 
T

Timothy

Guest
Instead of all the math, you can also just adjust the origin of the thruster sprite so that when it's drawn at the x and y of the ship and with the same angle, it's in the right position.

Math is necessary for some things (bullet spawns for instance) but this isn't one of them.

Edit: unless of course I'm mistaken on the circumstances.
 

YoSniper

Member
Instead of all the math, you can also just adjust the origin of the thruster sprite so that when it's drawn at the x and y of the ship and with the same angle, it's in the right position.

Math is necessary for some things (bullet spawns for instance) but this isn't one of them.

Edit: unless of course I'm mistaken on the circumstances.
It depends on how much transparency or empty space you want in your sprites.
 
J

jobel

Guest
@YoSniper I'm going to try that out, now that you write it out that makes sense, it needs to subratact the x's and y's... thanks so much..nice maths!

@Timothy yes this is true, but you would need to figure out that position. So do you mean...let's say the ship was 250x250 and the thruster was small at only 16x8, you'd make a 280x250 image with the thruster only and set the origin as the same as the ship? seems like it would waste a lot of space and be hard to line up.
 
T

Timothy

Guest
It depends on how much transparency or empty space you want in your sprites.
@YoSniper I'm going to try that out, now that you write it out that makes sense, it needs to subratact the x's and y's... thanks so much..nice maths!

@Timothy yes this is true, but you would need to figure out that position. So do you mean...let's say the ship was 250x250 and the thruster was small at only 16x8, you'd make a 280x250 image with the thruster only and set the origin as the same as the ship? seems like it would waste a lot of space and be hard to line up.
Both of you are assuming that the origin can't be outside of the sprite. No need for a big image.
 
J

jobel

Guest
Both of you are assuming that the origin can't be outside of the sprite. No need for a big image.
but that would be even harder to line up though...but I think that could work with trial and error. actually probably the same as the math coord

I'm surprised GMS2 doesnt have "Image Points" that you can reference on the sprite so you can easily attach things.
 
Last edited by a moderator:
T

Timothy

Guest
but that would be even harder to line up though...but I think that could work with trial and error. actually probably the same as the math coord

I'm surprised GMS2 does have "Image Points" that you can reference on the sprite so you can easily attach things.
Yeah anchors would be a cool thing.

But it's not that hard to adjust the origin. Just a little addition and subtraction.
 
Top