skip to Main Content

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.

2

Answers


  1. To solve the issue where the red cube’s position seems to mirror the green cube’s position in the database you need to ensure that both client-side and server-side code correctly differentiate between the two players and update their respective positions independen

    Ensure that the PHP script correctly identifies which player is making the request and updates the appropriate fields in the database The code snippet you provided should work if the correct player identity is being passed and checked

    Ensure that the JavaScript code correctly identifies the player and sends the correct player identity along with the position data to the server

    Verify that the database is being updated correctly for both players. Run a query to check the fight table and ensure that positions for player1 and player2 are stored correctly:

    Add debugging logs in both your PHP and JavaScript code to verify the flow of data:

    Ensure Unique Player Identification
    Make sure that the player parameter sent from JavaScript correctly matches the $_SESSION[‘username’] on the server. Verify that each player has a unique identifier and that this identifier is used consistently throughout the application.

    Testing with Different Players Manually test the position updates by logging in as different players and moving their cubes. Check the database entries after each move to confirm that the positions are updated correctly and independently.
    you should be able to identify where the issue lies and ensure that the cube positions are updated correctly for both players.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search