How can I make a sprite (or image) unable to pass through lines?

So I’ve imported a maze image (png) as the background of my canvas, and drawn lines over the corresponding lines in the image in p5. I’ve created a sprite which is my ‘character’ and it’s just a small image, and all it’s doing at the moment is moving with the arrow keys on top of the maze image. I want this to move through this maze and not go through any of these lines. I’m sure its a really simple answer but I can’t wrap my head around it. Here is my code:

var spr;
var slimeMould;
var maze;
var line1;
var line2;
var line3;
var line4;
var line5;
var line6;
var line7;
var line8;
var line9;
var line10;
var line11;
var line12;
var line13;
var line14;
var line15;
var line16; 
var line17;

function preload () {
    slimeMould = loadImage("assets/slimemould-01.png");
    maze = loadImage("assets/maze-02.png");


function setup() {
  createCanvas(1000, 1000);

  spr = createSprite(595, 100);



function draw() {

  textAlign(CENTER, CENTER);
  text("find the shortest route to the food!", width/2, height*0.79);

    line1 = line(86,168,460,168);
    line2 = line(460,168,460,265);
    line3 = line(460,265,310,265);
    line4 = line(310,265,310,220);
    line5 = line(310,220,380,220);
    line6 = line(86,168,86,730);
    line7 = line(140,168,140,220);
    line8 = line(240,220,240,410);
    line9 = line(140,260,240,260);
    line10 = line(140,260,140,360);
    line11 = line(140,360,190,360);
    line12 = line(190,360,190,330);
    line13 = line(86,410,140,410);
    line14 = line(140,410,140,508);
    line15 = line(86,553,125,553);
    line16 = line(125,553,125,619);
    line17 = line(125,619,260,619);   


function keyPressed() {
  if (keyCode == RIGHT_ARROW) {
    spr.setSpeed(1.75, 0);
  else if (keyCode == DOWN_ARROW) {
    spr.setSpeed(1.75, 90);
  else if (keyCode == LEFT_ARROW) {
    spr.setSpeed(1.75, 180);
  else if (keyCode == UP_ARROW) {
    spr.setSpeed(1.75, 270);
  return false;

function keyReleased() {
    if (keyCode == RIGHT_ARROW) {
      else if (keyCode == DOWN_ARROW) {
  else if (keyCode == LEFT_ARROW) {
  else if (keyCode == UP_ARROW) {
  return false;

it looks like you’re using to create the sprite correct?

you can likely use the setCollider() method to achieve this (although for sprite to line collision it might not be the most performant way)

You might also check out p5.collide2D’s collideLineRect() function

Here’s what’s happening under the hood with that collision detection if you want to write you’re own: . It’s using another function within the library to detect the line/line collisions: So the idea is basically that it’s doing 5 line/line collision detection tests.

1 Like

Thanks! Yeah I am using :slight_smile:
So I used setCollider() and changed my first line to a very thin rectangle:

rect1 = rect(86,160,378,15);

Then I set up my Collider:


but it’s not working :frowning: Do you know what I’m doing wrong?

(my sprite is at (595,100) so I’m not exactly sure if my offsetX, offsetY, width, & height are right)

I found a way! I just tested it using a change of colour to show collision and it works. Now I just need to replace the mouseX and Y with my sprite properties… but I’m not sure what to plug in?


If you post to multiple sites, please link between the posts so we know what help you’ve already received.

This question has also been posted here.