- Home /
Help matching ball velocity to calculated trajectory?
Hello,
I'm trying to make a 2d game where the "jump" mechanic is similar to the Angry Birds catapult. I want to draw the trajectory on screen as the player pulls back the mouse and then fire the ball (player) along the path when the mouse button has been released.
I found a formula for plotting trajectory here.
I'm moving a square to debug the position of the trajectory and the first attempt seems to work. I move the mouse around the ball and the little square is opposite the mouse and the further I pull back the further from the player it goes. When I release the mouse button, the ball travels at the right direction, but not with enough force.
I'm not sure where I'm going wrong, could anyone shed some light on this? I've been stuck for a week or so now.
Thanks
//waiting for mouse button to be lifted AND player initially clicked - FIRE!
if (Input.GetMouseButtonUp(0) && clickedPlayer == true){
playerCurrentPos.rigidbody.velocity = jumpVector * multiplier;
}
//Draw trajectory
if (Input.GetMouseButton(0)){
//Pull back position in screen space
jumpEnd = Input.mousePosition;
//Convert screen space to world space
p = Camera.main.ScreenToWorldPoint (Vector3 (jumpEnd.x, jumpEnd.y, 12.7));
//Try and detect angle
angle1 = (p - playerCurrentPos.transform.position);
angle2 = new Vector3(1, 0, 0);
angle = Vector3.Angle(angle1, angle2);
//Getting opposite angle
if (p.y <= playerCurrentPos.transform.position.y){
jumpAngle = (180 - angle) * Mathf.Deg2Rad;
}
else{
jumpAngle = angle * Mathf.Deg2Rad;
}
//Draw vector (player position to mouse drag position)
Debug.DrawLine(playerCurrentPos.transform.position, p, Color.red, 40);
//Work out jump vector
jumpVector = playerCurrentPos.transform.position - p;
//Vetical and horizontal velocity calculation
var xVelocity : float = (jumpVector.magnitude*multiplier) * Mathf.Cos(jumpAngle);
var yVelocity : float = (jumpVector.magnitude*multiplier) * Mathf.Sin(jumpAngle);
//Trajectory of X
var trajX : float = xVelocity * 0.5;
//Trajectory of Y
var trajY : float = (yVelocity * 0.5) - 0.5 * Physics.gravity.y * Mathf.Pow(0.5, 2);
//Debug - position a square object in the scene to match the velocity of the projectile after 0.5 seconds
square.transform.position = Vector3(trajX, trajY, 0);
}
You've posted a lot of code that would take a notable amount of time to figure out. I suggest you create a really cut down version of the problem...just a object launched at a specific angle along with the calculations on where it would land. You might find your own problem that way, or if you post it back here, it is more likely to get a response.
Thanks for the response robertbu, I'm new to scripting and not up on the etiquette of asking for help or showing off code. I've updated my original post and shortened the code to what I think is the crucial part of my problem. Any help on this would be much appreciated.
Thanks,
loupz
Answer by fafase · Feb 24, 2013 at 07:02 PM
From http://en.wikipedia.org/wiki/Trajectory_of_a_projectile
in order to get the height of an object at a distance x:
y = yStart + x*tan(angle) - g*(x*x)/(2*((v*cos(angle))*(v*cos(angle))));
though there is probably a simplified version of this...
I managed to find some time to try this but it it seemed to just be a straight line and the X positions were fixed.
I've just given this a go:
var Vxo = velocity * $$anonymous$$athf.Cos(jumpAngle);
var Vyo = velocity * $$anonymous$$athf.Sin(jumpAngle);
var trajX1 = playerCurrentPos.transform.position.x + Vxo * 1;
var trajY1 = playerCurrentPos.transform.position.y + Vyo * 1 - 0.5 * -9.81 * Time.deltaTime * 1 * 1;
square.transform.position = Vector3(trajX1, trajY1, 0);
var trajX2 = playerCurrentPos.transform.position.x + Vxo * 2;
var trajY2 = playerCurrentPos.transform.position.y + Vyo * 2 - 0.5 * -9.81 * Time.deltaTime * 2 * 2;
square2.transform.position = Vector3(trajX2, trajY2, 0);
I've still got the same issue as initially though. I am thinking it's to do with the way I'm calculating time as just a float or how I'm firing the ball:
playerCurrentPos.rigidbody.AddForce(playerCurrentPos.transform.right * jumpVector.magnitude * multiplier, Force$$anonymous$$ode.VelocityChange);
I'm going to keep trying though.
Thanks
PS - This is where I've got my latest equation from:
http://www.convertalot.com/ballistic_trajectory_calculator.html