To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here


A Flash Developer Resource Site

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Reply
 
Thread Tools Search this Thread Display Modes
Old 07-14-2001, 11:42 PM   #1
Rydeman
Senior Member
 
Join Date: May 2001
Location: Sweden
Posts: 197
Lightbulb

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);
}
All values are constant except for rotation, power and 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]
Rydeman is offline   Reply With Quote
Old 07-15-2001, 05:52 PM   #2
Rydeman
Senior Member
 
Join Date: May 2001
Location: Sweden
Posts: 197
*bump* Somebody say something. This thread is getting one-sided.
Rydeman is offline   Reply With Quote
Old 07-15-2001, 06:00 PM   #3
HowardTheDuck
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.
HowardTheDuck is offline   Reply With Quote
Old 07-15-2001, 06:01 PM   #4
Booya Climber
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
Booya Climber is offline   Reply With Quote
Old 07-15-2001, 08:05 PM   #5
Rydeman
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]
Rydeman is offline   Reply With Quote
Old 07-15-2001, 08:44 PM   #6
Rydeman
Senior Member
 
Join Date: May 2001
Location: Sweden
Posts: 197
bug fixed. I hate variable spelling errors...
Rydeman is offline   Reply With Quote
Old 07-15-2001, 09:15 PM   #7
Booya Climber
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.
Booya Climber is offline   Reply With Quote
Old 07-15-2001, 09:34 PM   #8
Rydeman
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.
Rydeman is offline   Reply With Quote
Old 07-16-2001, 04:42 AM   #9
McChicken Nuggets
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
McChicken Nuggets is offline   Reply With Quote
Old 07-16-2001, 05:52 AM   #10
McChicken Nuggets
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
McChicken Nuggets is offline   Reply With Quote
Old 07-16-2001, 03:51 PM   #11
McChicken Nuggets
Senior Member
 
Join Date: Apr 2001
Posts: 308
did this work?
McChicken Nuggets is offline   Reply With Quote
Old 07-16-2001, 07:37 PM   #12
Rydeman
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!
Rydeman is offline   Reply With Quote
Old 07-17-2001, 03:03 AM   #13
Jls2K
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).
Jls2K is offline   Reply With Quote
Old 07-17-2001, 03:19 AM   #14
McChicken Nuggets
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
McChicken Nuggets is offline   Reply With Quote
Old 07-17-2001, 05:45 AM   #15
Jls2K
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);
not so according to Flash... I think it's actually an Xor gate. this gives what we wnat:
Code:
trace (Math.pow ( 6, 2 ));
Jls2K is offline   Reply With Quote
Old 07-17-2001, 06:00 AM   #16
McChicken Nuggets
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
McChicken Nuggets is offline   Reply With Quote
Old 07-17-2001, 07:04 AM   #17
Jls2K
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
Jls2K is offline   Reply With Quote
Old 07-17-2001, 07:45 AM   #18
HowardTheDuck
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
HowardTheDuck is offline   Reply With Quote
Old 07-19-2001, 10:48 AM   #19
Rydeman
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?
Rydeman is offline   Reply With Quote
Old 07-19-2001, 01:16 PM   #20
McChicken Nuggets
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
McChicken Nuggets is offline   Reply With Quote
Reply

Go Back   Flash Kit Community Forums > Flash Help > Flash ActionScript

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:57 AM.


internet.commerce
Be a Commerce Partner
 »  »  »  »  »  »  »
 »  »  »  »  »  »
 

    

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.