[Problem] Perspective mapping

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
lauriszz123
Prole
Posts: 11
Joined: Thu Oct 04, 2018 1:01 pm

[Problem] Perspective mapping

Post by lauriszz123 »

Im developing a neural network game, and I have a camera that projects all of the objects from the scene, based if it is in range, and im putting it into a 16x16 image, problem is that i have no clue how to move the objects from my line if sight which is the direction i am facing.

The code below works by mapping the objects in the center, giving them a size based of the distance from the player and the object. But i dont want to see them in the canvas if they are out of my sight, any help?

Code: Select all

function vision(playerPosition, playerDirection)
	local imageWidth = 16
	local imageHeight = 16
	local viewRadius = 10
	local visibleRange = 128

	local size = vec2.new(imageWidth/2, imageHeight/2)

	local canvas = love.graphics.newCanvas(imageWidth, imageHeight)

	local width = love.graphics.getWidth()
	local height = love.graphics.getHeight()

	local mappedPlayerPosition = vec2.new( math.map( playerPosition.x, 0, width, 0, imageWidth, true ), math.map( playerPosition.y, 0, height, 0, imageHeight, true ) )

	love.graphics.setCanvas( canvas )
	
	local visibleObjects = {}

	for _, object in mob.get():pairs() do
		local mappedObjectPosition = vec2.new( math.map( object.position.x, 0, width, 0, imageWidth, true ), math.map( object.position.y, 0, height, 0, imageHeight, true ) )

		local displacement = mappedObjectPosition - mappedPlayerPosition
		local displacementDistance = displacement:magnitude()
		local distance = (object.position - playerPosition):magnitude()

		local objectAngle = math.atan2(displacement.y, displacement.x)
		local angleDifference = objectAngle - playerDirection

		-- Check if the object is within the visible range and angle of the player
		if distance <= visibleRange and angleDifference >= viewRadius / 4 then
			local size = (1 / distance) * (imageWidth * imageHeight)
			local color = 1 / displacementDistance
			table.insert( visibleObjects, {
				x = (displacement.x + imageWidth / 2) - playerDirection,
				y = displacement.y + imageHeight / 2,
				size = size,
				color = color,
			} )
		end
	end

	table.sort( visibleObjects, sorter )

	for i=1, #visibleObjects do
		local object = visibleObjects[ i ]
		love.graphics.setColor( object.color, object.color, object.color, 1 )
		love.graphics.circle( "fill", object.x, object.y, object.size )
	end

	love.graphics.setCanvas()

	return canvas
end
Image
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 52 guests