A Flash Developer Resource Site

Results 1 to 11 of 11

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

  1. #1
    Junior Member
    Join Date
    Nov 2008
    Posts
    15

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

    Since its late, and im not very good at problem solving, i cant figure out the answer to this simple little problem.

    I have a car that i want to move in the direction it is pointing. I assume that i would use the formula (in many posts on this site) for moving towards a point in order to move it, the only problem is that the point can be anywhere the car is facing (360 degrees i suppose). I doubt that it matters how far away the point is either, as the car will just drive as long as you hold down a key. how do i find this point...?

  2. #2
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    THEORY

    Now look at the sloped side (no idea what it is called in English so I call it sloped side) of the traingle. It's always a distance of 1 away from the origin. This is because a unit circle is always 1 unit (hence the name) in radius. Your speed won't always be 1, so you have to multiply it with your speed.

    Code:
    var dX:Number = speed * Math.cos(rotation)
    var dY:Number = speed * Math.sin(rotation)
    //
    newX = CurrentX + dX
    newY = CurrentY + dY

  3. #3
    Junior Member
    Join Date
    Nov 2008
    Posts
    15
    Well, after reading the theory and your post i thought i knew what you were talking about, but when i tried it i, of course, failed. I used your code + the code i found for moving towards a point in order to get it to move, but it doesnt. probably just some dumb simple thing i forgot but like i said im not good at problem solving.

    Code:
    var dir;
    function getAngles(){ //called using an ENTER_FRAME
    	var dX:Number = xSpeed * Math.cos(tank[0].rotation);//xSpeed is 2
    	var dY:Number = ySpeed * Math.sin(tank[0].rotation);//ySpeed is 2
    	var newX = tank[0].x + dX;
    	var newY = tank[0].y + dY;
    	dir = Math.atan2(tank[0].y-newY, tank[0].x-newX)*180/Math.PI;
    	//i took your formula, then added this one i found on moving towards a point
    	//then i moved it with the other formula (in the move tank function)
    }
    
    function moveTank(){  // called only when (and while) the player presses a key.
    	for(var i=0;i<tank.length;i++){//the tank parts are in an array, so       this moves 'em all
    		/*if(lockTurret){ not important here
    		tank[i].rotation+=rSpeed;
    		}else if(i!=2){
    		tank[i].rotation+=rSpeed;
    		}*/
    		tank[i].x+=Math.cos(dir*Math.PI/180)*xSpeed;//and this is what is susposed to move it 
    		tank[i].y+=Math.sin(dir*Math.PI/180)*ySpeed;//but it does not move the right way
    	}
    }
    i will also try to attach the fla, and hopefully you can find the problem in all the clutter and bad coding.
    Last edited by wonebyfase; 03-15-2009 at 11:26 PM.

  4. #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.

  5. #5
    Junior Member
    Join Date
    Nov 2008
    Posts
    15
    wow, that was quite a good explanation. The only question i have now is how to find Target.x and y?
    Code:
    function getAngle(){
        var dX:Number = Target.X - Tank.X
        var dY:Number = Target.Y - Tank.Y
        var DesiredDir:Number = Math.atan2(dY,dX)
    }
    i assume to find this point i would find the angle that the tank is at and somehow use the hypotenuse line (r) in the theory examples of your first post, but how to use it escapes me.

    other than this your explanation and changes help a lot, thank you so much!

  6. #6
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    It's not found it's defined. It's the point the tank will move to. This can be a (moving) enemy, a waypoint, whatever.

    If you just want it to keep on moving in a direction just define the tanks current location and direction and don't use the getAngle() and turnTank() funcions as they're used to steer the tank to the Target.

    If that isn't the behavior you're looking for, you'll have to explain what you actually need to me again. An animation would be the ultimate description ofcourse

  7. #7
    Junior Member
    Join Date
    Nov 2008
    Posts
    15
    ok i whipped up a really quick animation of what i would like, should say it all. i assume from your last post that this was not the way to do it, but what is?

    thanks for all the help, i appreciate it
    Last edited by wonebyfase; 03-15-2009 at 11:26 PM.

  8. #8
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    Well you already got that, don't you? It's the moveTank() function. That function takes the current direction of the tank, and moves it along that rotation accordingly.

  9. #9
    Junior Member
    Join Date
    Nov 2008
    Posts
    15
    well i probably do have it, i just dont see it. as of now if i take out the getAngle function and just use the moveTank one, it does not work (using the code for moveTank that you posted earlier).

  10. #10
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    Hmm, strange. I took a look at your .fla again and the only thing I can think of is that you're converting to degrees. I whipped up a quick AS2 example just in case.
    Attached Files Attached Files

  11. #11
    Junior Member
    Join Date
    Nov 2008
    Posts
    15
    ok, i finally got it working nicely using:
    Code:
    function moveTank(){
       var speed=2;
       var CurrentDir:Number = tank[0].rotation-90;
       CurrentDir=CurrentDir*(Math.PI/180);
       var dX:Number = speed * Math.cos(CurrentDir);
       var dY:Number = speed * Math.sin(CurrentDir);
    
       	for(var i=0;i<tank.length;i++){
    		if(lockTurret){
    		tank[i].rotation+=rSpeed;
    		}else if(i!=2){
    		tank[i].rotation+=rSpeed;
    		}
       tank[i].x+=dX;
       tank[i].y+=dY;
    	}
    }
    thank you so much for all your help, i will definitely use your code when making the enemies.
    Again, thanks a lot!

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