Adding acceleration to pathfinding enemies...
BlitzMax Forums/BlitzMax Beginners Area/Adding acceleration to pathfinding enemies...
| ||
Im making a 2d space shoot-em up where the character stays center-screen but roams the 'world' ..basically everything's position on-screen is offset by where the player is in the 'world'. I have an enemy that when you get within range will power up and start to hunt you... now I've coded the hunting part, but I want to add acceleration, friction, etc so its not so 'on/off' I've got the accel/friction idea working great for the player, but I cant seem to get it right for the enemy :/ Here's a snippet of the enemy code... Local dirX:Float = 1 Local dirY:Float = 0 Local speed:Float = 5 Local turnSpeed:Float = 250 'is player in range? if so power up.. if not lay dormant If( isOn = 1 ) 'hunt Local dX:Float = Player.worldX - worldX Local dY:Float = Player.worldY - worldY Local distanceLength:Float = Sqr( dX * dX + dY * dY ) dX:/ distanceLength dY:/ distanceLength If dY * dirX - dX * dirY > 0 Then dirX = Cos( Pi / 180.0 * turnSpeed ) * dirX - Sin( Pi / 180.0 * turnSpeed ) * dirY dirY = Cos( Pi / 180.0 * turnSpeed ) * dirY + Sin( Pi / 180.0 * turnSpeed ) * dirX If dY * dirX - dX * dirY < 0 Then dirX = dX dirY = dY EndIf Else dirX = Cos( -Pi / 180.0 * turnSpeed ) * dirX - Sin( -Pi / 180.0 * turnSpeed ) * dirY dirY = Cos( -Pi / 180.0 * turnSpeed ) * dirY + Sin( -Pi / 180.0 * turnSpeed ) * dirX If dY * dirX - dX * dirY < 0 Then dirX = dX dirY = dY EndIf End If worldX:+ ( dirX * speed ) worldY:+ ( dirY * speed ) Else End If Any help would be great :) |
| ||
did you convert this code from c to BlitzMax? The reason I am asking is because Pi/180.0 is used to convert degrees to radian but all of the Trig functions in BlitzMax work in degrees and I believe the standard trig functions in c work with radians. Instead of doing all of that extra math, jut remove the whole pi/180.0 out of all of the equations and change the turnSpeed speed value to a small number like maybe 2. so it will look something like this: turnSpeed = 2.0 . . dirX = Cos(turnSpeed ) * dirX - Sin(turnSpeed ) * dirY don't forget to keep the negative sign on the lower equations. I don't really know what that formula i supposed to do because I can't figure out how it's working and I don't have a working(runnable) example. I have some code in the code archive for a chaser missile that should do what you want: http://www.blitzmax.com/codearcs/codearcs.php?code=2045 it's not perfect but it should work fairly well for a constant frame rate(flip(-1)). yours might work better but unless you provide a runnable example I won't know. |