Как перевести вектор из вектора смещения - PullRequest
0 голосов
/ 27 марта 2020

Как перевести вектор из вектора смещения.

У меня есть линия с 2 точками, одна на начале линии и одна на конце вектора линии.

enter image description here

Я хочу перевести линию и точки, основанные на позиции DragConstrols, а не основывать на позиции линии. Я просто обновляю вершины геометрии линии и базу позиций точек на линии. начальный и конечный векторы.

Помните, что точки не являются дочерними по отношению к линии, все объекты являются дочерними по отношению к сцене. Линия хранит точки внутри пользовательских данных линии.

Моя цель это перевести все эти 3 объекта из смещения перетаскивания Vector3

Вот скриншоты линии с точками. start_vector = (-140, 60, 0) end_vector = (-70, 100, 0)

Я знаю вектор начала линии до того, как пользователь перетаскивает линию v1 = (-140, 60, 0), и я знаю вектор конца линии до перетаскивания v2 = (-70, 100, 0)

Теперь, когда пользователь перетаскивает линию с помощью DragConstrols я не позволяю dragControls изменять положение линии, я получаю вектор dragConstrol3 и хочу перевести базу линейных ветеринарных данных на этот вектор.

DragControls всегда начинаются с (0, 0, 0), поэтому я пытаюсь добавить линейные векторы (v1, v2) с позицией, но я получаю этот результат.

enter image description here

Может кто-нибудь показать мне, как установить начало и конец линии вектор из позиции dragControls. Или как перевести векторы из смещенных векторов.

Спасибо.

Мой код пока.

Имейте в виду, что линия - это Линия 2 (жирная линия) three/examples/jsm/lines/Line2



class NewLine extends Line2 {

  private _start = new Vector3();
  private _end = new Vector3();

  /** The viewport width */
  public viewportWidth: number;

  /** The viewport height */
  public viewportHeight: number;

  /** The line3 */
  public line3 = new Line3();

  public get start() { return this._start; }
  public set start(v: Vector3) {
    this._start = v;
    this.line3.set(v, this._end);
    this.geometry['setPositions']([
      v.x,
      v.y,
      v.z,
      this._end.x,
      this._end.y,
      this._end.z
    ]);
    this.geometry['verticesNeedUpdate'] = true;  // maybe i don't need that
    this.geometry.computeBoundingSphere();
    this.computeLineDistances();
  }

  public get end() { return this._end; }
  public set end(v: Vector3) {
    this._end = v;
    this.line3.set(this._start, v);
    this.geometry['setPositions']([
      this._start.x,
      this._start.y,
      this._start.z,
      v.x,
      v.y,
      v.z,
    ]);
    this.geometry['verticesNeedUpdate'] = true; // maybe i don't need that
    this.geometry.computeBoundingSphere();
    this.computeLineDistances();
  }


  constructor( start: Vector3, end: Vector3 ) {
    super();

    // create geometry
    const geometry = new LineGeometry();
    geometry.setPositions([
      start.x, start.y, start.z,
      end.x, end.y, end.z
    ]);
    this.geometry = geometry;

    // create material
    const material = new LineMaterial({
      color: 0x000000,
      linewidth:5,
      depthTest: false, // new
      vertexColors: false,
    });
    this.material = material;

    this.computeLineDistances();
    this.scale.set( 1, 1, 1 );
    this.line3.set(start, end);

  }

}



/**
 * Translates a line base on DragControls position.
 * line: The NewLine
 * position: The new Vector3 position from dragControls
 */
translateLineFromVector(line: NewLine, position: Vector3) {

  const v1 = line.start;
  const v2 = line.end;

  const offset_start = line.start.clone().add( position ).sub(line.position);
  const offset_end = line.end.clone().add( position ).sub(line.position);

  line.start.copy( offset_start );
  line.end.copy( offset_end );
}

1 Ответ

0 голосов
/ 27 марта 2020

Стандартный подход для перетаскивания:

On mouse down(X,Y):
    If Clicked at object and not Dragging:
         Dragging = True
         X0 = X
         Y0 = Y
         //important - remember object coordinates at this moment!
         V1_0 = V1
         V2_0 = V2

On mouse moving (X,Y) :
   If Dragging: 
       //Draw object at coordinates shifted by (X - X0), (Y - Y0)
       //from initial position, not from the current one
       V1.X = V1_0.X + X - X0
       V1.Y = V1_0.Y + Y - Y0
       same for V2
       DrawObject(V1,V2)


On Mouse Up(X,Y):
   If Dragging: 
       Fragging = False
       Fix positions V1,V2 if needed
...