Bullets target players last position?

Hey!
How could I do it that the enemy shoot at the player?
I know how to do it that the bullet fired by an enemy move towards the player but the bullet shall move onto the last position of the ship so that the player can evade.

Until now I have only bullets implemented that goes straight.
Now I want an enemy who can shoot in all direction toward the player but the bullets should still be straight.

Hello! I think i know how to sole your problem:

public void move(float interpolation) {
		switch(facing) {
		case 0: y += speed*interpolation; break;
		case 1: y += speed*Math.sin(Math.PI/4)*interpolation;
				x -= speed*Math.cos(Math.PI/4)*interpolation;
				break;
		case 2: x -= speed*interpolation; break;
		case 3: y -= speed*Math.sin(Math.PI/4)*interpolation;
				x -= speed*Math.cos(Math.PI/4)*interpolation;
				break;
		case 4: y -= speed*interpolation; break;
		case 5: y -= speed*Math.sin(Math.PI/4)*interpolation;
				x += speed*Math.cos(Math.PI/4)*interpolation;
				break;
		case 6: x += speed*interpolation; break;
		case 7: y += speed*Math.sin(Math.PI/4)*interpolation;
				x += speed*Math.cos(Math.PI/4)*interpolation;
				break;
		}
	}

So. You make a class named Bullet. When a enemy shoots a bullet, you create it and set it’s facing like the enemy’s one. Each time an update is made, you should call “move” for each bullet in your game. This is how i do. :smiley:

Yes I have a Bullet class and a move method.
But what is facing and what interpolation?

So on your movement the enemy can fire in 8 direction?

Facing is the current facing: 0-down, 1-left-down, 2-left, 3-left-up, 4-up, and so on… You could delete the interpolation (remove it where it appears).


// bullet heading
float dx = targetX - startX;
float dy = targetY - startY;
double h = Math.sqrt(dx * dx + dy * dy);
float dn = (float)(h / sqrt2);
		
Vector bulletHeading = new Vector(dx / dn, dy / dn, 0);

This works for all directions with heading values between 0.0 and 1.0, respectively -1.0.
So, multiplying these values with the bullet velocity results in actual velocity to be applied for each game frame.

Hm I dont understand it ô.o

I have this variables:
Player_X
Player_Y
Enemy_X
Enemy_Y

I only managed that the bullet is following the player until it hit him.
I want that it stay on its path straight to the players last known position whenthe enemy shooted the bullet.

Ah and do you know the weapon in Gradius which shoot in 3 directions?
I want that for an enemy too but how can I handle 3 bullets with different movements at the same time?

If you want to target arbitrary positions, you need to know the relation between x- and y-steps (calculated with Pythagoras theorem).
Subsequently, the values are normalized so they are between -1 and +1.
Further handling depends on your game loop and how you move objects.

I think you are confusing some things here… If you want that the bullet keeps on flying on the initial line, you either have to save the angle or the x and y speed, when you instance your bullet.
When you don’t want the bullets to be able to change direction or speed, you can precalculate x and y speed and just update with those values.

this is the update method in one of my prototypes:


public void updateDistance(int d) 
{
	xd = (float) (-Math.sin((r*Math.PI / 180)) * ys * d);
	yd = (float) (Math.cos((r*Math.PI / 180)) * ys * d);
}

the parameter d is the time delta in milliseconds since last update.
ys defines the speed
r is the rotation (float from 0-360, where 0 is pointing to the top/north)
xd and yd are the traveled distance in this frame.

if you want to precalculate, you would have to remove the “* d” from both formulas and then apply the delta during update.

I hope this helps. there may be a better solution (there most certainly is, as I’m not really that strong in math).

one additional thing you need is the angle. i calculate the angle (r := radius) like this


public static float getAngle(float x0, float y0, float x1, float y1)
{
    float f1 = (float)Math.toDegrees( Math.atan2( y0-y1, x0-x1 ) );
    f1+=270;
    while ( f1 > 360 ) f1-=360;
    while ( f1 <   0 ) f1+=360;
    return f1;
}

Simply use the bullet spawn coordinates for x0 and y0 and the current player coordinates for x1 and y1.

Who’s confusing things ?

No need for angles.

In vector terms you calculate this when shooting happend and then update ammoPos with ammoVelocity every frame.


ammoVelocity =  ammoSpeed * normalize( targetPos - pos)

I was talking to gustav :wink: his description sounded like he is updating the shooting direction every frame to target the player (because of the sentence “I only managed that the bullet is following the player until it hit him.”).

pitbullers version looks simple :smiley: have to try that one out.