Issue with Cube Positioning in Three.js and PHP Multiplayer Game
I'm developing a multiplayer game where players control cubes (representing themselves) in a 3D environment rendered using Three.js. The game backend is handled with PHP and MySQL for storing game data, including cube positions. Each player's cube should move independently based on their input, but I'm encountering an issue where the red cube's position seems to mirror the green cube's position in the database after movements.
Problem Description:
Red Cube Issue: When either player moves their cube, the red cube's position in the database updates to match the green cube's position instead of maintaining its own independent position. This results in both cubes appearing in the same location in the game world after movements.
Green Cube Functionality: The green cube behaves correctly, moving independently and updating its position accurately in the database.
Code Overview:
- PHP (game.php): Handles updates to cube positions based on player input and stores them in a MySQL database. Here's an example of how positions are updated:
// Example of how positions are updated in game.php if ($_SESSION['username'] == $_POST['player']) { $query = "UPDATE fight SET player1_position_x=?, player1_position_y=?, player1_position_z=? WHERE id=?"; } else { $query = "UPDATE fight SET player2_position_x=?, player2_position_y=?, player2_position_z=? WHERE id=?"; }
- JavaScript (Three.js): Controls cube movements on the client-side and sends AJAX requests to update and fetch cube positions. Here's an example of how positions are updated:
// Example of how positions are updated in JavaScript function updateCubePosition(x, y, z, player) { const xhr = new XMLHttpRequest(); xhr.open('POST', 'game.php?fight_id=<?php echo $fightId; ?>', true); // ... }
What I've Tried:
Ensured that AJAX requests correctly send and receive cube positions.
Verified that PHP scripts handle updates based on the correct player identity (
$_SESSION['username']
vs.$_POST['player']
).Confirmed that the JavaScript handles cube movements and updates positions accurately on the client-side.
Request for Assistance:
I'm seeking guidance on why the red cube's position in the database does not update independently and instead mirrors the green cube's position after movements. How can I ensure each cube (player) maintains its own position without interference?
Any insights or suggestions on debugging or fixing this issue would be greatly appreciated. Thank you for your help!=
When I opened the game I could move green perfectly and update the position on the DB. In a different browser I can make the green move but when I try to move the green in the separate browser the red becomes green, or it moves but doesnt save positions, or green follows red and becomes green - very confusing.