I am working on this prototype click me (updated). The problem i am having is following. You can use (W,A,S,D) or (up,left,down,right) to move your snake (path), the idea is that when you bump on another snake (path intersection) you are eating him (he is loosing his parts (points) and you are getting new parts (points). Removing points is not perfect but it works. This is the code:
var intersections = myPath.getIntersections(testWorm);
for(var i = 0; i < intersections.length; i++) {
testWorm.segments[i].remove();
}
It could use some tweaking but mainly it just works.
The code for attaching new points however does not work. This is the code:
var length = 15; // initial points length
length++; // increase initial points length to meet algorithm
var start = new paper.Point(Math.random()*100,Math.random()*100); // calculate start for new point
myPath.add(new paper.Point(myPath._segments * length + start.x, 0 + start.y)); // add new point
/* it was suppose to fix problem */ myPath.smooth();
/* it was suppose to fix problem */ view.update();
as you can see, in the moment you try to intersect the worm disappers. but there is no console log with error, so i dont understand what is going wrong.
Thank you for all the help ;)
I'm not sure what this part of the code is supposed to do, but I'm pretty sure it will produce NaN
, meaning your point will have an invalid coordinate and can't be drawn: myPath._segments * length
. Also, you shouldn't access 'private' properties (starting with a '_'
), instead, use myPath.segments
.