A real-time multiplayer 2D physics game built with Node.js, Socket.io, and Matter.js. Players control UFOs to help marbles navigate through physics-based levels while Twitch chat emotes spawn as interactive objects.
- Multiplayer UFO Control: Players control UFOs using WASD keys
- Tractor Beam Mechanics: Use SPACEBAR to activate beam and move objects
- Physics-Based Marble: Guide marbles through levels using Matter.js physics
- Goal-Based Levels: Help marbles reach the finish line to progress
- OAuth Login: Players log in with their Twitch accounts
- Live Chat Integration: Emotes from Twitch chat spawn as physics objects
- Real-time Emote Spawning: Chat emotes roll around and interact with the game world
- Visual Level Designer: Drag-and-drop interface for creating levels
- Physics Properties: Configure friction, restitution, and static/dynamic objects
- Special Objects: Set spawnpoints and goals for marbles
- Save/Load System: Levels saved as JSON files in the
levels/directory
- Real-time Synchronization: All players see the same game state
- Player Identification: Each player has a unique colored UFO
- Chat System: In-game chat for player communication
- XP/Level System: Players gain experience and level up
- Node.js (v14 or higher)
- npm or yarn
- Twitch Developer Account (for OAuth)
-
Clone and Install
git clone <repository-url> cd MarbleWS npm install
-
Configure Twitch Integration
- Create a Twitch application at https://dev.twitch.tv/console
- Update
.envfile with your credentials:
# Twitch API Configuration TWITCH_CLIENT_ID=your_twitch_client_id_here TWITCH_CLIENT_SECRET=your_twitch_client_secret_here TWITCH_REDIRECT_URI=http://localhost:3000/auth/twitch/callback/ TWITCH_CHANNEL=channelname # Twitch Bot Configuration TWITCH_BOT_USERNAME=botusername TWITCH_BOT_OAUTH_TOKEN=oauth:00000000000 # Server Configuration PORT=3000 DEV_MODE=true BASE_PATH= ALLOWED_ORIGINS=https://example.com,http://localhost:3000,http://127.0.0.1:3000 # Admin Panel Configuration ADMIN_USERNAME=admin ADMIN_PASSWORD=change_this_password
-
Start the Server
# Development mode (with auto-restart) npm run dev # Production mode npm start
-
Access the Game
- Game: http://localhost:3000
- Level Editor: http://localhost:3000/editor
- Admin panel: http://localhost:3000/admin
MarbleWS/
โโโ server/
โ โโโ index.js # Main server file
โ โโโ gameLogic.js # Game state and physics management
โ โโโ twitchChat.js # Twitch chat integration
โ โโโ socketHandlers.js # Socket.io event handlers
โโโ client/
โ โโโ index.html # Main game page
โ โโโ editor.html # Level editor page
โ โโโ css/
โ โ โโโ styles.css # Game and editor styles
โ โโโ js/
โ โโโ game.js # Main game logic
โ โโโ networking.js # Socket.io client
โ โโโ renderer.js # Canvas rendering
โ โโโ controls.js # Input handling
โ โโโ editor.js # Level editor logic
โโโ levels/ # Saved level JSON files
โ โโโ sample-level.json # Example level
โโโ package.json
โโโ .env # Environment variables
โโโ .gitignore
- Login: Click "Login with Twitch" to authenticate
- Movement: Use WASD keys to move your UFO
- Tractor Beam: Hold SPACEBAR and click to lift objects
- Objective: Help the red marble reach the yellow goal area
- Teamwork: Work with other players to solve physics puzzles
- Setup Twitch Integration: Configure your channel in
.env - Create Levels: Use the level editor to design custom challenges
- Engage Viewers: Chat emotes automatically spawn in the game
- Moderate: Control emote spawn rate (2-second cooldown)
- Access Editor: Visit http://localhost:3000/editor
- Select Tools: Choose Rectangle, Circle, or Select tool
- Place Objects: Click on canvas to create objects
- Set Properties: Configure physics properties in sidebar
- Special Objects: Mark objects as spawnpoints or goals
- Save Level: Give your level a name and click Save
- Static: Objects that don't move (platforms, walls)
- Dynamic: Objects players can move with tractor beams
- Friction: How slippery the surface is (0-2)
- Restitution: How bouncy the object is (0-2)
GET /api/levels- List all available levelsGET /api/levels/:name- Get specific level dataPOST /api/levels/:name- Save level data
GET /auth/twitch- Initiate Twitch OAuth flowGET /auth/twitch/callback- Handle OAuth callback
login- Player authenticationplayerMove- Player position updatesbeamToggle- Tractor beam activationbeamInteraction- Beam target interactionloadLevel- Request level changechatMessage- Send chat message
loginSuccess- Authentication confirmationgameState- Initial game stategameStateUpdate- Real-time state updatesplayerJoined/Left- Player connection eventslevelLoaded- Level change notificationchatMessage- Broadcast chat messages
- Matter.js: Handles all physics simulation
- 60 FPS: Physics updates at 60 frames per second
- Gravity: Configurable world gravity (default: 0.8)
- Collision Detection: Automatic collision handling
- Socket.io: Real-time bidirectional communication
- Server Authority: Physics simulation runs on server
- Client Prediction: Smooth UFO movement with prediction
- State Synchronization: 10 FPS game state broadcasts
- tmi.js: Twitch chat client library
- Anonymous Connection: No bot account required
- Emote Parsing: Automatic emote detection and URL generation
- Rate Limiting: 2-second cooldown between emote spawns
- Server Logic: Add to appropriate server module
- Client Logic: Update corresponding client module
- Networking: Add socket events if needed
- UI: Update HTML/CSS as required
- Server Logs: Check console for server-side issues
- Client Console: Use browser dev tools for client debugging
- Network Tab: Monitor Socket.io connections
- Physics Debug: Uncomment debug rendering in game.js
- Twitch Login Fails: Check client ID/secret in
.env - Chat Not Working: Verify channel name in
.env - Physics Lag: Reduce number of objects or lower update rate
- Connection Issues: Check firewall settings for port 3000
- Limit Objects: Keep level objects under 50 for best performance
- Optimize Images: Use small emote images (28x28px recommended)
- Clean Up: Old chat messages auto-delete after 50 messages
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is open source and published under the MIT License.
- Matter.js: Physics engine
- Socket.io: Real-time communication
- tmi.js: Twitch chat integration
- Express.js: Web server framework
Have fun building and playing MarbleWS! ๐ฎโจ