Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (581) Games in Android Showcase (162) games submitted by our members Games in WIP (632) 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 2803 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.

 Waterwolf (23 views) 2015-05-20 15:01:45 chrislo27 (27 views) 2015-05-20 03:42:21 BurntPizza (64 views) 2015-05-10 15:53:18 FrozenShade (48 views) 2015-05-07 09:11:21 TheLopais (210 views) 2015-05-06 13:36:48 TheLopais (194 views) 2015-05-06 13:35:14 TheLopais (201 views) 2015-05-06 13:33:39 TheLopais (220 views) 2015-05-06 13:32:48 TheLopais (216 views) 2015-05-06 13:31:28 ClaasJG (238 views) 2015-04-30 20:33:25
 Spasi 31x BurntPizza 16x theagentd 13x DavidBVal 13x ra4king 12x EgonOlsen 11x Husk 10x KevinWorkman 9x princec 8x scanevaro 8x opiop65 7x KaiHH 7x revers 6x Riven 6x HeroesGraveDev 5x MrPork 5x
 List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding 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:00
 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