A Flash Developer Resource Site

Results 1 to 17 of 17

Thread: [Help] AI Opponents in Racing Game (AS2)

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    13

    Exclamation [Help] AI Opponents in Racing Game (AS2)

    Hi Guys,

    I am building a racing game for my client, freelance.

    Now this is my first time using Flash to create any kind of game at all, so I've kept it simple and so far so good, really. I've managed to learn how to control the vehicle and prevent it from running off the page, and set up lap timers.

    Now my client knows that this is my first time doing this and that I will come across bumps in the road, which this is the first of.



    With my AI opponents I figured I could just animate them round the track and change the speed of the animations after I have tested the speed I can get round the track in the 'Player' car. I will have 3 opponents and obviously there will be a fast/medium/slow opponent.

    My main problem is sorting out the coding of what position the player is in. I REALLY don't know where to start when it comes to this. I read something about checkpoints which you would have a lot of on the track, and then it would update your position compared to the times it would take for the AI cars to get round.

    But I really don't know what to do here and I've been searching for tutorials and cannot find anything!

    Any help is appreciated, or if I have missed a tutorial somewhere explaining how to do this, please call me whatever you want and give me a link to it

    Much Appreciated!

    Oli

  2. #2
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    I would define the track as a path of bezier curves. Then at the start of the game compute the total length of all curves added together. Then as the race is going on run an intersection test to the bezier curves and find which bezier the car is closest to, next find the position that intersection point is on the bezier, using that position + that beziers position in the path will get you the position of the player in the path.

    pseudo:

    bezier = getBezier(player.x, player.y); //find bezier with closest intersection point
    point = getClosestPointOnBezier(bezier, player.x, player.y); //relative point on bezier
    pos = (bezier.position + point.length) / totalBezierLength;

    a beziers position is determined by the previous connecting bezier length + previous bezier position. To determine the length of a bezier, the simplest way is to sample the bezier as a series of line segments and add their length. I'm not sure if there is a complete mathematical solution to finding a beziers length.

    http://en.wikipedia.org/wiki/B%C3%A9zier_curve
    http://www.algorithmist.net/closestquad.html

    maybe a much simpler but less acurate method would be adding a series of points around the path, making sure that an imaginary line between each point does not cross over a bend. You then do the same thing as above. Find the total length of the path using all points, loop through all points to find the closest one (distance check), use that points position / totalLength to find the position the player is at.

    hope that helps
    lather yourself up with soap - soap arcade

  3. #3
    Junior Member
    Join Date
    Aug 2009
    Posts
    13
    Hmmm I can't say I'm with you..

    The only experience I have of using Bezier curves in Flash is for motion guides on tweens and drawing with the pen tool

    I'll show you what I have so far as it may make things easier to explain!

    I have been following this tutorial:
    http://www.emanueleferonato.com/2007...game-tutorial/

    and amending it to my graphics and changing the turn speed, etc.

    .fla:
    http://www.filefront.com/14320781/Pl...%20Terrain.fla

    .swf:
    http://www.filefront.com/14320797/Pl...%20Terrain.swf

    please take note this is my test track and not the finalised version

    If there are any examples or tutorials of implementing what you said I'd be very happy to see them, but like I said I'm very new to game developing. And I would say I'm still very amateur at Flash!

  4. #4
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    ok, i've created it for you, the code is all explained inside the fla. Its a lot different to how you're coding atm, I highly suggest you pick up an Actionscript book and learn some fundementals of programming, you really need to know how to create and reference variables properly, understand scope etc..

    But I won't go into that here. Anyway, let me know if it doesn't make sense and i'll try to explain it better:

    http://littlemofo.soap.com.au/gamedev/car/carGame.zip

    http://littlemofo.soap.com.au/gamedev/car/
    Last edited by mr_malee; 08-19-2009 at 10:17 PM.
    lather yourself up with soap - soap arcade

  5. #5
    Junior Member
    Join Date
    Aug 2009
    Posts
    13
    Yea like I said I'm a complete newb when it comes to programming games, this is just something I feel I've been forced into since leaving uni by my tutor and I've had to run before I can walk! ><

    Thanks for the help files I shall have a look at them now and get back to you! Very much appreciated

  6. #6
    Junior Member
    Join Date
    Aug 2009
    Posts
    13
    this is hard hehe, I think I understand the AS in the posCheck MC. But for some reason it seems to running up the total time and not just the player position text box.

    I've created some animations for my AI cars. How would I add these to the AS so we can get the players position instead of a timer?

    What good books or websites would you recomend for reading up on programming in flash? Would be very useful for future reference as although this is a new and hard experience for me I find it very rewarding when people play your game and it would obviously open more doors in other areas of flash with the more knowledge i have


    files:

    .fla:
    http://www.filefront.com/14377703/Pl...cars%20CS3.fla

    .swf
    http://www.filefront.com/14377711/Pl...cars%20CS3.swf

    I really appreciate all this help, and I wouldn't know where I would be without it!

  7. #7
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    you're using text box's wrong. You need to set their "text" property instead of making them listen to variables. While you can do it that way, its not wise as problems like this occur.

    simply give your textFields a name, like you would a movieclip, then to set their text value write:

    myTextField.text = myValue.toString();

    I'm not sure what you mean by this "I've created some animations for my AI cars. How would I add these to the AS so we can get the players position instead of a timer?"

    the function is setup to return a number between 0-1 for any car passed to it. So to get the position of the AI car, do the same as the player, but give it a different variable name. e.g:

    var pos1 = positions.getTrackPosition(_root["car1"]);
    var pos2 = positions.getTrackPosition(_root["car2"]);
    var pos3 = positions.getTrackPosition(_root["car3"]);

    if there's one piece of advice I can give you: USE VARIABLES, using objects in the way you are doing now:

    Code:
    _root["car"+who].pointLeft = {x:-4.5, y:0};
    _root["car"+who].localToGlobal(_root["car"+who].pointLeft);
    _root["car"+who].pointRight = {x:4.5, y:0};
    _root["car"+who].localToGlobal(_root["car"+who].pointRight);
    _root["car"+who].pointFront = {x:0, y:-4.5};
    _root["car"+who].localToGlobal(_root["car"+who].pointFront);
    _root["car"+who].pointBack = {x:0, y:4.5};
    _root["car"+who].localToGlobal(_root["car"+who].pointBack);
    can be simplified into:

    Code:
    var car = _root["car"+who]; 
    
    car.pointLeft = {x:-4.5, y:0};
    car.localToGlobal(car.pointLeft);
    car.pointRight = {x:4.5, y:0};
    car.localToGlobal(car.pointRight);
    car.pointFront = {x:0, y:-4.5};
    car.localToGlobal(car.pointFront);
    car.pointBack = {x:0, y:4.5};
    car.localToGlobal(car.pointBack);
    which is much more readable. Also, define every single variable with var leaving this out is very bad practice and will mess with variable scope if not used properly. Defining a variable with var inside a function tells flash that the variable is only accessible within the function and after the function has been run it will no longer exist. This is very handy especially in the above case where the variable "car" is really only needed inside the function and nowhere else.
    Last edited by mr_malee; 08-25-2009 at 11:56 PM.
    lather yourself up with soap - soap arcade

  8. #8
    Junior Member
    Join Date
    Aug 2009
    Posts
    13
    Yes I agree that is much nicer to read. I should be able to apply these variables to other things like this code?

    this["lpx"+who] = _root["car"+who].pointLeft.x;
    this["lpy"+who] = _root["car"+who].pointLeft.y;
    this["rpx"+who] = _root["car"+who].pointRight.x;
    this["rpy"+who] = _root["car"+who].pointRight.y;
    this["fpx"+who] = _root["car"+who].pointFront.x;
    this["fpy"+who] = _root["car"+who].pointFront.y;
    this["bpx"+who] = _root["car"+who].pointBack.x;
    this["bpy"+who] = _root["car"+who].pointBack.y;

    I wouldn't know what on earth to change it to though?

    I've managed to fix the problem with the trackPos time coming up into the totalTime by changing the Variable in the trackPos text box. But its still running in decimals and not giving a whole number. It would be nice if it were like 1/4, 2/4, 3/4, 4/4.

    I've been looking through tutorials on this site (which helped me fix the trackPos+totalTime problem) but I can't find any to do with racing and looking at code for positioning like this.

    I feel like I've asked a lot of you here Mr Malee and for that I am grateful. I added the variables in the AS for the enemy cars, but I still can't get the position to work . If you could be as so kind as to look at it for me, for hopefully the last time!

    http://www.filefront.com/14400861/TestAICS3.fla

    I really, really appreciate all the help you have given me

  9. #9
    Junior Member
    Join Date
    Aug 2009
    Posts
    13
    bump

    I hadn't received any contact off my client for 2/3 weeks, so I put this on the shelf for a while as I had other things I could be getting on with. I'm still stuck with what to do with the playerPos, to get it to come up 4/4, 3/4, 2/4, 1/4 like you see in racing games. Its so stressful

    also I've applied what I've learnt so far to 2 other tracks to give the player some choice, I basically copied all the frames and put them in the new files with the new artwork, I changed the animations, checkpoints and position checks to suit the new track, which is great. But for some reason my total lap time keeps running at the end of the game, even though it doesn't on the original, I've no idea why it is doing this!!!

    Can anyone reccomend some good books+websites for learning AS in games?

    Last edited by olirushworth; 09-10-2009 at 07:16 AM.

  10. #10
    Junior Member
    Join Date
    Aug 2009
    Posts
    13
    another shameless bump

  11. #11
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    just get all the player/Ai positions from the array when someone crosses the finish line, then just sort the array!
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  12. #12
    Junior Member
    Join Date
    Aug 2009
    Posts
    13
    and how would i do that?

    My AI cars are animated on a guide path, which loops and continues to loop after the race has ended. So they're just animations and don't really have any AI, I just refer to them as that because they aren't playable.

    If you see the files I posted earlier in the thread you may get more of an insight into what I am doing. This is my FIRST time making a flash game ever, so a lot of these terms and know-how is unknown/new to me

  13. #13
    Title? Am I King or something?
    Join Date
    Jul 2009
    Location
    Ponyville, Happytown on the continent Nowhereland
    Posts
    26
    Alternatively, you can just set "steerpoints" on the map and tell the car to steer for them. Use the same physics as the player car, and tell it wwhen it reaches a certain distance from the "point" to aim for the next. Do this whenever the car needs to steer.

    Fusiox

  14. #14
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    lather yourself up with soap - soap arcade

  15. #15
    Junior Member
    Join Date
    Aug 2009
    Posts
    13
    Thanks a lot Mr_malee. I've not had chance to look at it yet due being forced into reformatting my system, but undoubtedly it will be helpful and useful

  16. #16
    Junior Member
    Join Date
    Aug 2009
    Posts
    13
    ok, here is the game so far

    .fla

    http://www.filefront.com/14687809/Bl...ash%20Game.fla

    .swf

    http://www.filefront.com/14687843/Bl...ash%20Game.swf

    very buggy still, i was wondering if people could help me with any fixes, such as:

    The timer at the end not working.

    The Play Again button doesn't work, and I'm not sure how to resolve it.

    Why the player position doesn't work on the 'hard' track?

    Do you think the AI car which wins is going too fast?

    How hard would it be to add SFX to this game? with screeching and engine noises? Would these be bound to the up+side arrows?

    How hard would it be to implement a High-Score feature. Where players could upload their times from the different tracks, etc?

    So yes, still quite a way to go, but I must thank Mr.Malee for all his help ^^

  17. #17
    Junior Member
    Join Date
    Aug 2009
    Posts
    13
    bump

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center