2d Waypoint animation?
Blitz3D Forums/Blitz3D Programming/2d Waypoint animation?
| ||
Anyone have any good code for doing waypoint animation in 2D? I'm not refering to pathfinding, I've got code for that. I need some code that will take a 2d sprite and animate it along a path that is predefined. This could be a spline I guess or just simple linear interpelation. Any ideas? |
| ||
have a look at the plethora of example code concerning things like "heat seeking missles" or calculateing vectors... A real simple approch is something like this: CurrentX and CurrentY store the current 2D cords VectorX and VectorY store the vector...this is added to CurrentX and CurrentY to move it TargetX and TargetY store the 2D cords of the waypoint... basicly to find the vector you do this: VectorX = TargetX - CurrentX VectorY = TargetY - CurrentY you can then find the length of this vector (the distance) like so vectorlength = SQR( VectorX * VectorX + VectorY * VectorY ) Then you can check to see if the vectorlength is larger then the max speed you wish the object to travel If vectorlength > maxspeed Then scalefactor = maxspeed / vectorlength VectorX = VectorX * scalefactor VectorY = VectorY * scalefactor End If Then just move the object by adding the vector CurrentX = CurrentX + VectorX CurrentY = CurrentY + VectorY Some other ways to do this for different effects: An effect simular to moveing the camera in a RTS type game, or a speed independant way of moveing the object to the waypoint (further it is the faster it moves, closer the slower) done each update in which you need to move the object: CurrentX = CurrentX + ((TargetX - CurrentX) * .09) CurrentY = CurrentY + ((TargetY - CurrentY) * .09) Another simular effect is the little "options" often found following the players space ship around in games like R-Type: VectorX = VectorX + ((TargetX - CurrentX) * .1) VectorY = VectorY + ((TargetY - CurrentY) * .1) CurrentX = CurrentX + VectorX CurrentY = CurrentY + VectorY either way you can change the floating point value to something between 1 and 0 inorder to scale the speed up/down...the second example sorta gives the illusion of inertia because the vector values are carried over from update to update... Hope this helps :) |
| ||
Wow, I'll give this a try. Thanks! |