Issue with object rotation

hey there,

still working on my ELOCKI-Thing and i did a lot. At the moment i got a problem with rotation and i read a lot (beginnerstutorial, issues) but i did not get the point. I think i do not rotate my elocki properly…it seems to do something with the camera. i can go left and right but when i rotate it (with backspace or arrow-up) the scene is completly de-arranged.
So…do you have a hint for me.
(you can see a lot in the video and the part in the script)


here is the part of the init
// model
const modelSqu = new THREE.Object3D();
new GLTFLoader()
.setPath(‘models/gltf/’)
.load(‘EGCArena.gltf’, function (gltf) {
const box = new THREE.Box3().setFromObject(gltf.scene);
//const box = new THREE.Box3().setFromObject(gltf.scene);
// three.js docs
const boxHelper = new THREE.Box3Helper(box, 0xffff00);
scene.add(boxHelper); // see original position of model.gltf, not centered
const c = box.getCenter(new THREE.Vector3());
const size = box.getSize(new THREE.Vector3());
// center the gltf scene - important for modelSqu.rotation.y = t in function animate
//gltf.scene.position.set(-c.x, size.y / 2 - c.y, -c.z); // put // in front of this line, try it out
gltf.scene.position.set(-c.x, size.y / 2 - c.y, -c.z); // put // in front of this line, try it out
modelSqu.add(gltf.scene); // this gltf.scene is centered
//modelLh.scale.set(2, 2, 2); // because gltf.scene is big
modelSqu.position.set(0, 0, initialMoveZ);
scene.add(modelSqu);

				//const object = gltf.scene.getObjectByName( 'SheenChair_fabric' );

				const gui = new GUI();

				//gui.add( object.material, 'sheen', 0, 1 );
				gui.open();

			});

		//elocki
		egcElocki = new egcElockiType();
		egcElocki.scale.set(1, 1, 1);
		egcElocki.position.set(0, 3, initialMoveZ);
		scene.add(egcElocki);
		console.log(egcElocki.position);
		const box = new THREE.Box3().setFromObject(egcElocki);
		//const box = new THREE.Box3().setFromObject(gltf.scene);
		// https://threejs.org/docs/index.html#api/en/helpers/Box3Helper
		const boxHelper = new THREE.Box3Helper(box, 0xffff00);
		scene.add(boxHelper); // see original position of model.gltf, not centered
		const c = box.getCenter(new THREE.Vector3());
		const size = box.getSize(new THREE.Vector3());

and here are the orbit:

		controls = new OrbitControls(camera, renderer.domElement);
		controls.enableDamping = true;
		controls.minDistance = 1;
		controls.maxDistance = 10;
		controls.target.set(0, 0.35, 0);
		controls.update();

		window.addEventListener('resize', onWindowResize);

just to say : i build the table in theejs-editor and import it.

THANKS for your help (without that i could not continue!)

Rotating the camera and rotating an object are two VERY different things.

Please show us your backspace and arrow-up handlers.

On a side note: I seem to remember an identical project from a user named @Krungle. While it’s perfectly fine to start a new thread for a new aspect of your project, there is no need to change your username, too :thinking:

1 Like

Hey, yes you are right i am the same person. I changed the Account because i saved everything in a git -repo. And with that account i will continued working :grin:.

  1. the Arrow Handling could be Seen in the Video in the right Side.. but i send it soon again.
  2. what do you mean with the Background…Kamera, Scene or ?
    Thx from ELOCKI :wink:

arrow
document.onkeydown = function (e) {
console.log(e);
var elockiPostion = egcElocki.position;
var ballPosition = egcBall.position;
var gapX = elockiPostion.x - ballPosition.x
console.log("Positionen: " , gapX,elockiPostion,ballPosition);
var move;

		if (e.keyCode === 37) {
			if (egcElocki.position.x > -6) {
				egcElocki.position.x -= 1;
			}
		} else if (e.keyCode === 39) {
			if (egcElocki.position.x < 6) {
				egcElocki.position.x += 1;
			}
		} else if (e.keyCode === 38) {
			//elocki.position.z +=1;
			egcElocki.rotateX(angleBack);
		} else if (e.keyCode === 40) {
			egcElocki.rotateX(angleForward);
			//elocki.position.z -=1;
		} else if (e.keyCode === 65) {
			egcElocki.rotateX(45);
		} else if (e.keyCode === 83) {
			egcElocki.rotateY(45);
		} else if (e.keyCode === 32) {
			//egcElocki.rotateX(-90);
			egcElocki.rotateX(180);
			//egcElocki.position.y=0;
			//egcElocki.position.z=0;
		} else if (e.keyCode === 81) {

			//elocki.changeColor();
		}
		camera.updateProjectionMatrix();
		renderer.render(scene, camera);


	}

init():
function init() {

		const container = document.createElement('div');
		document.body.appendChild(container);

		//camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100);
		//camera.position.set(0, 0, 5);
		//camera.position.z = 5;
		scene = new THREE.Scene();

		renderer = new THREE.WebGLRenderer({ antialias: true });
		renderer.setSize(innerWidth, innerHeight);
		//renderer.setAnimationLoop(animationLoop);
		document.body.appendChild(renderer.domElement);

		camera = new THREE.PerspectiveCamera(80, innerWidth / innerHeight, 1, 500);
		camera.position.set(0, 5, 10);
		//camera.lookAt(scene.position);

		cameraControls = new OrbitControls(camera, renderer.domElement);
		cameraControls.target.set(10, 10, 10);
		cameraControls.maxDistance = 400;
		cameraControls.minDistance = 10;
		cameraControls.update();


		// lights
		const mainLight = new THREE.PointLight(0xe7e7e7, 2.5, 250, 0);
		mainLight.position.y = 60;
		scene.add(mainLight);

		const greenLight = new THREE.PointLight(0x00ff00, 0.5, 1000, 0);
		greenLight.position.set(550, 50, 0);
		scene.add(greenLight);

		const redLight = new THREE.PointLight(0xff0000, 0.5, 1000, 0);
		redLight.position.set(- 550, 50, 0);
		scene.add(redLight);

		const blueLight = new THREE.PointLight(0xbbbbfe, 0.5, 1000, 0);
		blueLight.position.set(0, 50, 550);
		scene.add(blueLight);


		// loading the texture
		//const managerTexture = new THREE.LoadingManager();
		//const textureLoader = new THREE.TextureLoader(managerTexture);
		//const texturBall = textureLoader.load('app/textures/ball.png', render);
		//texture.colorSpace = THREE.SRGBColorSpace;



		// model

		new GLTFLoader()
			.setPath('models/gltf/')
			.load('EGCArena.gltf', function (gltf) {

				scene.add(gltf.scene);

				//const object = gltf.scene.getObjectByName( 'SheenChair_fabric' );

				const gui = new GUI();

				//gui.add( object.material, 'sheen', 0, 1 );
				gui.open();

			});

		//elocki
		egcElocki = new egcElockiType();
		egcElocki.position.set(0, 2.5, 0);
		scene.add(egcElocki);
		console.log(egcElocki.position);
		
		//egcElocki.translateZ(-25);
		//egcElocki.translateY(25);
		
		let factor = 0.5;
		egcElocki.scale.x = factor;
		egcElocki.scale.y = factor;
		egcElocki.scale.z = factor;
		

		//ball
		egcBall = new egcBallType();
		scene.add(egcBall);
		egcBall.position.set(0, 1, 0);
		console.log(egcBall.position);

		renderer = new THREE.WebGLRenderer({ antialias: true });
		renderer.setPixelRatio(window.devicePixelRatio);
		renderer.setSize(window.innerWidth, window.innerHeight);
		renderer.setAnimationLoop(animate);
		renderer.toneMapping = THREE.ACESFilmicToneMapping;
		renderer.toneMappingExposure = 1;
		container.appendChild(renderer.domElement);
		/*
					const environment = new RoomEnvironment();
					const pmremGenerator = new THREE.PMREMGenerator(renderer);
		
					scene.background = new THREE.Color(0xbbbbbb);
					scene.environment = pmremGenerator.fromScene(environment).texture;
		*/
		controls = new OrbitControls(camera, renderer.domElement);
		controls.enableDamping = true;
		controls.minDistance = 1;
		controls.maxDistance = 10;
		controls.target.set(0, 0.35, 0);
		controls.update();

		window.addEventListener('resize', onWindowResize);

	}

Nobody is talking about background.

I was asking for your Backspace handler:

It does nor become clear what the difference between ‘elocki’ and ‘egcElocki’ is. Bofore we go any further, I guess, this may be a perfect moment to learn about how one can publish a live code example on platforms like codepen.io or similar.

okay…not a problem.

  1. do i need my own account or do you see what i just past in ?
  2. i put a few things in seperate files (for elocki and ball) - what shall i do with these things.
  3. i have a glft-file too.
  4. do you want to see the repo ?

and i have to put it in the html-css-js structure…

hey, here is the link: https://codepen.io/elocki/pen/raaaEdG

Question: glft and js files ?

Did you see your app working on codepen.io? Me neither. :wink:

But I honestly appreciate you seriously tried.

I see several issues here.

in the HTML section, you need to reference locations that codepen.io knows about. This is where CDNs come into play.

I changed your html to this:

     "imports": {
          "three": "https://cdn.jsdelivr.net/npm/three@0.174.0/build/three.module.js",
           "three/addons/": "https://cdn.jsdelivr.net/npm/three@0.174.0/examples/jsm/",
           "three/webgpu": "https://cdn.jsdelivr.net/npm/three@0.174.0/build/three.webgpu.js",
           "three/tsl": "https://cdn.jsdelivr.net/npm/three@0.174.0/build/three.tsl.js"
            }

You local stuff ( ‘./. ..’) is also impossible for codepen.io to access:

//egc-things
import { egcElockiType } from './egcElockiType.js';
import { egcBallType } from './egcBallType.js';

You’ll have to upload these to some publically available webspace, so codepen.io can access it.

The same also applies for any assets your code wants to access.

It’s best though, if you manage to strip down your problem without relying on too many external resources. Keep trying - you will profit many times over once you get this working. And people on this forum will be much more willing to look into a working code in the future!

1 Like

okay…that is what i was expecting. But i keep on and maybe i can locate and isolate it better. Thx for you help.