Browse Source

collider calculation update

master
Cailean Finn 2 weeks ago
parent
commit
7c0cb9a9e6
  1. 6
      css/style.css
  2. 22
      js/Item.js
  3. 4
      js/ModelLoader.js
  4. 2
      js/Player.js
  5. 15
      js/main.js
  6. 1047
      package-lock.json
  7. 3
      package.json
  8. 2
      public/json/Items.json

6
css/style.css

@ -19,8 +19,8 @@ canvas {
left:0; left:0;
width:100vw; width:100vw;
height:100vh; height:100vh;
background:#000000; background:#ffffff;
background: radial-gradient(circle,rgba(0, 0, 0, 1) 0%, rgba(0, 0, 0, 0.9) 100%); background: radial-gradient(circle,rgb(255, 255, 255) 0%, rgba(255, 255, 255, 0.9) 100%);
opacity:1.0; opacity:1.0;
z-index:10; z-index:10;
display:flex; display:flex;
@ -33,7 +33,7 @@ canvas {
} }
#instructions { #instructions {
color:#fff; color:#000000;
text-align:center; text-align:center;
position: relative; position: relative;
font-size: 40px; font-size: 40px;

22
js/Item.js

@ -70,15 +70,27 @@ export class Item {
const size = new THREE.Vector3(); const size = new THREE.Vector3();
boundingBox.getSize(size); boundingBox.getSize(size);
// Store the offset from the pivot (base) to the visual center // Calculate the world center of the bounding box
this.centerOffset.set(0, size.y / 2, 0); const worldCenter = new THREE.Vector3();
boundingBox.getCenter(worldCenter);
const rbDesc = RigidBodyDesc.dynamic().setTranslation(this.object.position.x, this.object.position.y, this.object.position.z); // Calculate the local center by subtracting the object's world position
this.centerOffset.subVectors(worldCenter, this.object.position);
let rbDesc;
if (this.content === null) {
rbDesc = RigidBodyDesc.kinematicPositionBased();
} else {
rbDesc = RigidBodyDesc.dynamic();
}
rbDesc.setTranslation(this.object.position.x, this.object.position.y, this.object.position.z);
this.rb = this.rapierWorld.createRigidBody(rbDesc); this.rb = this.rapierWorld.createRigidBody(rbDesc);
const colDesc = ColliderDesc.cuboid(size.x / 2, size.y / 2, size.z / 2) const colDesc = ColliderDesc.cuboid(size.x / 2, size.y / 2, size.z / 2)
.setTranslation(0, this.centerOffset.y, 0); // Use the stored offset .setTranslation(this.centerOffset.x, this.centerOffset.y, this.centerOffset.z); // Use the calculated local center offset
this.coll = this.rapierWorld.createCollider(colDesc, this.rb); this.coll = this.rapierWorld.createCollider(colDesc, this.rb);
this.loadState = 'loaded'; this.loadState = 'loaded';
} catch (error) { } catch (error) {
console.error(`Failed to load model for tiem: ${this.name}`); console.error(`Failed to load model for tiem: ${this.name}`);
@ -225,7 +237,7 @@ export class Item {
} }
async _createContentDisplay() { async _createContentDisplay() {
console.log(this.contentDisplay) //console.log(this.contentDisplay)
// Prevent creating a new display if the old one is still fading out. // Prevent creating a new display if the old one is still fading out.
if (this.content instanceof AudioContent && this.content.isFadingOut) { if (this.content instanceof AudioContent && this.content.isFadingOut) {
return; return;

4
js/ModelLoader.js

@ -85,8 +85,8 @@ export class ModelLoader {
canvas.width = 1024; canvas.width = 1024;
canvas.height = 1024; canvas.height = 1024;
const ctx = canvas.getContext('2d'); const ctx = canvas.getContext('2d');
ctx.fillStyle = '#fff'; ctx.fillStyle = '#0044ffff';
ctx.fillRect(0, 0, 512, 512); ctx.fillRect(0, 0, 1024, 1024);
finalTexture = new THREE.CanvasTexture(canvas); finalTexture = new THREE.CanvasTexture(canvas);
} }

2
js/Player.js

@ -75,7 +75,7 @@ export class Player {
} else if(e.code == 'KeyF' && this.currentIntItem && !this.attachedItem){ } else if(e.code == 'KeyF' && this.currentIntItem && !this.attachedItem){
this.attachedItem = this.currentIntItem; this.attachedItem = this.currentIntItem;
this.attachedItem.isActive = true; this.attachedItem.isActive = true;
console.log("Attached item to player: ", this.attachedItem.object.name); //console.log("Attached item to player: ", this.attachedItem.object.name);
} }
if (e.code === 'Space' && this.attachedItem) { if (e.code === 'Space' && this.attachedItem) {

15
js/main.js

@ -23,7 +23,7 @@ import { ItemManager } from './ItemManager';
let scene, renderer; let scene, renderer;
let player; let player;
let rapierWorld, debugLines, itemManager; let rapierWorld, debugLines, itemManager;
let debugRapier = false; let debugRapier = true;
let isPaused = false; let isPaused = false;
let socket; let socket;
@ -86,17 +86,8 @@ async function init() {
/* Load Items/Content */ /* Load Items/Content */
itemManager = new ItemManager("json/Items.json", scene, rapierWorld, player, interactableItems); itemManager = new ItemManager("json/Items.json", scene, rapierWorld, player, interactableItems);
// const textContent = new TextContent("This is an example of dynamic text rendered onto a texture in Three.js. It can be used to display information about an object. This is an example of dynamic text rendered onto a texture in Three.js. It can be used to display information about an object. This is an example of dynamic text rendered onto a texture in Three.js. It can be used to display information about an object. This is an example of dynamic text rendered onto a texture in Three.js. It can be used to display information about an object. This is an example of dynamic text rendered onto a texture in Three.js. It can be used to display information about an object. This is an example of dynamic text rendered onto a texture in Three.js. It can be used to display information about an object.This is an example of dynamic text rendered onto a texture in Three.js. It can be used to display information about an object. This is an example of dynamic text rendered onto a texture in Three.js. It can be used to display information about an object. This is an example of dynamic text rendered onto a texture in Three.js. It can be used to display information about an object."); const item = new Item(rapierWorld, scene, player, true, new THREE.Vector3(0, 20, 0), 20, "test", '/models/init.glb', null, spawnedObjects, null);
// const imageContent = new ImageContent("images/test.webp"); await item.loadModel();
// const videoContent = new VideoContent("videos/sintel.mp4");
// const audioContent = new AudioContent("sounds/Violet.mp3", player.audioListener);
// const item = new Item(rapierWorld, scene, player, false, new THREE.Vector3(0, 1, 0), 20, "test", '/models/init.glb', null, spawnedObjects, null);
// await item.loadModel();
// const intItem = new Item(rapierWorld, scene, player, true, new THREE.Vector3(50, 1, 0), 5, "intTest", './models/cone.glb', null, spawnedObjects, audioContent);
// await intItem.loadModel();
// interactableItems.push(intItem);
instructions.innerHTML = "Click to play"; instructions.innerHTML = "Click to play";

1047
package-lock.json

File diff suppressed because it is too large

3
package.json

@ -20,6 +20,7 @@
"description": "", "description": "",
"devDependencies": { "devDependencies": {
"@types/dat.gui": "^0.7.13", "@types/dat.gui": "^0.7.13",
"dat.gui": "^0.7.9" "dat.gui": "^0.7.9",
"vite": "^7.1.7"
} }
} }

2
public/json/Items.json

@ -20,7 +20,7 @@
"contentType": "text", "contentType": "text",
"file": "./texts/example2.md", "file": "./texts/example2.md",
"isCollider": true, "isCollider": true,
"position": { "x": 5, "y": 50, "z": 0 }, "position": { "x": -100, "y": 1, "z": 0 },
"rotation": { "x": 0, "y": 0, "z": 0 }, "rotation": { "x": 0, "y": 0, "z": 0 },
"scale": 10 "scale": 10
} }

Loading…
Cancel
Save