Jump to content
  • Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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:

1. Start with any point x1,y1
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

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

Angle=atan(float(y2-y1)/float(x2-x1) ) ;

Graphics.SetPixel(int(x1+i), int(y1+i*tan(Angle)), RGB(255, i, i), *g2);



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 this post

Link to post
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
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;
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 smile.png) Edited by SimonForsman

Share this post

Link to post
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

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!