Saltearse al contenido

3D Point Renderer Camera Perspective Issues

Esta página aún no está disponible en tu idioma.

💡 Struggling with 3D graphics and camera systems? Need help with complex mathematical projections? 🚀 Get Expert Help

TA

TechBuilder_Alex

Posted on July 22, 2025 • Advanced

🎯 3D Point Renderer Perspective Problem

Hey everyone! I’m working on a 3D point renderer in Scratch and I’m running into a weird perspective issue. When I change the camera’s Z-axis position, the points do get smaller and larger as expected, but there’s a major problem with the positioning:

  • When points get smaller (should be farther away), they should get closer together
  • When points get bigger (should be closer), they should spread farther apart
  • Instead, I’m getting the opposite effect! 😵

I’ve been staring at this code for hours and can’t figure out what’s wrong. The size changes work, but the positioning is completely backwards. Anyone know what might be causing this?

Here’s my project: https://scratch.mit.edu/projects/1200089648/

3D

Graphics_Wizard

Replied 5 hours later • ⭐ Best Answer

@TechBuilder_Alex I see exactly what’s happening! This is a classic 3D projection issue. The problem is in how you’re handling the camera space calculations. Let me break down the correct approach:

🔍 Understanding 3D Projection

The key to proper 3D rendering is understanding the difference between world space and camera space:

flowchart TD A[🌍 World Space Points] --> B[Calculate Camera Space] B --> C[Apply Projection Formula] C --> D[📺 Screen Space Points] B1[Point Position - Camera Position] --> B C1[Divide X,Y by Z depth] --> C C2[Scale by Field of View] --> C style A fill:#e1f5fe style B fill:#f3e5f5 style C fill:#e8f5e8 style D fill:#fff3e0

🧮 The Correct Projection Formula

Here’s the proper way to handle 3D point projection:

    // Step 1: Convert to camera space
set [camera x v] to ((point x) - (camera position x))
set [camera y v] to ((point y) - (camera position y))
set [camera z v] to ((point z) - (camera position z))

// Step 2: Check if point is in front of camera
if <(camera z) > [0]> then
// Step 3: Apply perspective projection
set [screen x v] to (((camera x) / (camera z)) * (scale factor))
set [screen y v] to (((camera y) / (camera z)) * (scale factor))

// Step 4: Set point size based on distance
set [point size v] to (((base size) / (camera z)) * (scale factor))

// Step 5: Draw the point
go to x: (screen x) y: (screen y)
set pen size to (point size)
pen down
pen up
end
  

🔧 Key Points to Remember

  • Camera Space: Always subtract camera position from point position first
  • Division by Z: This creates the perspective effect - closer objects (smaller Z) appear larger
  • Scale Factor: Controls field of view - start with 100-200 and adjust
  • Z > 0 Check: Prevents rendering points behind the camera

🎯 Advanced Camera System

For a more robust camera system, you can add rotation support:

    // Camera rotation (optional)
// Rotate around Y-axis (left/right)
set [rotated x v] to (((camera x) * ([cos v] of (camera rotation y))) + ((camera z) * ([sin v] of (camera rotation y))))
set [rotated z v] to (((camera z) * ([cos v] of (camera rotation y))) - ((camera x) * ([sin v] of (camera rotation y))))

// Use rotated values for projection
set [screen x v] to (((rotated x) / (rotated z)) * (scale factor))
  

The issue you were experiencing happens when you don’t properly convert to camera space first. Without this step, the projection formula works backwards! 🔄

TA

TechBuilder_Alex

Replied 2 hours later

@Graphics_Wizard THANK YOU SO MUCH! 🎉 That was exactly the problem!

I was trying to do the projection directly on world coordinates instead of converting to camera space first. Once I added the camera position subtraction, everything works perfectly now. The perspective looks amazing!

The explanation about world space vs camera space really helped me understand what was going wrong. This is going to make my 3D engine so much better! 🚀

MG

MathGuru_Sam

Replied 1 day later

Great solution @Graphics_Wizard! 👏 For anyone wanting to dive deeper into the math, here’s why this works:

The perspective projection formula (x/z, y/z) simulates how our eyes see the world. Objects farther away (larger Z) get divided by a bigger number, making them appear smaller and closer together. It’s the same principle cameras use!

Pro tip: You can also add depth sorting by storing the Z values and drawing points from back to front for proper layering. 🎨

VB

Vibelf_Community

Pinned Message • Moderator

🚀 Master Advanced 3D Graphics in Scratch!

Excellent discussion on 3D projection! For those ready to take their 3D graphics skills to the next level, our expert tutors can help you implement:

  • 🎮 Complete 3D game engines
  • 🌟 Advanced lighting and shading
  • 🎯 3D collision detection
  • 🎨 Texture mapping and materials

📚 Related 3D Graphics Topics

Ready to create stunning 3D graphics? Get personalized guidance from our 3D programming experts!