Hello all math masters, I got a problem for you:
I have a 2D game (top down), and I would like to make the character escape from a shot, but not just walk away from the shot (I mean, don't be pushed by the shot), I want it to have a good dodging skills.
The variables are:
- shotX - shot x position
- shotY - shot y position
- shotSpeedX - shot x speed
- shotSpeedY - shot x speed
- charX - character x position
- charY - character y position
- keyLeft - Set to true to make the character press the to left key
- keyRight - Set to true to make the character press the to right key
- keyUp - Set to true to make the character press the to up key
- keyDown - Set to true to make the character press the down key
I can understand the following languages:
- C/C++
- Java
- Actionscript 2/3
- Javascript
I got this code (Actionscript 3), but sometimes it doesn't work:
var escapeToLeft:Boolean = false;
var r:Number = Math.atan2(0 - shotSpeedY, 0 - shotSpeedX)
var angle:Number = Math.atan2(charY - (shotY + shotSpeedY), charX - (shotX + shotSpeedX));
var b:Number = diepix.fixRotation(r-angle); // This function make the number between -180 and 180
if(b<0) {
escapeToLeft = true;
}
r += (escapeToLeft?1:0 - 1) * Math.PI / 2;
var cx:Number = Math.cos(r);
var cy:Number = Math.sin(r);
if(cx < 0.0) {
keyLeft = true;
}else {
keyRight = true;
}
if(cy < 0.0) {
keyUp = true;
}else {
keyDown = true;
}