Strange Problem

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Dresden
Prole
Posts: 9
Joined: Sun Sep 26, 2010 9:36 pm

Strange Problem

Post by Dresden »

Using the LOVE engine I am trying to draw an oblique projection grid, that will be able to be "Shaped" and shaded based on height. For this I will need four triangles per square and am calculating the approximate midpoint of the grid to connect the shading triangles. That is not the problem however, the problem seems to be part of the way I am implementing Lua, this is my first time using it and I'm not really sure of the entirely correct way to do things. I think the problem is in how I'm initializing the function "getMidPoint" since the first few times it is called, it returns (0, 0) and afterwards it works properly as it should sortof. As you can see in the picture, the triangle's third point is (0,0) for all of the squares and then in the console "getMidPoint" is called three times then the fourth time it works (See [207.5, 85] this is the midpoint in pixels of grid point [1, 1]). Anyways, I've hit a huge wall and I'm not sure why. Here is all my initializing code:

Code: Select all

mainfont = love.graphics.newFont("ahronbd.ttf",14)
winCenter = love.graphics.getWidth()/2
tileSize = 20
grid_size = 10 -- Refers to number of points, for tiles use: tiles = grid_size - 1. Note that in order to center a character it would be best to have an odd number of tiles.
grid_x = love.graphics.getHeight() - 400
rectX = 20
grid_y = 150
gridData = {}
mouseToGrid = {}

function love.load()
    --[[music = love.audio.newSource("music.s3m","stream")
    music:setLooping(true)
    music:setVolume(0.3)
    love.audio.play(music)]]--
    
    love.mouse.setVisible(false)
    print("Program launched.\n  Console is active so that it may potentially be used later to\n  monitor networking.")
    for gridX = 1,grid_size do
       gridData[gridX] = {}
       for gridY = 1,grid_size do
          gridData[gridX][gridY] = { ["X"] = 0,
                ["Y"] = 0,
                ["midX"] = 0,
                ["midY"] = 0,
                ["Z"] = 0
                }
       end
    end
    print(getMidPoint(1,1))
    mouseToGrid["X"] = 0
    mouseToGrid["Y"] = 0
    gridLoad()
    
end

function gridLoad()
    print(getMidPoint(1,1))
    for x = 1,grid_size do
        print(getMidPoint(1,1))
        for y = 1,grid_size do
            --if x < grid_size and y < grid_size then
                gridData[x][y]["X"] = ((grid_x + (tileSize*(x-1)) + (((grid_size * tileSize) - (tileSize*(y-1)))/2)) * 1.5) - 20
                gridData[x][y]["Y"] = grid_y + (tileSize*(y-1))
                
            if x < grid_size and y < grid_size then
                gridData[x][y]["midX"],gridData[x][y]["midY"] = getMidPoint(x,y)
                print(string.format("midX: %d midY: %d",x,y))
                
            end
            --print(string.format("midX: %d midY: %d",x,y))
        end
    end
end

function getMidPoint(x,y) --Will calculate with regard to grid, top left corner, or when transformed isometrically, top corner.
    --if x < grid_size and y < grid_size then
        bigX = gridData[x+1][y]["X"]-- WHY IN THE FUCK??
        bigY = gridData[x][y+1]["Y"]
        smallX = gridData[x][y]["X"]
        smallY = gridData[x][y]["Y"]
        if gridData[x+1][y+1]["X"] > bigX then
            bigX = gridData[x+1][y+1]["X"]
        end
        if gridData[x+1][y+1]["Y"] > bigY then
            bigY = gridData[x+1][y+1]["Y"]
        end
        if gridData[x][y+1]["X"] < smallX then
            smallX = gridData[x][y+1]["X"]
        end
        if gridData[x+1][y]["Y"] < smallY then
            smallY = gridData[x+1][y]["Y"]
        end
    --end
    return smallX + ((bigX - smallX) / 2), smallY + ((bigY - smallY) / 2)
end
Image here
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Strange Problem

Post by Robin »

I'm sorry, both from your post and your code it is rather unclear what it does and what it is supposed to do. The only thing I could think of is that it might call getMidPoint the first few times before it initialized the data?
Help us help you: attach a .love.
User avatar
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Location: In your head.
Contact:

Re: Strange Problem

Post by zac352 »

Dresden wrote:Using the LOVE engine I am trying to draw an oblique projection grid, that will be able to be "Shaped" and shaded based on height. For this I will need four triangles per square and am calculating the approximate midpoint of the grid to connect the shading triangles. That is not the problem however, the problem seems to be part of the way I am implementing Lua, this is my first time using it and I'm not really sure of the entirely correct way to do things. I think the problem is in how I'm initializing the function "getMidPoint" since the first few times it is called, it returns (0, 0) and afterwards it works properly as it should sortof. As you can see in the picture, the triangle's third point is (0,0) for all of the squares and then in the console "getMidPoint" is called three times then the fourth time it works (See [207.5, 85] this is the midpoint in pixels of grid point [1, 1]). Anyways, I've hit a huge wall and I'm not sure why. Here is all my initializing code:

Code: Select all

mainfont = love.graphics.newFont("ahronbd.ttf",14)
winCenter = love.graphics.getWidth()/2
tileSize = 20
grid_size = 10 -- Refers to number of points, for tiles use: tiles = grid_size - 1. Note that in order to center a character it would be best to have an odd number of tiles.
grid_x = love.graphics.getHeight() - 400
rectX = 20
grid_y = 150
gridData = {}
mouseToGrid = {}

function love.load()
    --[[music = love.audio.newSource("music.s3m","stream")
    music:setLooping(true)
    music:setVolume(0.3)
    love.audio.play(music)]]--
    
    love.mouse.setVisible(false)
    print("Program launched.\n  Console is active so that it may potentially be used later to\n  monitor networking.")
    for gridX = 1,grid_size do
       gridData[gridX] = {}
       for gridY = 1,grid_size do
          gridData[gridX][gridY] = { ["X"] = 0,
                ["Y"] = 0,
                ["midX"] = 0,
                ["midY"] = 0,
                ["Z"] = 0
                }
       end
    end
    print(getMidPoint(1,1))
    mouseToGrid["X"] = 0
    mouseToGrid["Y"] = 0
    gridLoad()
    
end

function gridLoad()
    print(getMidPoint(1,1))
    for x = 1,grid_size do
        print(getMidPoint(1,1))
        for y = 1,grid_size do
            --if x < grid_size and y < grid_size then
                gridData[x][y]["X"] = ((grid_x + (tileSize*(x-1)) + (((grid_size * tileSize) - (tileSize*(y-1)))/2)) * 1.5) - 20
                gridData[x][y]["Y"] = grid_y + (tileSize*(y-1))
                
            if x < grid_size and y < grid_size then
                gridData[x][y]["midX"],gridData[x][y]["midY"] = getMidPoint(x,y)
                print(string.format("midX: %d midY: %d",x,y))
                
            end
            --print(string.format("midX: %d midY: %d",x,y))
        end
    end
end

function getMidPoint(x,y) --Will calculate with regard to grid, top left corner, or when transformed isometrically, top corner.
    --if x < grid_size and y < grid_size then
        bigX = gridData[x+1][y]["X"]-- WHY IN THE FUCK??
        bigY = gridData[x][y+1]["Y"]
        smallX = gridData[x][y]["X"]
        smallY = gridData[x][y]["Y"]
        if gridData[x+1][y+1]["X"] > bigX then
            bigX = gridData[x+1][y+1]["X"]
        end
        if gridData[x+1][y+1]["Y"] > bigY then
            bigY = gridData[x+1][y+1]["Y"]
        end
        if gridData[x][y+1]["X"] < smallX then
            smallX = gridData[x][y+1]["X"]
        end
        if gridData[x+1][y]["Y"] < smallY then
            smallY = gridData[x+1][y]["Y"]
        end
    --end
    return smallX + ((bigX - smallX) / 2), smallY + ((bigY - smallY) / 2)
end
Image here
If you're trying to get the middle of the square,

Code: Select all

function getMidPoint(x1,y1,x2,y2,x3,y3,x4,y4)
local x=x1+x2+x3+x4
local y=y1+y2+y3+y4
return x/4,y/4
end
Hello, I am not dead.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 5 guests