Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (522) Games in Android Showcase (127) games submitted by our members Games in WIP (590) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Bullets target players last position?  (Read 2558 times) 0 Members and 1 Guest are viewing this topic.
GustavXIII

Junior Devvie

Projects: 1

 « Posted 2012-04-18 09:54:45 »

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.
marcuiulian13

Senior Devvie

Medals: 5
Exp: 3 years

 « Reply #1 - Posted 2012-04-18 10:11:58 »

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

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20 `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.

Getting a project done is by far the most hard thing in game development.
GustavXIII

Junior Devvie

Projects: 1

 « Reply #2 - Posted 2012-04-18 10:24:35 »

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?
marcuiulian13

Senior Devvie

Medals: 5
Exp: 3 years

 « Reply #3 - Posted 2012-04-18 10:26:40 »

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).

Getting a project done is by far the most hard thing in game development.
65K
 « Reply #4 - Posted 2012-04-18 10:31:15 »

 1  2  3  4  5  6  7 `// bullet headingfloat 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.

GustavXIII

Junior Devvie

Projects: 1

 « Reply #5 - Posted 2012-04-18 14:42:59 »

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?

65K
 « Reply #6 - Posted 2012-04-18 15:40:29 »

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.

atombrot
 « Reply #7 - Posted 2012-04-18 15:55:29 »

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:
 1  2  3  4  5 `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

 1  2  3  4  5  6  7  8 `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.
65K
 « Reply #8 - Posted 2012-04-18 16:44:39 »

Who's confusing things ?

pitbuller
 « Reply #9 - Posted 2012-04-18 16:58:12 »

No need for angles.

In vector terms you calculate this when shooting happend and then update ammoPos with ammoVelocity every frame.
 1 `ammoVelocity =  ammoSpeed * normalize( targetPos - pos)`
atombrot
 « Reply #10 - Posted 2012-04-18 17:27:56 »

I was talking to gustav 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 have to try that one out.
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 trollwarrior1 (29 views) 2014-11-22 12:13:56 xFryIx (71 views) 2014-11-13 12:34:49 digdugdiggy (50 views) 2014-11-12 21:11:50 digdugdiggy (44 views) 2014-11-12 21:10:15 digdugdiggy (38 views) 2014-11-12 21:09:33 kovacsa (62 views) 2014-11-07 19:57:14 TehJavaDev (67 views) 2014-11-03 22:04:50 BurntPizza (64 views) 2014-11-03 18:54:52 moogie (80 views) 2014-11-03 06:22:04 CopyableCougar4 (80 views) 2014-11-01 23:36:41
 theagentd 28x basil_ 27x HeroesGraveDev 23x BurntPizza 21x Riven 19x Spasi 18x princec 17x KevinWorkman 13x kpars 11x SHC 11x gouessej 10x Grunnt 10x Nate 9x Longarmx 9x LiquidNitrogen 8x CopyableCougar4 8x
 Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50List of Learning Resources2014-07-31 16:26:06
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org