0

I'm just trying to create a small 2d game where we control a character that can place block on the "map" with the mouse and jump on it to climb etc. It's just a test to practice.

I've achieved to make the character stops when he walk into blocks.

But, i'm facing a problem when I want my character to jump on a block, i'm not able to make him land on it. I can't find the condition to. Can someone helps me on it ? The code that handle the collision between the character and blocks is in the draw function and in the for.

Controls

  • Left: Q
  • Right: D
  • Jump: Space Bar
  • Place a block: Mouse Click
  • Remove a block: Mouse Click

Edit: You might have ask yourself why i'm using some variables that are not declared anywhere. It's because they are given by P5.js. (ex: width)

function Man() {
    this.w = 20;
    this.h = 40;
    this.x = width/2;
    this.y = height - this.h;
    this.canJump = true;

    this.velocity = 0;

    this.show = function(){
        fill(255);
        rect(this.x, this.y, this.w, this.h);
    }

    this.update = function(){
        this.velocity += gravity;
        this.y += this.velocity;

        if(this.y + this.h  > height) {
            this.y = height - this.h ;
            this.velocity = 0;
            this.canJump = true;
        }

        if(this.x <= 0) {
            this.x = 0;
        }

        if(this.x + this.w >= width) {
            this.x = width - this.w;
        }
    }

    this.jump = function(){
        if(this.canJump) {
            man.velocity -= 6;
            this.canJump = false;
        }
    }
}


var man;
var ground;

var gravity = 0.4;

function setup(){
    frameRate(60);
    createCanvas(640, 480);

    man = new Man();
}

var tilesSize = 20;
var tiles = [];

for(var i = 0; i < 640; i+= tilesSize) {
    for(var j = 0; j < 480; j+= tilesSize) {
        tiles.push({x:i, y:j, empty: true});
    }
}

function draw(){
    background(0);
    noStroke();

    push();
    for(var i = 0; i < tiles.length; i++) {
        if(!tiles[i].empty) {
            fill(150);
            rect(tiles[i].x, tiles[i].y, tilesSize, tilesSize);
        }
        if(mouseX >= tiles[i].x && mouseX < tiles[i].x + tilesSize && mouseY >= tiles[i].y && mouseY < tiles[i].y + tilesSize) {
            if(tiles[i].empty) {
                fill(50);
                rect(tiles[i].x, tiles[i].y, tilesSize, tilesSize);
            }else {
                fill(255,0,0);
                rect(tiles[i].x, tiles[i].y, tilesSize, tilesSize);
            }
        }

        // HERE IS CONDITIONS FOR COLLISION WITH BLOCK
        if(!tiles[i].empty) { 

            if(tiles[i].x + tilesSize >= man.x && tiles[i].x < man.x && tiles[i].y < man.y + man.h ) {      
                man.x = tiles[i].x + tilesSize;    
            }
            if(man.x + man.w >= tiles[i].x && man.x <= tiles[i].x && tiles[i].y < man.y + man.h) {
                man.x = tiles[i].x - man.w;
            }
        }
    }
    pop();

    man.show();
    man.update();
    
    moveMan(); 
}

function moveMan(){
    if(keyIsDown(81)) {
        man.x -= 2;
    }
    if(keyIsDown(68)) {
        man.x += 2;
    }
}

function keyPressed() {
    if(keyIsDown(32)) {
       man.jump();
    }
}

function mouseClicked() {
    for(var i = 0; i < tiles.length; i++) {
        if(mouseX >= tiles[i].x && mouseX < tiles[i].x + tilesSize && mouseY >= tiles[i].y && mouseY < tiles[i].y + tilesSize) {
            if(tiles[i].empty) {
                tiles[i].empty = false;
            }else {
                tiles[i].empty = true;
            }
        }
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.min.js"></script>
ggorlen
  • 44,755
  • 7
  • 76
  • 106
Alexis Vandepitte
  • 2,077
  • 2
  • 12
  • 28
  • You are more likely to get responses if you change the title of your question from what you want your game to do to what specific programming issue you need help with. – Scott Marcus Oct 21 '18 at 17:17
  • @ScottMarcus Sorry, it's was not easy to explain the issue with few words. – Alexis Vandepitte Oct 21 '18 at 17:22
  • The title still describes what you want your game to do. A better title might be "Need help detecting when one UI element overlaps another" - - that's what you need to do. Once you can detect when your character's UI overlaps the block, you can stop the character's movement, thus keeping it on the block. The more specific you can be, the better chance at getting responses and accurate ones at that. – Scott Marcus Oct 21 '18 at 17:31
  • This is what i don't know how to do. You propose me a title that is not what i need to do, the fact here is that i need to find a condition that makes me able to land a character on a block and not to go through it. An overlapping function will only tells me if there is a collision between two element. Here i need to know the direction or something that i don't know, but not just the overlapping. – Alexis Vandepitte Oct 21 '18 at 17:44
  • Before you can land on something, you have to begin to occupy the space that that something is located in. Your first step is to be able to detect when two objects overlap. Is that the complete problem? No. I'm simply telling you that you will get better responses when you can accurately describe the ***programming problem*** you are having, not the ***software requirements*** you wish to achieve. – Scott Marcus Oct 21 '18 at 17:48
  • Stack Overflow is a sentence formulation help website or a coding help website ? My programming problem is the `Condition to land a character on a block after a jump on it`. If i'm asking this question it's because i don't know what is the problem. So i don't know if it's an overlapping problem, i don't know if it's a condition problem, i don't know. I just don't know. Now you gave me clues while you criticize my title, so I know a little more and I will continue to search. – Alexis Vandepitte Oct 21 '18 at 18:02
  • 1
    I'm not criticizing you at all. I'm trying to help you. I've told you what the ***programming problem*** that you should be trying to solve first is. `Condition to land a character on a block after a jump on it` is not a ***programming problem***, it's a software requirement. I am really sorry if you don't understand that, but the fact that you don't is really the basis for you not understanding what I'm trying to tell you. – Scott Marcus Oct 21 '18 at 18:10
  • Related: [Collision Handling in p5.js](https://stackoverflow.com/questions/69773503/collision-handling-in-p5-js) – ggorlen Dec 24 '22 at 23:49

0 Answers0