Code: Select all
Error
main.lua:104: attempt to index local 'vn' (a nil value)
Traceback
main.lua:104: in function 'polyPoint'
main.lua:41: in function 'update'
[C]: in function 'xpcall'
Code: Select all
--love.load()
v = {}
v[1] = {x = 200,y = 100}
v[2] = {x = 400,y = 130}
v[3] = {x = 350,y = 300}
v[4] = {x = 250,y = 300}
--test is a string that outputs "collision" if the collision is true
test = ""
I'm also aware that this can return nil so I make an if statement to stop it from checking nil
Code: Select all
--love.update()
local x = love.mouse.getX()
local y = love.mouse.getY()
mouse.x,mouse.y = push:toGame(x,y)
if not(mouse.x == nil) and not(mouse.y == nil) then
if polyPoint(v,mouse.x,mouse.y) then
test = "collision"
else
test = ""
end
else
test = ""
end
Code: Select all
--love.draw()
love.graphics.polygon("fill",v[1].x,v[1].y,v[2].x,v[2].y,v[3].x,v[3].y,v[4].x,v[4].y)
love.graphics.print(""..test,0,0)
Code: Select all
function polyPoint(vertices,px,py)
local collision = false
local next = 1
for current = 1, #vertices do
next = current + 1
if (next == #vertices) then
next = 1
end
local vc = vertices[current]
local vn = vertices[next]
if(((vc.y >= py) or (vc.y < py and vn.y >= py)) and
(px < (vn.x - vc.x)*(py - vc.y)/(vn.y - vc.y)+vc.x)) then
collision = not(collision)
end
end
return collision
end
http://www.jeffreythompson.org/collisio ... point.php
If you don't want to go to the site here is the original code. Its in C++. I converted it into Lua or at least tried to.
Code: Select all
float px = 0; // point position
float py = 0;
// array of PVectors, one for each vertex in the polygon
PVector[] vertices = new PVector[4];
void setup() {
size(600,400);
noCursor();
strokeWeight(5); // make the point easier to see
// set position of the vertices
// here we draw a distorted trapezoid, but
// you could make much more complex shapes
// or even randomize the points!
vertices[0] = new PVector(200,100);
vertices[1] = new PVector(400,130);
vertices[2] = new PVector(350,300);
vertices[3] = new PVector(250,300);
}
void draw() {
background(255);
// update point to mouse coordinates
px = mouseX;
py = mouseY;
// check for collision
// if hit, change fill color
boolean hit = polyPoint(vertices, px,py);
if (hit) fill(255,150,0);
else fill(0,150,255);
// draw the polygon using beginShape()
noStroke();
beginShape();
for (PVector v : vertices) {
vertex(v.x, v.y);
}
endShape();
// draw the point
stroke(0, 150);
point(px,py);
}
// POLYGON/POINT
boolean polyPoint(PVector[] vertices, float px, float py) {
boolean collision = false;
// go through each of the vertices, plus
// the next vertex in the list
int next = 0;
for (int current=0; current<vertices.length; current++) {
// get next vertex in list
// if we've hit the end, wrap around to 0
next = current+1;
if (next == vertices.length) next = 0;
// get the PVectors at our current position
// this makes our if statement a little cleaner
PVector vc = vertices[current]; // c for "current"
PVector vn = vertices[next]; // n for "next"
// compare position, flip 'collision' variable
// back and forth
if (((vc.y >= py && vn.y < py) || (vc.y < py && vn.y >= py)) &&
(px < (vn.x-vc.x)*(py-vc.y) / (vn.y-vc.y)+vc.x)) {
collision = !collision;
}
}
return collision;
}