Windows 'Faking' the origin point of a sprite [SOLVED]

W

Wintermute()

Guest
I have an issue where having the center of my sprite as the origin is necessary for correct movement, but it doesn't look good when I am creating ballistic missiles at the sprite's x/y point, as they come out of the middle of the sprite, and not the 'barrel of the gun' if you get my drift...

Been looking how to do this in the gms help, is sprite_set_offset the right command to use ? my question - I'm using this code but it isn't working, can anyone let me know where I've gone wrong ? this assumes I've created a new sprite (spr_faker) with the origin point at the barrel of the gun as a clone of the original (spr_orig) :

sprite_assign(spr_faker, spr_orig);
sprite_set_offset(spr_faker, sprite_get_xoffset(spr_orig), sprite_get_yoffset(spr_orig));

?

thank you!
 
P

ParodyKnaveBob

Guest
Howdy, Wintermute(),

The sprite_set_offset() page subtly mentions editing the sprite itself; from the Contents tab, you can see its parent page, Sprites, has a Manipulating Sprites header which mentions something similar, and that you shouldn't use those functions when already referencing.

In other words, no, this is not what you want. That function doesn't "fake" the origin -- it actually changes the sprite's origin for future use! And, it can have unexpected behavior if it's called when an active instance is already using the sprite in the room.

Depending where your barrel ends and what the sprite's mask looks like, you might be able to get away with just using the bbox_left and bbox_right variables. Otherwise, it sounds like you'll need to add or subtract some constant amount (depending on facing) from your x value.

Tell me if this makes sense or not,
Bob $:^ J
 
W

Wintermute()

Guest
Guys, thanks -

so more like this, from the link nightfrost posted:

instance_create(x + lengthdir_x(64, image_angle), y + lengthdir_y(64, image_angle), obj_bullet);

and I'd just substitute my x/y pixel measurements relating to the gun barrell for 64 ?
 
W

Wintermute()

Guest
Just to simplify this for anyone else interested....

The following is the code I used to make a bullet appear at the tip of the gun of a fast moving sprite that moves in 360 degrees:

this assumes the tip of the gun is (x) 38 pixels to the right of the sprite origin and is (y) 0 pixels below the sprite origin. (use sprite editor to get the x,y)

Len = point_distance(0, 0, 38, 0);
Angle = point_direction(0, 0, 38, 0);
bullet_id = instance_create(x + lengthdir_x(Len, Angle + image_angle), y + lengthdir_y(Len, Angle + image_angle), obj_bullet);
bullet_id.direction = image_angle;
bullet_id.speed = 10;

this has made the game much more accurate in shooting while on the move, now I have to make my AI a bit deadlier to compensate :)
 
Top