Obi Official Forum

Full Version: add vector to obi cloth
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello.
I try to add vector to obi cloth. I have a error:Cannot implicitly convert type 'UnityEngine.Vector3' to 'float', but the direction of coding is not problem?

using UnityEngine;
using System.Collections;
using Obi;

[RequireComponent(typeof(ObiActor))]
public class DistanceAnchor : MonoBehaviour
{

    ObiActor actor;
    public ObiSolver solver;
    


    void Awake()
    {
        actor = GetComponent<ObiActor>();

    }

    void Update()
    {
        ObiSolver solver = actor.Solver;

        float invMassPerParticle = 0.01f;
        for (int i = 0; i < actor.invMasses.Length; ++i)
        {
            int indexInSolver = actor.particleIndices[i];
            actor.invMasses[i] = solver.invMasses[indexInSolver];
            actor.invMasses[i] = new Vector3(0.0f, 0.05f, 0.0f);
            
        }
    }
}
The invMasses array contains floats (scalars). You cannot assign a vector to a scalar value, it does not make sense:
Code:
actor.invMasses[i] = new Vector3(0.0f, 0.05f, 0.0f);

Maybe you intended to use the "positions" array instead?
(05-03-2019, 04:00 PM)josemendez Wrote: [ -> ]The invMasses array contains floats (scalars). You cannot assign a vector to a scalar value, it does not make sense:
Code:
actor.invMasses[i] = new Vector3(0.0f, 0.05f, 0.0f);

Maybe you intended to use the "positions" array instead?

I think you said obi cloth can move by vector, not transform. How can I move by vector on script?
(05-03-2019, 04:05 PM)Richard Wrote: [ -> ]I think you said obi cloth can move by vector, not transform. How can I move by vector on script?

By changing its particles' velocities or positions. For that you have to use either the "positions" or "velocities" arrays.

But trying to assign a vector to a scalar value does not make sense. which is what your code does. A float is a single scalar value, and a Vector3 is a point/direction in 3D space, composed of 3 scalars. Read more on data types, as it seems to me you're still hoping for things to work out of luck instead of trying to understand what you're doing and why.
(05-03-2019, 04:14 PM)josemendez Wrote: [ -> ]By changing its particles' velocities or positions. For that you have to use either the "positions" or "velocities" arrays.

But trying to assign a vector to a scalar value does not make sense. which is what your code does. A float is a single scalar value, and a Vector3 is a point/direction in 3D space, composed of 3 scalars. Read more on data types, as it seems to me you're still hoping for things to work out of luck instead of trying to understand what you're doing and why.

I thought that it is specific problem of obi cloth, because tutorial(http://obi.virtualmethodstudio.com/tutor...icles.html) writes "velocities" I cannot find out in document , not velocity. If that is a trivial question, I say sorry.

"positions" or "velocities" arrays means putting all particle in an array and multiply by transform.position or vector3.velocity?
(06-03-2019, 09:58 AM)Richard Wrote: [ -> ]I thought that it is specific problem of obi cloth, because tutorial(http://obi.virtualmethodstudio.com/tutor...icles.html) writes "velocities" I cannot find out in document , not velocity. If that is a trivial question, I say sorry.

The array is called "velocities". There's an example in that page that reads from it:

Code:
// solver index of the first particle in the actor.
int particleSolverIndex = actor.particleIndices[0];

// use it to get the particle's current velocity.
Vector3 velocity = solver.velocities[particleSolverIndex];

Writing is done in exactly the same way:

Code:
// solver index of the first particle in the actor.
int particleSolverIndex = actor.particleIndices[0];

// use it to set the particle's current velocity.
solver.velocities[particleSolverIndex] = new Vector3(1,0,0);

Quote:"positions" or "velocities" arrays means putting all particle in an array and multiply by transform.position or vector3.velocity?

No. All particle properties are already put in arrays by Obi, all you need to do is access these arrays to write to or read from them.
Also, multiplying by transform.position doesn't make any sense (maybe adding or setting, but not multipliying. If you multiply a position by another position, you're scaling it.)

Vector3.velocity doesn't even exist:
https://docs.unity3d.com/ScriptReference/Vector3.html

You got Vector3.one, Vector3.left, Vector3.up...etc, but no Vector3.velocity.

These are all extremely basic general programming questions. Please make sure you understand them before attempting more advanced stuff.