Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (757) Games in Android Showcase (229) games submitted by our members Games in WIP (844) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 Bullets target players last position?  (Read 5403 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: 6
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?
 Games published by our own members! Check 'em out!
marcuiulian13

Senior Devvie

Medals: 6
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.

Lethal Running - a RPG about a deadly game show held in a futuristic dystopian society.
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.

Lethal Running - a RPG about a deadly game show held in a futuristic dystopian society.
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 ?

Lethal Running - a RPG about a deadly game show held in a futuristic dystopian society.
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)`
 Games published by our own members! Check 'em out!
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

 EgonOlsen (79 views) 2018-06-10 19:43:48 EgonOlsen (59 views) 2018-06-10 19:43:44 EgonOlsen (78 views) 2018-06-10 19:43:20 DesertCoockie (261 views) 2018-05-13 18:23:11 nelsongames (159 views) 2018-04-24 18:15:36 nelsongames (158 views) 2018-04-24 18:14:32 ivj94 (901 views) 2018-03-24 14:47:39 ivj94 (162 views) 2018-03-24 14:46:31 ivj94 (813 views) 2018-03-24 14:43:53 Solater (177 views) 2018-03-17 05:04:08
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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