Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Solved] Algebra: get z-component from x and y component and angle  (Read 3522 times)

0 Members and 1 Guest are viewing this topic.

Guido_Ion

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Flow Soccer development
Hi, I'm programming a Football/Soccer game, currently I'm working on the height of the kick. The game is top down so I'm simulating the height of the ball with calculations and visual aids.

The kick consists of a 2d vector with it's magnitude (force) and it's direction. From this information and the angle of the kick in the z-axis (height, angle a in the image) that I assign according to the force, I want to get the z-component of the vector.

From hours of researching I get that I can get the x-component like this: std::abs(magnitude) * cos(angleInDegrees) and the y-component like this: std::abs(magnitude) * sin(angleInDegrees). But of course I have these components in the vector itself.

I have this so far for the z-component:
Quote
initialAngle = getAngleRelativeToForce(force); // this returns an angle from 0 to 60 according to the force of the kick (this works fine)
b2Vec2 resultingVector = force * direction;
float sinAngleY = resultingVector.x / std::abs(force); // angle between y-axis and hypotenus (x-component/hypotenuse)
// Formula for z-component: Force * sinAngleY * sin(initialAngle)
forceInZAxis = std::abs(force) * sinAngleY * std::sin(Game::DegreesToRadians(initialAngle));

This gives me weird behaviors: for example similar kick with huge height when I'm facing up, and no height when facing down. Or a strong kick with no height.

I'm stuck there, I keep reseraching but I feel there is something I'm missing and maybe someone can point it out.
« Last Edit: July 24, 2021, 12:28:03 am by Guido Bisocoli »

kojack

  • Full Member
  • ***
  • Posts: 170
    • View Profile
Re: Algebra: get z-component from x and y component and angle
« Reply #1 on: July 22, 2021, 10:55:08 pm »
Have a look at "spherical coordinates". For example: https://mathinsight.org/spherical_coordinates
From there, the formulas would be:
(phi is vertical angle of kick, theta is horizontal angle of kick, rho is strength of kick)
x = rho * sin(phi) * cos(theta)
y = rho * sin(phi) * sin(theta)
z = rho * cos(theta)

Guido_Ion

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Flow Soccer development
Re: Algebra: get z-component from x and y component and angle
« Reply #2 on: July 24, 2021, 12:27:34 am »
Thank you very much! I was able to make it work, this is the final code for the formula:

Quote
forceInZAxis = 0.28f * force * std::cos(Game::DegreesToRadians(90 - initialAngle));

I had to add a multiplier (0.28f) because the force in the z-axis was too strong, but that's just for my game. Thanks again!