junkerstock
 vrx-test13 

<!DOCTYPE html>
<html>
<head>
<title>A-Frame - 3D巨大迷路</title>
<script src="https://aframe.io/releases/1.7.0/aframe.min.js"></script>
<script src="https://unpkg.com/aframe-look-at-component@0.8.0/dist/aframe-look-at-component.min.js"></script>
<script src="https://unpkg.com/aframe-troika-text/dist/aframe-troika-text.min.js"></script>

<script>
// --- SCRIPT BLOCK 1: Component Definitions ---
// --- プレイヤー移動制御用カスタムコンポーネント ('camera-relative-controls') ---
AFRAME.registerComponent('camera-relative-controls', {
schema: {
targetSpeed: { type: 'number', default: 5 },
acceleration: { type: 'number', default: 10 },
damping: { type: 'number', default: 8 },
brakingDeceleration: { type: 'number', default: 20 },
enabled: { type: 'boolean', default: true },
rotationSpeed: { type: 'number', default: 1.5 },
pitchLimit: { type: 'number', default: 85 },
verticalSpeed: { type: 'number', default: 30 },
groundY: { type: 'number', default: 0 }
},
init: function () {
this.keys = {};
this.leftThumbstickInput = { x: 0, y: 0 };
this.rightThumbstickInput = { x: 0, y: 0 };
this.currentVelocity = new THREE.Vector3();
this.ZERO_VECTOR = new THREE.Vector3();
this.cameraDirection = new THREE.Vector3();
this.cameraRight = new THREE.Vector3();
this.moveDirection = new THREE.Vector3();
this.desiredVelocity = new THREE.Vector3();
this.cameraWorldQuaternion = new THREE.Quaternion();
this.rigEl = this.el;
this.cameraEl = this.el.querySelector('[camera]');
this.isReady = false;
// --- ▼▼▼【壁判定ロジック用】ここから追加 ▼▼▼ ---
// 迷路データを格納するためのプロパティを準備します。
this.mazeData = null;
this.mazeGridSize = 0;
this.mazeCellSize = 0;
this.mazeOffset = 0;
// --- ▲▲▲【壁判定ロジック用】ここまで追加 ▲▲▲ ---
if (!this.cameraEl) { console.error('camera-relative-controls: カメラエンティティが見つかりません。'); }
this.el.sceneEl.addEventListener('loaded', () => {
this.leftHand = document.getElementById('leftHand');
if (this.leftHand) {
this.leftHand.addEventListener('thumbstickmoved', this.onLeftThumbstickMoved.bind(this));
} else { console.warn("camera-relative-controls: 左手コントローラー(#leftHand)が見つかりません。"); }
this.rightHand = document.getElementById('rightHand');
if (this.rightHand) {
this.rightHand.addEventListener('thumbstickmoved', this.onRightThumbstickMoved.bind(this));
} else { console.warn("camera-relative-controls: 右手コントローラー(#rightHand)が見つかりません。"); }
});
this.onKeyDown = this.onKeyDown.bind(this);
this.onKeyUp = this.onKeyUp.bind(this);
window.addEventListener('keydown', this.onKeyDown);
window.addEventListener('keyup', this.onKeyUp);
},
remove: function () {
window.removeEventListener('keydown', this.onKeyDown);
window.removeEventListener('keyup', this.onKeyUp);
if (this.leftHand) { try { this.leftHand.removeEventListener('thumbstickmoved', this.onLeftThumbstickMoved.bind(this)); } catch(e){} }
if (this.rightHand) { try { this.rightHand.removeEventListener('thumbstickmoved', this.onRightThumbstickMoved.bind(this)); } catch(e){} }
},
// --- ▼▼▼【壁判定ロジック用】ここから追加 ▼▼▼ ---
// 3D空間のワールド座標を、迷路配列のインデックスに変換する関数です。
worldToMazeCoords: function(worldX, worldZ) {
const i = Math.round(worldX / this.mazeCellSize + this.mazeOffset);
const j = Math.round(worldZ / this.mazeCellSize + this.mazeOffset);
return { i, j };
},
// --- ▲▲▲【壁判定ロジック用】ここまで追加 ▲▲▲ ---
onLeftThumbstickMoved: function (evt) { this.leftThumbstickInput.x = evt.detail.x; this.leftThumbstickInput.y = evt.detail.y; },
onRightThumbstickMoved: function (evt) { this.rightThumbstickInput.x = evt.detail.x; this.rightThumbstickInput.y = evt.detail.y; },
tick: function (time, timeDelta) {
if (!this.data.enabled) return;
if (!this.isReady) {
if (this.cameraEl && this.cameraEl.object3D && this.cameraEl.object3D.matrixWorld) { this.isReady = true; }
else { if (!this.cameraEl) { this.cameraEl = this.el.querySelector('[camera]'); } return; }
}
if (!this.cameraEl || !this.cameraEl.object3D || !this.rigEl || !this.rigEl.object3D) { return; }

const data = this.data;
const dt = timeDelta / 1000;
const position = this.rigEl.object3D.position;

// VRモードでの移動
if (this.rigEl.sceneEl.is('vr-mode')) {
if (Math.abs(this.rightThumbstickInput.x) > 0.1) {
const yawAngle = -this.rightThumbstickInput.x * data.rotationSpeed * dt;
this.rigEl.object3D.rotation.y += yawAngle;
}
// VRでの上下移動。下の地面チェックで制限される
if (Math.abs(this.rightThumbstickInput.y) > 0.1) {
const verticalMovement = this.rightThumbstickInput.y * data.verticalSpeed * dt;
position.y -= verticalMovement;
}
}

const cameraObject = this.cameraEl.object3D;
cameraObject.getWorldQuaternion(this.cameraWorldQuaternion);
this.cameraDirection.set(0, 0, -1).applyQuaternion(this.cameraWorldQuaternion);
if (this.cameraDirection.lengthSq() > 0.0001) this.cameraDirection.normalize();
this.cameraRight.set(1, 0, 0).applyQuaternion(this.cameraWorldQuaternion);
this.cameraRight.y = 0;
if (this.cameraRight.lengthSq() > 0.0001) this.cameraRight.normalize();

this.moveDirection.set(0, 0, 0);
if (this.keys['KeyW'] || this.keys['ArrowUp']) { this.moveDirection.add(this.cameraDirection); }
if (this.keys['KeyS'] || this.keys['ArrowDown']) { this.moveDirection.sub(this.cameraDirection); }
if (this.keys['KeyA'] || this.keys['ArrowLeft']) { this.moveDirection.sub(this.cameraRight); }
if (this.keys['KeyD'] || this.keys['ArrowRight']) { this.moveDirection.add(this.cameraRight); }
if (Math.abs(this.leftThumbstickInput.y) > 0.1) {
const forwardBackward = this.cameraDirection.clone().multiplyScalar(-this.leftThumbstickInput.y);
this.moveDirection.add(forwardBackward);
}
if (Math.abs(this.leftThumbstickInput.x) > 0.1) {
const leftRight = this.cameraRight.clone().multiplyScalar(this.leftThumbstickInput.x);
this.moveDirection.add(leftRight);
}

// --- ▼▼▼【壁判定ロジック用】isInputActive を isInputting に名前変更し、以降のロジックで利用します ▼▼▼
const isInputting = this.moveDirection.lengthSq() > 0.0001;
if (isInputting) { this.moveDirection.normalize(); }

let lerpFactor = data.damping;
const isCurrentlyMoving = this.currentVelocity.lengthSq() > 0.01;
if (isInputting) { // isInputActive を isInputting に変更
let isOpposingInput = false;
if (isCurrentlyMoving) {
const dotProduct = this.currentVelocity.dot(this.moveDirection);
if (dotProduct < -0.1) { isOpposingInput = true; }
}
if (isOpposingInput) {
this.desiredVelocity.copy(this.ZERO_VECTOR);
lerpFactor = data.brakingDeceleration;
} else {
this.desiredVelocity.copy(this.moveDirection).multiplyScalar(data.targetSpeed);
lerpFactor = data.acceleration;
}
} else {
this.desiredVelocity.copy(this.ZERO_VECTOR);
lerpFactor = data.damping;
}

const effectiveLerpFactor = 1.0 - Math.exp(-lerpFactor * dt);
this.currentVelocity.lerp(this.desiredVelocity, effectiveLerpFactor);
if (this.currentVelocity.lengthSq() < 0.0001) { this.currentVelocity.copy(this.ZERO_VECTOR); }

// --- ▼▼▼【壁判定ロジック用】ここから追加・変更 ▼▼▼ ---
if (this.currentVelocity.lengthSq() > 0) {
const deltaPosition = this.currentVelocity.clone().multiplyScalar(dt);

// 迷路データがセットされていれば、壁との衝突判定を実行します。
if (this.mazeData) {
const margin = 0.2; // プレイヤーの当たり判定の半径のようなものです。

// X方向の移動をチェックします。
const nextX = position.x + deltaPosition.x;
const signX = Math.sign(deltaPosition.x);
const { i: nextI_X, j: nextJ_X } = this.worldToMazeCoords(nextX + (margin * signX), position.z);

// 移動先が壁(1)の場合の処理です。
if (this.mazeData[nextI_X] && this.mazeData[nextI_X][nextJ_X] === 1) {
deltaPosition.x = 0; // X方向の移動量をゼロにします。
// プレイヤーからの入力がない場合(止まりたいのに滑っている場合)だけ、
// X方向の速度もゼロにして、滑りを完全に止めます。
if (!isInputting) {
this.currentVelocity.x = 0;
}
}

// Z方向の移動をチェックします。
const nextZ = position.z + deltaPosition.z;
const signZ = Math.sign(deltaPosition.z);
const { i: nextI_Z, j: nextJ_Z } = this.worldToMazeCoords(position.x, nextZ + (margin * signZ));

// 移動先が壁(1)の場合の処理です。
if (this.mazeData[nextI_Z] && this.mazeData[nextI_Z][nextJ_Z] === 1) {
deltaPosition.z = 0; // Z方向の移動量をゼロにします。
// プレイヤーからの入力がない場合だけ、Z方向の速度もゼロにします。
if (!isInputting) {
this.currentVelocity.z = 0;
}
}
}
position.add(deltaPosition);
}
// --- ▲▲▲【壁判定ロジック用】ここまで ▲▲▲

// 地面との衝突判定(基準コードから変更なし)
if (position.y < data.groundY) {
position.y = data.groundY;
if (this.currentVelocity.y < 0) {
this.currentVelocity.y = 0;
}
}
},
onKeyDown: function (event) { if (!this.data.enabled) { return; } if (['KeyW', 'ArrowUp', 'KeyS', 'ArrowDown', 'KeyA', 'ArrowLeft', 'KeyD', 'ArrowRight'].includes(event.code)) { this.keys[event.code] = true; } },
onKeyUp: function (event) { if (this.keys[event.code] !== undefined) { delete this.keys[event.code]; } }
});
// --- End of SCRIPT BLOCK 1 ---
</script>
</head>
<body>
<a-scene id="myScene" vr-mode-ui="enabled: true" background="color: #87CEEB">

<a-entity id="rig" camera-relative-controls="targetSpeed: 10; acceleration: 12; damping: 10; groundY: 0;">
<a-entity id="camera" camera="far: 20000;" look-controls="pointerLockEnabled: false; touchEnabled: false" position="0 1.6 0">
<a-entity id="mouseCursor" cursor="rayOrigin: mouse; fuse: false;" raycaster="objects: .maze-wall, a-sphere;" position="0 0 -1" geometry="primitive: ring; radiusInner: 0.02; radiusOuter: 0.03;" material="color: black; shader: flat; opacity: 0.7;"></a-entity>
</a-entity>
<a-entity id="leftHand" oculus-touch-controls="hand: left; model: true;"></a-entity>
<a-entity id="rightHand" oculus-touch-controls="hand: right; model: true;" raycaster="objects: .maze-wall, a-sphere;" laser-controls="hand: right; model: false; lineColor: white; lineOpacity: 0.75" ></a-entity>
</a-entity>

<a-entity light="type: ambient; color: #888"></a-entity>
<a-entity light="type: directional; color: #FFF; intensity: 0.8; castShadow: true" position="-100 200 100"></a-entity>

<a-plane id="ground" position="0 0 0" rotation="-90 0 0" width="500" height="500" color="#7BC8A4" shadow="receive: true"></a-plane>

<script>
// --- SCRIPT BLOCK 2: Main Scene Logic (Maze Integration) ---
document.addEventListener('DOMContentLoaded', function () {
const sceneEl = document.querySelector('a-scene');
if (sceneEl.hasLoaded) {
initializeWorld();
} else {
sceneEl.addEventListener('loaded', initializeWorld, {once: true});
}
const MAZE_GRID_SIZE = 99;
const MAZE_CELL_SIZE = 4;
const WALL_HEIGHT = 8;
function initializeWorld() {
console.log("Generating maze data...");
const mazeData = generateMazeData(MAZE_GRID_SIZE);
console.log("Creating 3D maze from data...");
create3DMaze(mazeData);
// --- ▼▼▼【壁判定ロジック用】ここから追加 ▼▼▼ ---
// 生成した迷路データを、プレイヤーの移動コンポーネントに渡します。
const rigEl = document.getElementById('rig');
const controls = rigEl.components['camera-relative-controls'];
if (controls) {
controls.mazeData = mazeData;
controls.mazeGridSize = MAZE_GRID_SIZE;
controls.mazeCellSize = MAZE_CELL_SIZE;
controls.mazeOffset = (MAZE_GRID_SIZE - 1) / 2;
console.log("Maze data has been set to the control component.");
}
// --- ▲▲▲【壁判定ロジック用】ここまで追加 ▲▲▲ ---
setPlayerStartPosition(mazeData);
createSpheres();
}
function generateMazeData(gridSize) {
let ary = Array.from({ length: gridSize }, () => Array(gridSize).fill(0));
for (let i = 0; i < gridSize; i++) {
ary[i][0] = 1; ary[i][gridSize - 1] = 1; ary[0][i] = 1; ary[gridSize - 1][i] = 1;
}
for (let i = 1; i < (gridSize - 1) / 2; i++) {
for (let j = 1; j < (gridSize - 1) / 2; j++) {
const x = i * 2; const y = j * 2;
ary[x][y] = 1;
const isStartArea = (i === 1 && j === 1);
let ranten;
if (isStartArea) {
ranten = Math.random() < 0.5 ? 0 : 2;
} else {
ranten = Math.floor(Math.random() * 4);
}
if (ranten === 0) { ary[x + 1][y] = 1; }
else if (ranten === 1) { ary[x - 1][y] = 1; }
else if (ranten === 2) { ary[x][y + 1] = 1; }
else if (ranten === 3) { ary[x][y - 1] = 1; }
}
}
return ary;
}
function create3DMaze(mazeData) {
const gridSize = mazeData.length;
const offset = (gridSize - 1) / 2;
for (let i = 0; i < gridSize; i++) {
for (let j = 0; j < gridSize; j++) {
if (mazeData[i][j] === 1) {
const wall = document.createElement('a-box');
const x = (i - offset) * MAZE_CELL_SIZE;
const y = WALL_HEIGHT / 2;
const z = (j - offset) * MAZE_CELL_SIZE;
wall.setAttribute('position', {x: x, y: y, z: z});
wall.setAttribute('width', MAZE_CELL_SIZE);
wall.setAttribute('height', WALL_HEIGHT);
wall.setAttribute('depth', MAZE_CELL_SIZE);
wall.setAttribute('color', '#A0522D');
wall.setAttribute('material', { roughness: 0.8 });
wall.setAttribute('shadow', 'cast: true; receive: false');
wall.classList.add('maze-wall');
sceneEl.appendChild(wall);
}
}
}
console.log("3D maze created.");
}
function setPlayerStartPosition(mazeData) {
const rigEl = document.getElementById('rig');
const gridSize = mazeData.length;
const offset = (gridSize - 1) / 2;
const startGridX = 1;
const startGridZ = 1;
const startWorldX = (startGridX - offset) * MAZE_CELL_SIZE;
const startWorldZ = (startGridZ - offset) * MAZE_CELL_SIZE;
rigEl.setAttribute('position', `${startWorldX} 0 ${startWorldZ}`);
console.log(`Player start position set to: X=${startWorldX}, Z=${startWorldZ}`);
}
function createSpheres() {
const numberOfSpheres = 30;
const spawnRadius = MAZE_GRID_SIZE / 2 * MAZE_CELL_SIZE * 1.1;
for (let i = 0; i < numberOfSpheres; i++) {
const sphereEl = document.createElement('a-sphere');
const radius = Math.random() * 3 + 0.5;
const angle = Math.random() * Math.PI * 2;
const distance = spawnRadius + Math.random() * 50;
const x = Math.cos(angle) * distance;
const z = Math.sin(angle) * distance;
const y = radius;
const color = `hsl(${Math.random() * 360}, 70%, 50%)`;
sphereEl.setAttribute('position', {x: x, y: y, z: z});
sphereEl.setAttribute('radius', radius);
sphereEl.setAttribute('color', color);
sphereEl.setAttribute('shadow', 'cast: true');
sceneEl.appendChild(sphereEl);
}
console.log(`${numberOfSpheres} spheres created around the maze.`);
}
});
</script>
</a-scene>
</body>
</html>


使用変数

-------( Function )
angle
ary
background
camera
cameraDirection
cameraEl
cameraObject
cameraRight
color
controls
create3DMaze -------( Function )
createSpheres -------( Function )
currentVelocity
cursor
data
deltaPosition
desiredVelocity
distance
dotProduct
dt
eftThumbstickInput
eraWorldQuaternion
ffectiveLerpFactor
forwardBackward
generateMazeData -------( Function )
geometry
ghtThumbstickInput
gridSize
height
i
id
initializeWorld -------( Function )
isCurrentlyMoving
isInputting
isOpposingInput
isReady
isStartArea
j
keys
leftHand
leftRight
lerpFactor
light
margin
material
mazeCellSize
mazeData
mazeGridSize
mazeOffset
MAZE_CELL_SIZE
MAZE_GRID_SIZE
moveDirection
nextX
nextZ
numberOfSpheres
offset
onKeyDown
onKeyUp
position
radius
ranten
raycaster
rigEl
rightHand
rotation
sceneEl
setPlayerStartPosition -------( Function )
shadow
signX
signZ
spawnRadius
sphereEl
src
startGridX
startGridZ
startWorldX
startWorldZ
ui
verticalMovement
wall
WALL_HEIGHT
width
worldX, worldZ) { const i = Math.round -------( Function )
x
X
y
yawAngle
z
Z
ZERO_VECTOR