Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (581)
games submitted by our members
Games in WIP (500)
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 2343 times)
0 Members and 1 Guest are viewing this topic.
Offline GustavXIII

Junior Member


Projects: 1



« Posted 2012-04-18 11: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.
Offline marcuiulian13

Senior Member


Medals: 5
Exp: 3 years



« Reply #1 - Posted 2012-04-18 12: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. Cheesy

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

Junior Member


Projects: 1



« Reply #2 - Posted 2012-04-18 12: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!
Legends of Yore - The Casual Retro Roguelike
Offline marcuiulian13

Senior Member


Medals: 5
Exp: 3 years



« Reply #3 - Posted 2012-04-18 12: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.
Offline 65K
« Reply #4 - Posted 2012-04-18 12:31:15 »

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

Offline GustavXIII

Junior Member


Projects: 1



« Reply #5 - Posted 2012-04-18 16: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?



Offline 65K
« Reply #6 - Posted 2012-04-18 17: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.

Offline atombrot

Junior Member


Medals: 1
Projects: 1



« Reply #7 - Posted 2012-04-18 17: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.
Offline 65K
« Reply #8 - Posted 2012-04-18 18:44:39 »

Who's confusing things ?

Offline pitbuller
« Reply #9 - Posted 2012-04-18 18: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!
Legends of Yore - The Casual Retro Roguelike
Offline atombrot

Junior Member


Medals: 1
Projects: 1



« Reply #10 - Posted 2012-04-18 19:27:56 »

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

xsi3rr4x (64 views)
2014-04-15 18:08:23

BurntPizza (62 views)
2014-04-15 03:46:01

UprightPath (75 views)
2014-04-14 17:39:50

UprightPath (58 views)
2014-04-14 17:35:47

Porlus (76 views)
2014-04-14 15:48:38

tom_mai78101 (101 views)
2014-04-10 04:04:31

BurntPizza (161 views)
2014-04-08 23:06:04

tom_mai78101 (256 views)
2014-04-05 13:34:39

trollwarrior1 (209 views)
2014-04-04 12:06:45

CJLetsGame (216 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!