I’m want to have my enemies shoot projectiles at my main character.
The way I’m thinking of how to do that is to calculate a vector using the enemies & my main character’s position and then send a projectile over a straight line according to the calculated vector.
Thing is i have no idea how to do this. It’s been a while since I’ve done some vector math and I wouldn’t no what code to use.
Does anyone have any suggestions for me?
Links to examples, tutorials or explanation of how to code this?
To get the angle between the player and the enemy you can do:
double xx = targetX - shooterX; // Target would be the Mob that gets shot at, and shooter would be the mob that shoots.
double yy = targetY - shooterY; // Keep in mind that all these values give more accurate results when they are doubles.
double angle = Math.atan2(yy, xx);
To send a projectile over this angle you could do this for every update:
Would I get problems with projectileX/Y not being integers but doubles?
Or can I just pass double values for x & y to my EnemyProjectile instances?
Like changing EnemyProjectile (int x, int y) to EnemyProjectile (double x, double y)?
This looks pretty simple and i would have to mind about x being 0 because my enemies wouldnt ever fire projectiles when they’re at x or y=0.
But so far it isnt working.
I assume
double angle = Math.atan2(yy / xx);
needs to be
double angle = Math.atan2(yy, xx);
since atan2 requires 2 variables y,x.
This is what i added to my ProjectileEnemy class’ constructor:
x = startX;
y = startY;
//
// bunch of other variables here
targetX = KnightmareGame.popolon.getCenterX();
targetY = KnightmareGame.popolon.getCenterY();
System.out.println("popolon x,y=" + targetX + "," + targetY );
shooterX = startX;
shooterY = startY;
System.out.println("start x,y=" + shooterX + "," + shooterY );
double xx = targetX - shooterX; // Target would be the Mob that gets shot at, and shooter would be the mob that shoots.
double yy = targetY - shooterY; // Keep in mind that all these values give more accurate results when they are doubles.
double angle = Math.atan2(yy , xx);
System.out.println("angle= " + angle );
And in my update method:
x += speedX * Math.cos(angle);
y += speedY * Math.sin(angle);
but i just get projectiles with y = always 0 and every projectile slowly moving to the right with a speed of x=1 if i set speedX to 1 instead of speed
Target’s x/y and shooter’s x/y values are passed correctly and a different angle is calculated for different projectiles.
but math.sin(angle) is always close to 1 and math.cos(angle) always close to 0.
what’s going wrong?
I’ll also check & see if i can just solve the whole problem using cylab’s way
Well it shouldn’t really make a huge difference, but try to use doubles for your x and y coords. Or at least try to cast the targetX and shooterX to a double.
I guess maybe i should just change y to a double and only cast it to an int in my paintmethod at g.drawImage(arrow_knight, (int)pe.getX(), (int)pe.getY(), (this));
yeah understood it before, but what i didnt understand was whether or not the double -0.999etc got rounded to 0 (and why not -1 f.e.) and this was the problem or something else was going on.