General discussion about LÖVE, Lua, game development, puns, and unicorns.
5 posts • Page 1 of 1
I'm working on a pathfinding routine and am trying to do something similar to raycasting to to have an agent look ahead to the goal and see if it is clear or obstructed. I've successfully been using a matched imagedata to check for object collision using img:getPixel. I'm wondering if there is a way to ":getPixel" along a line (without drawing the line) to get the color data for each point along the line.
If it's a straight line, you can do something like this:
Warning: code is untested.
Code: Select all
function getLine( x1, y1, x2, y2 ) -- Get two points that you know are on the line- your start and end points. -- ( x1, y1 ) should always be the start. -- y = mx + b local m = ( y1 - y2 ) / ( x1 - x2 ) local b = y1 - m * x1 local function sign( x ) return ( x > 0 and 1 ) or ( x < 0 and -1 ) or 0 end local addx = sign( x1 - x2 ) for x = x1, x2, addx do local y = m * x + b -- Put code here. Use 'y' and 'x' as the values. end end
- Party member
- Posts: 3087
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
On the other hand, tables have some overhead in memory, while imagedata is (to my knowledge) just a simple c array holding byte quadruplets for the pixel colors; so that should consume less memory as a tradeoff... if i am rightPlu wrote:Keep in mind that using getPixel constantly is probably (a lot) slower than pulling the whole image into a lua-table and accessing that.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
Users browsing this forum: No registered users and 15 guests