A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: Simple Question: How to move to a non-fixed point

Threaded View

  1. #4
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    Ah nice, you're building the same foundation as I had to for the current big game I'm working on. That makes understanding your problem a lot easier. I can give you tips on driver, gunner and tactical AI if you get stuck later on. At least that what I've named them in my class library.

    You're going to have to learn vectors. Google Tonypa's vector tutorials. It mostly handles collisions but it explains the basic of vectors and their related math not assuming you already know this and that. Looking at your code you already grasp the concept by figuring stuff out yourself, but you haven't been explained yet. Thrill your math teacher and ask him about them.

    Basicly a vector is something with a amount AND direction. This may sound a little abstract, but it's quite a general, but therefore accurate, definition. Think stuff like velocity. Not only does velocity have a amount (how fast am I going?) it also has a direction (what direction am I going?). Another example is force. How hard am I pushing it and where and in what direction am I pushing it? You can see that the effect a force has, not only is defined by the amount of force but also where and in what direction it's applied. There are two popular ways used to define vectors.

    1. dX and dY
    2. Magnitude and Angle


    Take a look at the triangle in the unit circle again and you can see the two definitions.

    That bieng said; on to the next level.

    For a better simulation I suggest an addition to your physics model. When I move my arm, I move my arm with my muscles. My muscles don't generate velocity, they generate force. Your tank at the moment generates velocity. To go ahead with the arm example: so I generate force, but how does this make my arm move? One definition of force is F = mass*accelaration (F = m*a) . You see acceleration in there, so Force generates (well technicly it generates nothing) accelaration. Also you see mass in there. If you look at it, you can see that the more mass there is, the more force is needed to accelarate it. Since acceleration generates velocity, that's how my muscles move my arm.

    To apply this to your game:
    Acceleration = (EnginePower/Mass) and Current rotation
    Velocity += Acceleration
    Velocity * Friction //Or else it would keep on going

    This basicly is your moving function in pseudocode. To actually get the tank moving to the point, you need to calculate the angle to the point and turn the tank. This changes the Current rotation variable and accelerates the tank towards the point.

    Looking at your .fla you only needed to know the getAngle() function. You had to be really close to the solution, because you seem to already understand A LOT of the problem.

    Code:
    //This code won't work as not all the variables are properly referred to. Adjust the code to fit your game.
    function getAngle(){
        var dX:Number = Target.X - Tank.X
        var dY:Number = Target.Y - Tank.Y
        var DesiredDir:Number = Math.atan2(dY,dX)
    }
    function turnTank(){
       var CurrentDir:Number = "Current direction"
       var Angle:Number = DesiredDir - CurrentDir
       var MaxTurn:Number
       if(Angle > MaxTurn){
          //Right
          CurrentDir += MaxTurn
       } else if (Angle < MaxTurn){
          //Left
          CurrentDir -= MaxTurn
       } else {
          //To avoid oversteering which makes your tank wobble towards it's target
          CurrentDir = DesiredDir
       }
    }
    function moveTank(){
       //Sorry no "advanced" model in here. That's up to you to code.
       var CurrentDir:Number = "Current direction"
       //Actually the xspeed and yspeed you're using is dX and dY. They are the X and Y components of the speed vector. 
       var dX:Number = speed * Math.cos(CurrentDir)
       var dY:Number = speed * Math.sin(CurrentDir)
       //
       newX = CurrentX + dX
       newY = CurrentY + dY
    }
    function updateTank(){
        getangle()
        turnTank()
        movetank()
    }
    If you need any further help just keep posting, as I'd love to help you out. If you need tips on AI, managing several tanks and enemies, checking for hits and the works, just PM me and/or post on the games section of the board (always better to do both).

    EDIT: and a coding tip, makes it a lot easier to read. BTW, your coding is very clean and neat. Don't feel bad for using procedural programming instead of object orientated programming.
    Code:
    case 37: //left arrow
       //stuff
    break
    
    var LEFT = 37
    case LEFT:
       //stuff
    break
    Last edited by TOdorus; 11-22-2008 at 09:36 PM.

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