Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (800)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (867)
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  
  Bullet Shooting 3D LWJGL  (Read 6285 times)
0 Members and 1 Guest are viewing this topic.
Offline TGStudiosDE

JGO Coder


Medals: 14
Projects: 3
Exp: 3 years


17 Years Old Developer from Germany :)


« Posted 2016-11-22 13:24:57 »

Hello Guys,

I have some Problems with my Bullet Class because it doesn't shoot in the right Direction.

Bullet Class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
private boolean hasHit = false;

   /** The owner of this Bullet. */
   private Entity owner;
   private double damage;

   private int time = 0;

   protected int ticksInGround;

   float speed = 0.012f;

   float gravity = 0.00027f;

   int textureID;
   
   public Bullet(Level level, Entity entity, float x, float y, float z, float hangle, float vangle, float downfall) {
      super(level);
      this.owner = entity;
      setSize(0.3F, 0.5F);
      this.damage = 3;

      this.motionX = ((float) Math.sin(Math.toRadians(hangle)) * speed);
      this.motionY = ((float) -Math.cos(Math.toRadians(hangle)) * speed);
      this.motionZ = -((float) Math.tan(Math.toRadians(vangle)) * speed);
     
      setPos(x, y, z);
   }

Contact me about Bugs: TGStudiosDE@gmail.com
Offline KaiHH

JGO Kernel


Medals: 783



« Reply #1 - Posted 2016-11-22 13:58:08 »

The rotation calculations are incorrect. A single sin() or cos() is too simple. (also you don't need a tan())
If you want to rotate around the X axis (via vangle) and around the Y axis (via hangle), you should start with building the rotation matrices:
https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations
Then you can do two matrix*vector multiplications with the "standard forward vector", which is the direction the bullet should shoot in with hangle/vangle = 0.0.
If you formulate that out, you'll get the resulting formula for the this.motionX/Y/Z variables.

EDIT: Example code how to derive the rotation formula:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
double xangle = toRadians(45); // <- example angle around x axis
double yangle = toRadians(45); // <- example angle around y axis
// standard "forward" vector (where to shoot at when xangle/yangle = 0.0
// assuming standard OpenGL convention with z=-1 pointing into screen)
double x = +0;
double y = +0;
double z = -1;
// apply rotation around x with assuming general (x, y, z) vector
// use https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations
double x2 = x * 1 + y * 0 + z * 0;
double y2 = x * 0 + y * cos(xangle) + z * -sin(xangle);
double z2 = x * 0 + y * sin(xangle) + z * cos(xangle);
// apply rotation around y with assuming general (x, y, z) vector
// use https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations
double x3 = x2 * cos(yangle) + y2 * 0 + z2 * sin(yangle);
double y3 = x2 * 0 + y2 * 1 + z2 * 0;
double z3 = x2 * -sin(yangle) + y2 * 0 + z2 * cos(yangle);
// we can simplify (x3, y3, z3) with assuming (0, 0, -1) vector:
double x4 = -cos(xangle) * sin(yangle);
double y4 = sin(xangle);
double z4 = -cos(xangle) * cos(yangle);
this.motionX = x4 * speed;
this.motionY = y4 * speed;
this.motionZ = z4 * speed;

You probably have to reverse the order of rotations and rotate around Y first, and then around X.
Offline SteveSmith
« Reply #2 - Posted 2016-11-22 15:03:30 »

I don't know if it's an option, but could you just take the direction of the shooter (x,y,z) and use those values for the bullet's motion values?

Java Multiplayer FPS Engine - https://github.com/SteveSmith16384/SteveTech1
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline TGStudiosDE

JGO Coder


Medals: 14
Projects: 3
Exp: 3 years


17 Years Old Developer from Germany :)


« Reply #3 - Posted 2016-11-24 13:15:28 »

Don't work with the Code above :/

Contact me about Bugs: TGStudiosDE@gmail.com
Offline KaiHH

JGO Kernel


Medals: 783



« Reply #4 - Posted 2016-11-24 14:15:36 »

You should get the idea: Apply rotations around the "horizontal" and the "vertical" axes (whatever that might be for you) by means of matrix*vector multiplications with the base rotation matrices.
The correct solution, which works for you, depends on:
- what transformations you do additionally
- what your "standard forward vector" is (i.e. where should your player look at when hangle and vangle are both zero, this also depends on your model which you want to rotate)
- how you set up your coordinate system (what is X, Y and Z, respectively, in your world)

Like I mentioned above, you can also try reversing the order of rotations. First rotate the vector around Y and then around X, which is (assuming (0, 0, -1) as "forward"):
1  
2  
3  
double x = -sin(yangle);
double y = cos(yangle) * sin(xangle);
double z = -cos(yangle) * cos(xangle);

Pages: [1]
  ignore  |  Print  
 
 

 
Riven (351 views)
2019-09-04 15:33:17

hadezbladez (5154 views)
2018-11-16 13:46:03

hadezbladez (2038 views)
2018-11-16 13:41:33

hadezbladez (5405 views)
2018-11-16 13:35:35

hadezbladez (1124 views)
2018-11-16 13:32:03

EgonOlsen (4546 views)
2018-06-10 19:43:48

EgonOlsen (5409 views)
2018-06-10 19:43:44

EgonOlsen (3087 views)
2018-06-10 19:43:20

DesertCoockie (3981 views)
2018-05-13 18:23:11

nelsongames (4555 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
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!