# Help out my Archers please...

This topic is 2309 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So I have many small archers and I want to draw their arrows as pixels. I need a working solution that doesn't require a lot of calcs,like sines and arctans cause there are many many archers...

So to define the problem in terms of variables:

2. Arrow can fly to any endpoint, call it x2,y2
3. Now pretend you draw a line from x1,y1 to x2,y2
4. Now take the middle of the line and move it away a distance "h", end points don't move.
5. I need an arc, starting at x1,y1 and ending at x2,y2, with a height of h... make sense?

I need an efficient algorithm for calculating this, Here's how I'm drawing a straight line from x1, y1 to x2, y2

[source]
int x1,y1,x2,y2,h;
float Dist, Angle;
//---------------------------------------------------------------------------------------------------------------

// These can be any combination of numbers
x1=90; y1=90; x2=300; y2=295, h=50;

Graphics.DrawLine(x1, y1, x2, y2, Red, *g2); //From my Graphics Library

Dist=sqrt(float(x2-x1)*float(x2-x1)+float(y2-y1)+float(y2-y1));
Angle=atan(float(y2-y1)/float(x2-x1) ) ;

for(i=0;i<Dist;i++)
{
Graphics.SetPixel(int(x1+i), int(y1+i*tan(Angle)), RGB(255, i, i), *g2);
}

[/source]

I'd like to have little to no trig functions like sine or tan if possible...

All my men are in a 2d iso view, so I don't think gravity formula's will work well as the arc would come out weird, if everything was 2d then gravity would work fine...

I was thinking of using the equation of an ellipse and rotating it but I think it'll look to bulgy or rounded on the ends, plus it would use lots of time consuming eqn's with sqrt's....

Hope that made sense?

Any help would be appreciate...

Vanz Edited by rhuala

##### Share on other sites
How about something like:

Since you are representing it as a pixel you don't need to get a perfect line so..

when the arrow is fired you calculate these:
distance = (endpoint - startpoint).length();
totalFlightTime = distance / arrowSpeed;
direction = (endpoint - startpoint).normalize();
offset.x = -direction.y
offset.y = direction.x
//if you want the offset to always point upwards just flip the signs on both components if offset.y is negative
thus:
if (offset.y<0) {
offset.y = -offset.y;
offset.x = -offset.x;
}
offset = offset.normalize();

Now things can be kept fairly simple for the per frame update:

to get the x.y position on a straight line we just do:
currentPos = startPoint + direction*time*velocity;
to get an arc shape we probably want a curve with its peak at half the flighttime, since our offset is normalized we can do:
timeFraction = (time/totalFlightTime)-0.5;
offset.scale(timeFraction*timeFraction*arcHeight);
currentPos += offset;

The only expensive operations here are the vector normalizations and length calculation (1 sqrt each) but they are only done when the arrow is fired so it shouldn't be a big deal. (3 total per arrow), the part that has to be done for each step on the way is extremely lightweight. (The biggest problem with this approach is that quite a bit of data has to be stored for each arrow which could cause problems for the CPU cache if you got an extremely high number of arrows that are updated on a per frame basis in which case it might be better to only store the startpoint, endpoint and start time for each arrow and recalculate the rest each frame (the CPU is alot faster than RAM so if you can't fit it all in cache it will probably be faster to do a few extra calculations)) (If you draw out the entire arc for one arrow at the time rather than animate the arrows as flying pixels the potential cache issue becomes irrelevant but that won't be as cool ) Edited by SimonForsman

##### Share on other sites
[indent=1]I agree, the blue line looks weird for an Iso view. One thing you have to keep in mind, when you add an arc shot in Iso, it is hard to tell if it goes up or right. If you have an Action RPG and the player should evade the arrows, an arc may be frustrating for the user, because it is hard to tell where it's target is. If you have a strategy game, it might look better with an arc.

[indent=1]e: didn't see the first response
Edited by Bluefirehawk

1. 1
2. 2
Rutin
19
3. 3
4. 4
khawk
15
5. 5
A4L
13

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633743
• Total Posts
3013644
×