ID:2215479
 
(See the best response by Ter13.)
Hi,

I've been having a hard time spawning a projectile exactly at the source's location. What I was trying to do is to spawn it directly at the center then make it move to the direction the source is facing. I was somehow able to get the desired result except when the source is situated between tiles.
Here's a preview:
Centered on a tile

and then the problem...


Here's how it should look like:


It's basically using its iris as a projectile.

I'm simply spawning the projectile at the source's location through:
obj/projectile
New(shooter)
loc = locate(shooter.x,shooter.y, shooter.z)


I know there are a lot of libraries out there that handles projectiles but I sort of find them a little bit complicated to understand at times. What I am trying to achieve is a simple system.

I would greatly appreciate it if someone could help me out with this issue.


Thanks in advance!
Check out Absolute Positioning.

Once you install that library and link it to your project, all you need to do is:

projectile.SetCenter(source.Cx(), source.Cy(), source.z)


This makes sure the projectile is centered relative to source's bounding box no matter where they are. From there you can do whatever you want.
Best response
To answer the question more directly:

calculate the absolute position of target:

var/ax = target.x*TILE_WIDTH + target.step_x - target.bound_x
var/ay = target.y*TILE_HEIGHT + target.step_y - target.bound_y


Calculate the relative bound width/height:

var/rw = target.bound_width - src.bound_width
var/rh = target.bound_height - src.bound_height


add half the relative bounds

var/nx = ax + rw/2
var/ny = ay + rh/2


Convert into tile-step coordinates:

var/turf/t = locate(nx/TILE_WIDTH,ny/TILE_HEIGHT,target.z)
var/sx = nx - t.x*TILE_WIDTH + src.bound_x
var/sy = ny - t.y*TILE_HEIGHT + src.bound_y


You can condense this code more too:

var/nx = target.x*TILE_WIDTH + target.step_x - target.bound_x + (target.bound_width - src.bound_width)/2
var/ny = target.y*TILE_HEIGHT + target.step_y - target.bound_y + (target.bound_height - src.bound_height)/2
var/turf/t = locate(nx/TILE_WIDTH,ny/TILE_HEIGHT,target.z)
src.Move(t,target.dir,nx-t.x*TILE_WIDTH+src.bound_x,ny-t.y*TILE_HEIGHT+src.bound_y)
Thank you for those answers!
I was considering the Absolute Positioning library but it bugs me to try to learn the way of doing it without the lib.

I'd like to give you two a vote for the quick response but I guess I can only give it to one.

Both of the answers are absolutely helpful though.