首页 > 其他分享 >threejs 键盘控制运动 前进 后退 左转 右转

threejs 键盘控制运动 前进 后退 左转 右转

时间:2022-12-14 22:46:25浏览次数:51  
标签:threejs const matrix car carArrowNormal 0.01 右转 new 左转

输出效果

image

代码

import {
  PlaneGeometry,
  Scene,
  PerspectiveCamera,
  WebGLRenderer,
  Object3D,
  Clock,
  AnimationMixer,
  Color,
  AmbientLight,
  DirectionalLight,
  GridHelper,
  AxesHelper,
  Material,
  Vector3,
  BoxGeometry,
  MeshBasicMaterial,
  CylinderGeometry,
  MeshLambertMaterial,
  Mesh,
  Vector2,
  TextureLoader,
  CircleGeometry,
  cloneUniformsGroups,
  ArrowHelper,
  CurvePath,
  BufferGeometry,
  LineBasicMaterial,
  Line,
  SplineCurve,
  PointLightShadow,
  Matrix4,
  Matrix3,
} from "three";
import * as THREE from "three";
import * as TWEEN from "@tweenjs/tween.js";

type IPoint = {
  x: number;
  y: number;
};

export class Car {
  public car: Mesh;
  private carArrow: Vector2;
  constructor() {
    this.car = this.createCar();
    this.carArrow = new Vector2(1, 0);
  }

  private createCar() {
    const carParam = {
      width: 0.1,
      height: 0.05,
      depth: 0.05,
    };

    const geometry = new BoxGeometry(
      carParam.width,
      carParam.height,
      carParam.depth
    );

    const car = new Mesh(
      geometry,
      new MeshLambertMaterial({
        color: 0xffffff,
      })
    );

    car.position.z = carParam.depth * 0.5;

    const arrow = new THREE.Vector3(1, 0, 0);
    arrow.normalize();
    const origin = new Vector3(0, 0, 0);
    const arrowHelper = new ArrowHelper(arrow, origin, 0.3, 0xffff00);

    car.add(arrowHelper);

    return car;
  }

  public forward() {
    const carArrowNormal = this.carArrow.normalize();
    const matrix = new Matrix4();
    //   1    0     0    carArrowNormal.x*0.01
    //   0    1     0    carArrowNormal.y * 0.01
    //   0    0     1    0
    //   0    0     0    1
    matrix.set(
      1,
      0,
      0,
      carArrowNormal.x * 0.01,

      0,
      1,
      0,
      carArrowNormal.y * 0.01,

      0,
      0,
      1,
      0,

      0,
      0,
      0,
      1
    );
    this.car.applyMatrix4(matrix);
  }

  public backward() {
    const carArrowNormal = this.carArrow.normalize();
    const matrix = new Matrix4();
    matrix.set(
      1,
      0,
      0,
      -carArrowNormal.x * 0.01,

      0,
      1,
      0,
      -carArrowNormal.y * 0.01,

      0,
      0,
      1,
      0,

      0,
      0,
      0,
      1
    );
    this.car.applyMatrix4(matrix);
  }

  public turnleft() {
    const matrix = new Matrix3();
    const angle = Math.PI * 0.01;
    const sinValue = Math.sin(angle);
    const cosValue = Math.cos(angle);
    matrix.set(
      cosValue,
      -sinValue,
      0,

      sinValue,
      cosValue,
      0,

      0,
      0,
      1
    );
    this.carArrow.applyMatrix3(matrix);
    this.car.rotateZ(angle);
  }

  public turnright() {
    const matrix = new Matrix3();

    const angle = Math.PI * 0.01;
    const sinValue = Math.sin(angle);
    const cosValue = Math.cos(angle);

    matrix.set(
      cosValue,
      sinValue,
      0,

      -sinValue,
      cosValue,
      0,

      0,
      0,
      1
    );
    this.carArrow.applyMatrix3(matrix);
    this.car.rotateZ(-angle);
  }
}


标签:threejs,const,matrix,car,carArrowNormal,0.01,右转,new,左转
From: https://www.cnblogs.com/zhuoss/p/16983858.html

相关文章

  • threeJs 创建文字方式
    一,使用 TextGeometry创建3D文字1.需加载字体配合使用,使用THREE.FontLoader//加载字体loadFont(){returnnewPromise(function(resolve,reject){......
  • Threejs:光影
     环境光constcolor=0xFFFFFF;constintensity=1;constlight=newTHREE.AmbientLight(color,intensity);scene.add(light);创建地面constplaneSize=......
  • Threejs:捕获鼠标位置
     //获取鼠标坐标functiononPointerMove(event){//将鼠标点击位置的屏幕坐标转换成threejs中的标准坐标mouse.x=(event.clientX/window.innerWidth)*2-......
  • Threejs:创建矩阵
     设置顶点创建矩形constgeometry3=newTHREE.BufferGeometry();constvertices=newFloat32Array([-1.0,-1.0,1.0,1.0,-1.0,1.0,1.0,1.0,1.0,......
  • Threejs:创建纹理
    创建纹理//导入纹理constloader=newTHREE.TextureLoader();//加载所需要的纹理图片consttexture1=loader.load('./dist/texture/sea.jpg')constmaterial5......
  • Threejs:创建几何体——图元
     BoxGeometry盒子+MeshBasicMaterialconstgeometry=newTHREE.BoxGeometry(1,1,1);constmaterial=newTHREE.MeshBasicMaterial({color:0x00ff00});const......
  • Threejs教程-07-光源2
    接上节,讲了Threejs06”。本节具体讲解一些Threejs的知识点。首先有条件的话搞个服务器,这里还是用的三丰云免费云服务器,免费虚拟主机在这里不适用就不介绍了,安装的Centos7......
  • Threejs:渲染第一个场景
     导入方式1import*asTHREEfrom"three"; //设置场景constscene=newTHREE.Scene(); //初始化渲染器constrenderer=newTHREE.WebGLRenderer();......
  • Threejs:创建文字
    1.DOM+CSS2.将文字绘制到画布中,并将其用作Texture(纹理)如果你希望在three.js的场景中的平面上轻松地绘制文本,请使用此方法。3.在你所喜欢的3D软件里创建模型,并导......
  • Threejs:安装及其环境
    vue3框架安装:npminstallthree导入://方式1:导入整个three.js核心库import*asTHREEfrom'three';constscene=newTHREE.Scene(); //方式2:仅导......