- Home /
Scale a Vector3 Force depending to alignment of a Drag Vector3
Hi,
I like to simulate wind on a helicopter. The helicopter has different drag values and is flying in the air (CGM rc Heli Simulator). The goWind object is a seperate object where the wind is calculated and stored in it's transform coordinates. 0,0,0 means no wind. 0,0,0.5 means wind from left. What I try to do is, to add the wind force in the correct strength, depending to the orientation of the helicopter. The force direction is working, but the strength of the wind force is not scaled as wanted.
// This script is attached to the helicopter with rigidbody and other physic calculations
private var drag:Vector3 = Vector3 (0.2,5.0,0.2); // Helicopter Drag: sideways, up-down, forward
function FixedUpdate () {
var windDrag:Vector3 = Vector3.Scale(goWind.transform.position, drag);
rigidbody.AddForce (windDrag, ForceMode.Impulse);
}
Best regards and thanks in advance for help, Klaus
Scaling one force by another doesn't make make much sense to me. Typically you want to add forces together. That is drag is one force, and wind is a second force.
Thank you very much for your comment. $$anonymous$$aybe I have named that wrong. Drag is the air resistance of the helicopter. So the higher drag is in one axis, the more the force can move the helicopter in this axis.
Answer by robertbu · May 25, 2013 at 12:02 AM
I see one problem that may account for the "force not scaled as wanted." I'm assuming the helicopter is allowed to turn and that 'drag' is defined in terms of the helicopter (and therefore it is a local coordinate). windDrag is calculate as a world vector. You need to convert drag into world coordinates before you do you calculation. See Transform.TransformPoint() to make the conversion.
I'm only seeing a very small piece of the code, but it seems to me that what you really want is the relative wind direction. That is you add the negated velocity vector of the helicopter with the velocity of the wind and apply the drag to the result.
Thank you very much for your reply.
Yes, the helicopter is allowed to turn and 'drag' is defined in terms of the helicopter. 'drag' is a local coordinate. The wind force is a world vector. The x, y and z position values assign the strength and direction (relative to 0,0,0).
When I convert 'drag' to world coordinates, I get a wrong result. The wind increases depending to the position of the helicopter because the world coordinates get also the position of the helicopter i the world. Then I have tried to remove the helicopter position from these coordinates to get only the alignment coordinates for the multiplication with the force. But that doesn't work as expected. $$anonymous$$aybe I'm on a wrong path.
I have to scale the wind force depending to the angle alignment of the helicopter (not the position). $$anonymous$$aybe I have to solve that with angle calculations for each axis seperately.
A wind zone object which uses the physic of a piece of paper would be the solution.
You are right that Transform.TransformPoint() is the wrong thing to use. I think you want:
var worldDrag = transform.rotation * drag;
I don't have time at the moment to test it. If this doesn't work, let me know. Since I now understand what you want, I should be able to work it out.
Thank you very much for pointing me in the right direction.
This is the solution:
// This script is attached to the helicopter with rigidbody and other physic calculations
private var airResistance:Vector3 = Vector3 (0.15,1.0,0.07); // Helicopter air resistance: sideways, up-down, forward
private var goWind:GameObject; // reference to the Game Object 'Wind'
// cache this for performance reasons
private var _transform:Transform;
private var _rigidbody:Rigidbody;
private var _goWindTransform:Transform;
function Awake() {
_transform = transform;
_rigidbody = rigidbody;
goWind = GameObject.Find("Wind");
_goWindTransform = goWind.transform;
}
function FixedUpdate () {
// calculates air resistance depending to the alignment of the helicopter
var corrAirRes:Vector3 = _transform.rotation * airResistance;
var resWindDrag:Vector3 = Vector3(
$$anonymous$$athf.Abs(corrAirRes.x)*_goWindTransform.position.x,
$$anonymous$$athf.Abs(corrAirRes.y)*_goWindTransform.position.y,
$$anonymous$$athf.Abs(corrAirRes.z)*_goWindTransform.position.z);
_rigidbody.AddForce (resWindDrag, Force$$anonymous$$ode.Impulse); // add the wind to the helicopter
var pointA:Vector3 = _transform.position - Vector3.forward*0.5; // visual debugging
var pointB:Vector3 = pointA + resWindDrag;
Debug.DrawLine( pointA, pointB, Color.red);
}
There must be still a bug in that calculation in line 19. When the helicopter is blown from left, the resWindDrag is lower than when it is blown from right... a.s.o. So I tried to limit the angle values. But that doesn't work as expected.
var corrRotation:Quaternion = Quaternion.Euler(
(_transform.rotation.eulerAngles.x+180)%180,
(_transform.rotation.eulerAngles.y+180)%180,
(_transform.rotation.eulerAngles.z+180)%180);
var corrAirRes:Vector3 = corrRotation * airResistance;
I don't have any idea how to fix that.
I'll take a look this evening. Just to make absolutely sure, _goWindTransform represents a vector that is the direction and magnitude of the force of the wind in world coordinates. On a quick read, your $$anonymous$$athf.Abs() seems wrong to me.
Your answer
Follow this Question
Related Questions
Touchscript drag constraints 1 Answer
Drag rigidbody to crosshair 0 Answers
Drag-and-drop dot on mobile 2D 0 Answers
Drag object in unity 2d with moving camera 0 Answers
Keep vectors exactly 15 distance apart 2 Answers