|
|
|||||||
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
|
#1 |
|
Senior Member
Join Date: May 2001
Location: Sweden
Posts: 197
|
Don't let the length of the tread frighten you.
![]() I'm working on a game much like the old DOS classic Scorched Earth. http://www.rydeman.com/erik/tanks3.html I have now come to a point in development when it would be a good idea to start poking the AI for opponents a little. What I need is for the enemy tank to calculate the rotation/power combo that will hit another tank bullseye. After that I'll add some randomness to that depending on difficulty. Anyway, here's how I calculate the projectile's path (all the hittest crap etc. of the function removed of course): Code:
on(enterFrame){
count++
this._x =startX+power*count*Math.cos(-rotation*Math.PI/180)*AR;
this._y =startY-(power*count*Math.sin(-rotation*Math.PI/180)-0.5*gravity*count*count);
}
Again, the task: Getting the best power/rotation combo! For those who wish to know what these values do: Power varies between 0 and 100 but is multiplied with 0.21875 above. you do not need to know this. rotation varies between 0 and 180 but we can limit it to 0-90 by comparing x-coords or maybe 45-90 by rationalising. statX/startY= the position of the tank fireing. this._x/this._y = the position we want the projectile to be at ie the other tank's position. AR=air resistance(0.95). gravity = the acceleration down of pixels/(frame*frame) (=0.5) Ideas for solving it (Allah knows I've tried): Getting rid of count by replacement and put the derivative of rotation as a function of power to 0. (or the other way around) Making power a high constant and just calculating the rotation. this will look kinda bad but a possible if-all-else-fails solution. or if all else *really* fails making a few thousand calculations comparing all combos and picking the best one. Anyone that works out a *good working* solution *will* be mentioned in the credits! ![]() Even if you don't have the time to have a go at it, ideas for solving it or comments about the game so far is greatly appreciated. [Edited by Rydeman on 07-15-2001 at 04:49 PM] |
|
|
|
|
|
#2 |
|
Senior Member
Join Date: May 2001
Location: Sweden
Posts: 197
|
*bump* Somebody say something.
This thread is getting one-sided.
|
|
|
|
|
|
#3 |
|
Senior Member
Join Date: Feb 2001
Posts: 475
|
hi
well an idea woudl be doing it like the binary search (guessing ) system does it.1. let the tank shoot with some power and some angle 2. measure the distance to the target 3. figure out if you shot in front of the target or behind 4. increase/decrease power/anlge by some value 5. see point 1. but i think this is only necessary when doing real high complex calculations WITHOUT knowing the influences of the environment to the projectile (wind,...) - yours is not that complex plus you know the exact position of the target. Just calculate the optimal shot using standard physics formula calculating a projectiles flight and reverse it. you "know" already where the bomb will fly to, what you want to know is how it flys there Well you might say then "okay, but there are 2 variables - angle AND power". Well, as u said: limit the calculation to find the necessary power for every angle until it hits. there are only 45 (or 90 if u use 0.5 angles too) different angles to work with, that should be no problem for any pc being pentium class.So create a loop that increases both (power and angle) until it hits the target or shoots too far - when shot was too far keep angle but modify power. Yours HTD BTW the angles needed are only "45 to 90" starting from the ground. u can also use 0 to 90 but i think this is unnecessary. |
|
|
|
|
|
#4 |
|
Senior Member
Join Date: Jul 2000
Posts: 198
|
Here's an idea... havent quite worked it out yet... you could put everything in a while loop (while (_x<>targetx))or somethin like that.. for each time it loops through, you would find the rotation value using algebra, then store it in an array... once you are done looping through... call the rotation values from the array, for each frame... I hope that makes sense
|
|
|
|
|
|
#5 |
|
Senior Member
Join Date: May 2001
Location: Sweden
Posts: 197
|
Thanks for the ideas fellas.
![]() I meesed around bigtime with while loops the other day BC... nothing good came out of it with the inverse formula since they just changed all three values to fit in a non-possible solution... Hehe, mr Duck hit the spot riht on. I don't know why, but I always come up with the great ideas when giving up and going to bed after a night of beating the script around.Your solution is basicly what I worked out today! I added about 200 lines of code to the enemy tank checking the last explosion coords and then increasing/decreasing angle/rotation depending on a bunch of things. http://www.rydeman.com/erik/tanks3.html Edit: Eeeep, just noticed a bug locking the game up on occasion! I'll see if I can find out what it is... [Edited by Rydeman on 07-15-2001 at 07:09 PM] |
|
|
|
|
|
#6 |
|
Senior Member
Join Date: May 2001
Location: Sweden
Posts: 197
|
bug fixed. I hate variable spelling errors...
|
|
|
|
|
|
#7 |
|
Senior Member
Join Date: Jul 2000
Posts: 198
|
Lookin good Rydeman... just wonderin how you did the destructable terrain.... I dont need actual source... just somethin to give me an idea. It could be very useful for games.
|
|
|
|
|
|
#8 |
|
Senior Member
Join Date: May 2001
Location: Sweden
Posts: 197
|
aye, I'm very proud of the terrain.
![]() Basicly I have 320 evenly spread 2-pixel-wide pillars with 480 frames. At frame 1 the pillar is at a height of 1 pixel and the highest 480. motiontween. Then as a bomb goes off I get the location, translate the _x coordinate to the proper pillar MC, slap up the mathematical formula for a halfcircle (which I figured out myself! ) ranging from pillar-radius to pillar+radius.Then I check how much of the pillar is above the explosion (ycoord-the halfcircle function) and if it's more than halfcircleY *2 subtract halfcircleY*2 from the pillars frames. Otherwise subtract just as much as is above the lower limit of the explosion. Did that make any sense? otherwise just use the top part and figure the rest out for yourself.
|
|
|
|
|
|
#9 |
|
Senior Member
Join Date: Apr 2001
Posts: 308
|
hi
just to make sure i understand this, you have two points and you want to know what combination of speed and angle will satisfy by passing through the two points. i did a similar thing a while back. however that was for a tennis ball passing through three points. i will have a dig around and find the papers, and hopefully report a solution back soon. matthew |
|
|
|
|
|
#10 |
|
Senior Member
Join Date: Apr 2001
Posts: 308
|
hopefully this is what you need.
im sorry about the notation but its not easy with only the key board and there are no microsoft word symbols. any how, heres a qucik introduction say you have two dots as your tanks, find the x co-ordinate difference, and the y co-ordinate difference, i assume this is no problem one more thing, for some reason flash kit does not like spaces at the begining of the line, so where i have used ` it is not to mean any thing, just to keep formating | | represents a vector ie | | = x component | | = y component |0| |g| = a intergrate the aceleration vector |u | |u-gt| = v intergrate the velocity components |ut | |ut - 0.5gt*t| = r this gives us the position vectors, bringing in sine and cosine |utcos@ | |utsin@ - 0.5*g*t*t| = r this is the majority of what you need to be able to claculate the required velocty and angle of projection. but to make it more useful we need it as one equation, so here goes x = utcos@ ... equation 1 y = utsin@ - 0.5*g*t*t ... equation 2 rearange equation 1 in terms of t x/(ucos@) = t ... equation 3 sub equation 3 into equation 2 y = u*x*sin@`````````g*x*x ````---------- - --------------- `````u*cos@ 2*u*u*cos@*cos@ simplifing gives y = xtan@ -````g*x*x ```````````------------ ``````````2*u*u*cos@*cos@ now if we reange in terms of u we get 2*u*u =````````-g*x*x ```````---------------------- ````````y - x*tan@)*cos@*cos@ then by a further bit of simple rearaging we get ``u =`````````````-g*x*x ````````( ------------------------ )^0.5 ````````2*(y - x*tan@)*cos@*cos@ hopefully i have not made any erros in the math there, but more its possible i have, ill check it latter just to make sure. using the final equation this will give you the speed to hit the target for various angles. i woud suggest setting priority speeds, so it starts at 50 unit speed than used the angle. if there is a mountian in the way than it agjusts the angle by say 10 degrees, and calcuates the required velocity of the bullet. this way it does not appear to perfect, and appears to have some inteligence any way good luck, let me know if this is what you need, and how you get on. sorry about the spelling ![]() matthew |
|
|
|
|
|
#11 |
|
Senior Member
Join Date: Apr 2001
Posts: 308
|
did this work?
|
|
|
|
|
|
#12 |
|
Senior Member
Join Date: May 2001
Location: Sweden
Posts: 197
|
Oh, the equation looks nice! I have yet to test it.
Slightly confused though, which part is ^0.5? all of the below part of the division (don't know what the name is in English)? if this works it sure will come in handy! You have my deepest thanks! |
|
|
|
|
|
#13 |
|
Senior Member
Join Date: Aug 2000
Posts: 1,030
|
I think the word is "remainder". I must say I'm totally impressed by the whole thing. Just like the orriginal but in my favoured medium. I would think that your system of 320 2px animated mc's would have taken you ages! Did you perhaps consider just using 4px square mcs and duplicating them?
Are you going to allow for caverns? Remmeber the original had cavern maps with a ceiling as well as a floor... how about rollers? (My personal favourite weapon). |
|
|
|
|
|
#14 |
|
Senior Member
Join Date: Apr 2001
Posts: 308
|
^o.5 simply means raising the whole equation to the power of 0.5 this is the same as square rooting the whole equation
|
|
|
|
|
|
#15 |
|
Senior Member
Join Date: Aug 2000
Posts: 1,030
|
Yeah that's what maths says, but Flash thinks differently. 6^2 = 36 right?
Code:
trace (6^2); Code:
trace (Math.pow ( 6, 2 )); |
|
|
|
|
|
#16 |
|
Senior Member
Join Date: Apr 2001
Posts: 308
|
the only reason i used the ^0.5 was because i did not have a suitable square root key on my key board
|
|
|
|
|
|
#17 |
|
Senior Member
Join Date: Aug 2000
Posts: 1,030
|
yeah yeah, I know that, I'm just pointin it out for Rydeman b/c it took me a while to figure out
|
|
|
|
|
|
#18 |
|
Senior Member
Join Date: Feb 2001
Posts: 475
|
hm... math is of course the best solution. But i thought in this case that's not necessary - that's why i posted a weird explanation of how he could do it
instead of telling him to integrate his formulas to get the bomb-curve needed for any coordinates...Again, the math way is the best - great work done here!! Yours HTD |
|
|
|
|
|
#19 |
|
Senior Member
Join Date: May 2001
Location: Sweden
Posts: 197
|
Wohoo! The formula works!!!
Thanks a bunch mate!What would you like to be called in the 'thanks to' section of the credits? ![]() People might get strange ideas if I state that *McChicken Nuggets* helped me solve a complicated equation. ![]() Perhaps I should show this game to my physics teacher once school starts? |
|
|
|
|
|
#20 |
|
Senior Member
Join Date: Apr 2001
Posts: 308
|
no problem
im pleased it worked. i did some math along similar lines a while ago. only it was slightly more complicated, but very similar principles. i knew my maths would have some use after school, and flash is an excellent place for it D)
in the `thanks to` section you can use my reall name, Matthew Warneford, thanks as a favour in return could you check out the site of the company i work for. its aim at teenagers, and we would love some feed back. http://www.dubit.co.uk depending on what time you go it will be very busy or quite. its busy around 8 pm uk time. or often during uk working hours thanks matthew can you email me the game when youve finished it id love to see how you get along thanks |
|
|
|
![]() |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | |
|
|