I’m still working on my golf game (I’ve posted a few questions before). I’m blowing my brain out trying to figure out what to do to move the ball in any direction and applying the correct force. The problem is that the Ode function setForce takes three params : X, Y and Z. Now, to get the ball to move in a specific direction, I need to calculate the correct values between X and Y to move the ball (the Z value is just a matter of height).
Now, the formula I’m using is this:
H = (Y2 - Y1) / (X2 - X2)
H is then multiplied my the Y-value. Sure, the ball moves in the correct direction, but the problem is when the value of H becomes very large, i.e. > 2. It f***es up the power of the shot. If I’m shooting towards a destination where X and Y values are close to the same, everything is OK, but when I’m shooting towards a destication where X-value og Y-value are close to zero, I loose control of things.
Any good mathematitions online tonight ??
I’d take advantage of panda’s scenegraph instead :
- create a nodepath
- orient it so it match the direction
- get its Y+ vector relative to the ball, ie. ball.getRelativeVector(dirNP, Vec3(0,1,0))
I’m not sure that helps.
I should mention that the ball needs to move towards a certain target (i.e. the flag stick). So (X1, Y1) are the coordinates of the ball and (X2, Y2) are the coordinates of the flag stick.
I’ve been experimenting with setting the maximum force of either X or Y values to a certain value (i.e. 300) but then the ball doesn’t move the same distance in all directions.
Again, any help would be very much appreciated.
I don’t get what you are supposed to achieve with your H value, at best you could feed those the X and Y difference values into atan() to get the angle of the shot, but then you’d have to convert it right back to a force vector anyways.
What you should be doing is:
calculating the difference vector between positions, e.g., <X2 - X1, Y2 - Y1, 0>
normalizing the vector to get a direction vector without magnitude (calculate vector length, then divide all vector components by this length)
multiplying this vector by the shot power to get a force vector
Since you want to hit the ball to the hole you might even skip steps 2 and 3 and just try multiplying the vector in 1 by a constant (found by testing different values), since the farther the hole is from the ball, the larger this vector is and the more force you will want. However you will eventually need steps 2 and 3 so you can do things like set a user defined force or limit the maximum force of the shot.