PDA

View Full Version : Arrow shooting combat system



Warheart
February 25th, 2009, 04:38 AM
Hey all,

Been a long time since I've been on here with high school and all. Working on a basic little combat system at the moment. All it is is one MC for the character, one for the enemy and one for the arrow. Shooting the arrow, rotation all that is fine, the only problem i'm having is finding a good way to set the speed of the arrow. At the moment i'm using:


if (_root.Xdistance<_root.Ydistance) {
_root.movex = 1;
_root.movey = _root.Ydistance/_root.Xdistance;
} else if (_root.Xdistance>_root.Ydistance) {
_root.movex = _root.Xdistance/_root.Ydistance;
_root.movey = 1;
}For most times it works fine, eg if the Xdistance is 5, Ydistance is 10, u get movex of 1, naturally, and movey of 2, meaning it moves towards the target along the angle perfectly, but when you get a bigger gap between the distances the speed greatly increases, for example Xdistance is 5, Ydistance is 150, you get one again for movex, but you get 30 for movey and as you can imagine, it will move rather fast.

I've tried different ways to get a good speed function working but this is the best i could come up with =/ if anyone has some ideas/suggestions then that'd be great. Thanks in advance -

-Grant

p.s. i have looked on the internet for ideas but couldnt find anything

Edit: thought id upload the .fla

Blackroot
February 25th, 2009, 05:47 AM
Try finding the angle and distance between the two points, A and B and go from there.



Angle between points:
Math.atan2(By - Ay, Bx - Ax)

Distance between points:
sqrt((Bx-Ax)^2 + (By-Ay)^2)


You then know the angle and the distance. I'd increment X/Y by some portion of the distance:



xIncrement = Math.cos(angle) * (distance/64)
yIncrement = Math.sin(angle) * (distance/64)


64 is just some abstract number, it really depends on the rate you're incrementing the objects speed at. Also, this doesn't account for parabolic motion, which an arrow would most certainly follow. However, I'd say you should tinker around with the above code for the speed portion.

Warheart
February 25th, 2009, 06:21 AM
thanks, got a few problems with it though, at alot of different angles the arrow just doesn't move, and for the ones it does it goes in a totally random direction. And im not too worried about bringing in the technical parabolic motion and changing speeds depending on the arch of the arrow etc, just aiming to complete something basic for now

Warheart
February 25th, 2009, 07:37 AM
well i fixed it, with a bit of good ol' graph paper and testing i realised all i had to do was swap things around, instead of having one number being one, and the other being larger then one, which was logically determining the speed, i changed it to:



if (_root.Xdistance<_root.Ydistance) {
_root.movex = _root.Xdistance/_root.Ydistance;
_root.movey = 1;
} else if (_root.Xdistance>_root.Ydistance) {
_root.movex = 1;
_root.movey = _root.Ydistance/_root.Xdistance;
}

It has just swapped it so that it will be, one number is one and the other is between 0 and 1, there may still be some differential in speeds, but because its a decimal its virtually unnoticeable.