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:
// 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=?";
}
// 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.
There's a logic error.
Your PHP code always updates player 1's position if the session username (presumably the logged-in user?) matches the posted username. But both players can't be player 1!
Your PHP code and/or database data needs to keep track of which username is player 1 in the current game, and which username is player 2, and then update the positions according to that instead.
Also there seems to be a security flaw: If the posted player ID doesn't match the username in the session, it will cheerfully update player 2's position instead. But why? Surely a player should not be allowed to move another player's piece?
I don't see the logic in posting a player ID from the client-side at all - it's easy for someone malicious to fake, and there's no need for the client to specify which player is moving - it should always be the logged-in player who is moving, I would expect.