Projectile Motion in 3D space (Writing an Aimbot)

Click For Summary

Discussion Overview

The discussion revolves around calculating the correct pitch angle for a projectile (arrow) in a 3D space for an aimbot in a game, considering the effects of gravity and the distance to the target entity. Participants explore the implementation of a trajectory calculation algorithm and address issues related to the code provided.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • The original poster describes their approach to calculating the pitch angle using a trajectory algorithm and shares their code implementation.
  • Some participants suggest that there may be a degrees vs radians issue affecting the calculations, particularly with the use of Math.atan2().
  • Others point out a potential discrepancy in the formula used for gravity, noting that the wiki equation states g*x^2, while the poster used (g*x)^2 for gxsquared.
  • It is noted that the variable z does not appear to be utilized in the second function, which could impact the calculations.

Areas of Agreement / Disagreement

Participants express various concerns and suggestions regarding the code, but there is no consensus on the exact cause of the issue or a definitive solution provided.

Contextual Notes

The discussion highlights potential limitations in the code related to the handling of angles and the use of variables, but these remain unresolved.

wolfknight
Messages
5
Reaction score
0
Alright, so, I am writing an aimbot for a popular game, and I need a bit of help with calculating the correct pitch angle for the player. The weapon is a bow, so the arrows are affected by gravity. I am trying to find the pitch angle that accounts for gravity and distance from the Entity you are firing at.

My current code doesn't work, but I will post it anyways. I have researched the correct algorithm (I found it at http://en.wikipedia.org/wiki/Trajectory_of_a_projectile#Angle_required_to_hit_coordinate_.28x.2Cy.29) and have attempted to implement it. However, when the aimbot is being used, for some reason, it stays in the middle of my screen, regardless of the entity's Y value or the player's Y value. I have the velocity correct, and the gravity constant correct, and that is all I know.
Anyways, enough rambling, here is my code:
Code:
	private float getBowPitchFromEntity(Entity e) {
		double x = getX(e);
		double y = getY(e);
		double z = getZ(e);
		double velocity = getVelocity();
		double posX = Math.pow(x, 2) + Math.pow(z, 2);
		double height = y - getY(getPlayer());
		return (float)getTrajectory(posX, z, velocity, height);
	}
	
	
	private double getTrajectory(double x, double z, double velocity, double height) {
		double vsquared = Math.pow(velocity, 2);
		double vquad = Math.pow(velocity, 4);
		double gxsquared = Math.pow(x * gravityConstant, 2);
		double gx = x * gravityConstant;
		double twoyvsquared = 2 * Math.pow(height * velocity, 2);
		double add = Math.atan2(vsquared + Math.sqrt(vquad - (gravityConstant * (gxsquared + twoyvsquared))), gx);
		double sub = Math.atan2(vsquared - Math.sqrt(vquad - (gravityConstant * (gxsquared + twoyvsquared))), gx);
		return Math.min(add, sub);
	}

The gravity constant is -0.05D, and the velocity doesn't matter, as I have compared it to the in-game arrow velocity and it matches.

If anyone can help, I would appreciate it.
 
Physics news on Phys.org
could you have a degrees vs radians issue? I noticed the last eqns using Math.atan2()

I also noticed that the wiki eqn says g*x^2 but you've used (g*x)^2 for gxsquared

Lastly, z doesn't appear to be used in the second function.
 
Last edited:
jedishrfu said:
could you have a degrees vs radians issue? I noticed the last eqns using Math.atan2()

I also noticed that the wiki eqn says g*x^2 but you've used (g*x)^2 for gxsquared

Lastly, z doesn't appear to be used in the second function.

That might possibly be my problem. I got mad and deleted the module last night, so I'll rewrite it and try again. Thank you very much for your help!
 
wolfknight said:
That might possibly be my problem. I got mad and deleted the module last night, so I'll rewrite it and try again. Thank you very much for your help!

Note to self: Never get mad at code and delete it instead comment it out in a more gentle deprecational mode.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
40
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 21 ·
Replies
21
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 14 ·
Replies
14
Views
5K