Hello, I’m working on game design. I’m trying to create a 2D vector system for movement (if it’s tilted), but it’s not working as I’d like. The forums haven’t addressed this question yet.
Here is the code for my 3D, considering that yellow represents the front of the figure.
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader.js';
import * as THREE from 'three';
export function loadervoit(scene) {
const geometry = new THREE.BoxGeometry( 1, 2, 1 );
const material = new THREE.MeshBasicMaterial( {color: 0x0068FF } );
const cubeA = new THREE.Mesh( geometry, material );
const geometryB = new THREE.BoxGeometry( 1, 1, 1 );
const materialB = new THREE.MeshBasicMaterial( {color: 0xF7FF00 } );
const cubeB = new THREE.Mesh( geometryB, materialB );
cubeB.position.y = 1
const groupvoit = new THREE.Group();
groupvoit.add( cubeA );
groupvoit.add( cubeB );
scene.add( groupvoit );
return groupvoit
}
Here is the code to test the vector system.
const groupvoit = loadervoit(scene);
const speed = 0.01;
// Fonction pour mettre à jour la position de groupvoit en fonction de sa rotation
function updatePosition() {
// Calculer la direction basée sur la rotation actuelle de groupvoit
const direction = new THREE.Vector2(
Math.sin(groupvoit.rotation.x),
Math.cos(groupvoit.rotation.x)
);
// Crée un nouveau vecteur basé sur la direction et la vitesse
const displacement = direction.clone().multiplyScalar(speed);
// Met à jour la position de groupvoit en utilisant le vecteur de déplacement
groupvoit.position.y += displacement.x;
groupvoit.position.z += displacement.y;
// Redessine ou rafraîchit la scène si nécessaire
renderer.render(scene, camera);
}
Here is the code to test the movement system and then link the vector system to the button.
// debut vérif si touche est préssé
var keysPressed = {};
var alerte = false;
document.addEventListener("keydown", function (event) {
keysPressed[event.key] = true;
if (event.key === "z") {
if (alerte === false) {
alert(groupvoit.rotation.x);
var result = groupvoit.rotation.x / 90;
confirm(result);
alerte = true;
}
} else if (event.key === "a") {
alerte = false;
} else if (event.key === "e") {
groupvoit.rotation.x = 1.57;
} else if (event.key === "ArrowUp") {
groupvoit.position.y += 0.1;
} else if (event.key === "ArrowDown") {
groupvoit.position.y -= 0.1;
} else if (event.key === "ArrowLeft") {
if (groupvoit.rotation.x >= 6.29) {
groupvoit.rotation.x = 0;
}
groupvoit.rotation.x += 0.1;
} else if (event.key === "ArrowRight") {
if (groupvoit.rotation.x <= 0) {
groupvoit.rotation.x = 6.29;
}
groupvoit.rotation.x -= 0.1;
} else if (event.key === " ") {
console.log(groupvoit.position.x," ; ", groupvoit.position.y);
console.log(groupvoit.rotation.x);
}
// Si la touche "Shift" gauche est enfoncée
else if (event.key === "Shift") {
window.location.href = "test/test.html";
}
});
document.addEventListener("keyup", function (event) {
// Réinitialisez l'état de la touche lorsque la touche est relâchée
keysPressed[event.key] = false;
});
// fin vérif si touche est préssé
Knowing that I use Tween for the animations in Three.js.
The language used for the comments is French because I am French.
thanks for help