Luke B
Luke B

Reputation: 2121

How to create the vision of a 2D camera looking at a 2D world

In a 3D world, a camera looking out will create a 2D representation of what it sees, AKA a picture. Looking at a 3D cube would produce this 2D image:

Cube image

However, inside a 2D world, a camera looking out will create a 1D line of pixels that will represent what it sees.

The camera in this scene:

Camera in Scene

Would produce this 1D "image": 1D picture

Think of the camera looking out. It would be unable to see all of the pink shape, because most of it is obstructed be the red shape. It would only see the part that is unobstructed. Also, objects further away appear smaller.

How can I create the view of a 2D camera looking out at a 2D world, creating a 1D image?

I am looking for a method, preferably in Python, to accomplish this. I am trying to do this for some 2D creatures I am simulating. I want their input to be 1D array that represents their view of the world.

Upvotes: 0

Views: 317

Answers (1)

gilch
gilch

Reputation: 11641

Pick a focal point at the creature. Draw a circle around it. Subdivide the circle for each pixel in your image resolution, and cast a ray from the focal point to the circle point and out into the world. Find the points of intersection between that ray and the world objects. Get the color of the closest one to set the color in the image at that pixel. Repeat for each pixel.

This gives a 360-degree perspective view, which might simplify things for a simulated creature. If you want a more directional view, just use an arc instead of a circle. 45 degrees seems reasonable.

Upvotes: 1

Related Questions