[SOLVED] Apply Force at certain angle regardless of camera view

Hey guys, I am trying to make a wind effect for my golf game, I get an “angle” value from 0 to 360 from the server, and I want to apply a force to the ball in the direction of the angle:
Tried this:
Let’s say the angle is passed as 90:

let force = 4;
this.windFactorInX = Math.cos(angle * Math.PI / 180) * force;
this.windFactorInZ = Math.sin(angle * Math.PI / 180) * force;

// this.windFactorInX will be calculated 0 here
// this.windFactorInZ will be calculated 4 here

this.ball.rigidbody.applyForce(this.windFactorInX, 0, this.windFactorInZ);`

but this seems to be flawed, it “only” works at a certain angle, the camera is facing eg:
At this angle from the camera, this will work, as the blue component is the Z component and also is 90 degrees, so the ball will go left correctly:

But if the camera angle changes to this, the same 90 degrees given will give the same X and Z values but now the X and Z components in the world space are exchanged so the ball goes towards the camera, because the Z: 4 force is being applied.

  1. Made an example project: https://playcanvas.com/editor/scene/1282090
    Scene: Wind Correct The angle 90 is set on the wind arrow, and the ball moves correctly to where the arrow is pointing,
    Vs Scene: Wind Incorrect In this scene, I only change the camera position but the ball doesn’t move towards where the arrow is pointing.
    The same script is on the ball in both the scenes, just the camera position is changed, but I want the ball to move towards the pointing arrow in both cases.

Click the white button to throw the ball in both scenes

Hi @Nole

I have fixed your example and I have tested in both the scenes:

Fixed Project:
https://playcanvas.com/project/855775/overview/wind-effect

I will explain this through a diagram:

1 Like