How to spawn an entity adjacent to you, position depending on your angle and speed?


(John Andersson) #1

Ok, so I am using this code to move individual soldiers:

angle = FP.angle(x, y, world.mouseX, world.mouseY);
x += speed * Math.cos(angle * FP.RAD);
y += speed * Math.sin(angle * FP.RAD);

If you kill someone, I want another soldier to appear behind you and follow you.

If you are walking at an angle of 90*, I want the new soldier to appear at an angle of 270, a.k.a if you are walking upwards whilst killing someone, I want the new soldier to appear below you.

If you are walking at an angle of 0*, I want the new soldier to appear at an angle of 180, a.k.a if you are walking to the right whilst killing someone, I want the new soldier to appear to the left of you.

In what way can I use the angle of the player to calculate where to put the new soldiers? I know I will have to put them a certain length away from the player, and a constant will be used for that, but I can’t figure out the angles and math for this!

I know that I could use something like Math.abs(angle - 180) to get the “opposite” degree, but how would I convert that to a Point for the new soldier to spawn on? And how do I account for the speed of the original soldier?

Essentially;

I want a trail of soldiers walking in a line, following your mouse. They are supposed to add up in numbers as you kill, and they should not walk over each other.

I can take care of the problem of everyone bundling up by using a timer of some sort, I guess, but not where to spawn them.

Thanks

EDIT: Seems like I can’t even make them not bundle up -.-


(Bora Kasap) #2

you just need a little modify on this when creating your soldiers like that: you need that the same “angle” variable of course;

as example if you’re creating your new soldier inside your player class when it kills somebody::

var newsoldier:Entity = world.add(new Soldier());
newsoldier.x = x + distancefromplayer * Math.cos((angle+180) * FP.RAD);
newsoldier.y = y + distancefromplayer * Math.sin((angle+180) * FP.RAD);