OK, this is probably going to be kind of hard to explain, but I'll give it a shot: :)
I've written a MoveEntity function for my 2d vector entity lib, but I'd like to check with someone (Floyd? :) who has a better math brain than me that I'm not doing things the long/slow way.
I thought this function was going to be fairly straightforward to implement... which it was, until you consider moving a child entity whose parent has a non-unifrom scale.
In this case, the child's movement vector is in terms of the parent's scaled coordinate system - it's X units might be twice the size of it's Y units, for example.
Because of this, I've been struggling to scale the movement vector correctly to fit the parent's coordinate system scale. The movement vector needs to be transformed into the parent's coord system becasue this is where the child entity 'lives' - i.e. it's position is relative to it's parent.
I think I've finally found a solution to this, but it requires 2 Sqr calls, and the whole process seems a bit 'mechanical' to me. Maybe it's the only way, but I was wondering if there's a more elegant way to achieve the same results?
Here's the (semi-pseudo) code I'm using:
P.S. I did infact find an alternative method without using the 2 Sqr calls, but it required 2 ATan2 calls, a Sin and a Cos. I figured the Sqr method was better and maybe (slightly) faster?
Thanks for any knowledgeable opinions! :)
I've written a MoveEntity function for my 2d vector entity lib, but I'd like to check with someone (Floyd? :) who has a better math brain than me that I'm not doing things the long/slow way.
I thought this function was going to be fairly straightforward to implement... which it was, until you consider moving a child entity whose parent has a non-unifrom scale.
In this case, the child's movement vector is in terms of the parent's scaled coordinate system - it's X units might be twice the size of it's Y units, for example.
Because of this, I've been struggling to scale the movement vector correctly to fit the parent's coordinate system scale. The movement vector needs to be transformed into the parent's coord system becasue this is where the child entity 'lives' - i.e. it's position is relative to it's parent.
I think I've finally found a solution to this, but it requires 2 Sqr calls, and the whole process seems a bit 'mechanical' to me. Maybe it's the only way, but I was wondering if there's a more elegant way to achieve the same results?
Here's the (semi-pseudo) code I'm using:
; Child's movement vector. We want to move the child 1 unit along it's positve X axis ; regardless of it's current rotation! vx# = 1.0 vy# = 0.0 ; Rotate movement vector to match child's global rotation. cos_rot# = Cos(child_global_rot#) sin_rot# = Sin(child_global_rot#) global_vx# = (vx * cos_rot) - (vy * sin_rot) global_vy# = (vy * cos_rot) + (vx * sin_rot) ; Rotate global movement vector into parent's coord system. cos_rot# = Cos(-parent_global_rot#) sin_rot# = Sin(-parent_global_rot#) parent_vx# = (global_vx * cos_rot) - (global_vy * sin_rot) parent_vy# = (global_vy * cos_rot) + (global_vx * sin_rot) parent_vx = parent_vx / parent_global_scale_y# parent_vy = parent_vy / parent_global_scale_x# ; At this point, our parent vector has the correct direction, but it's ; magnitude/length needs to be scaled to match the parent's coord system scale... ; Find magnitute/length of the orignal movement vector ; and the magnitude of the equivalent vector in the parent's coord system. global_mag# = Sqr(vx * vx + vy * vy) parent_mag# = Sqr(parent_vx * parent_vx + parent_vy * parent_vy) ; Scale the parent's vector to match the magnitude of the original movement vector, ; but specified in terms of the parent's coord system scale. move_vx = (parent_vx / parent_mag) * global_mag move_vy = (parent_vy / parent_mag) * global_mag ; Finally, move the child. child_x = child_x + move_vx child_y = child_y + move_vy
P.S. I did infact find an alternative method without using the 2 Sqr calls, but it required 2 ATan2 calls, a Sin and a Cos. I figured the Sqr method was better and maybe (slightly) faster?
Thanks for any knowledgeable opinions! :)