person
Very well I will make my own original approach
So first you’re going to want to set up an array with some nested arrays inside of it. Next, you need to make a function to define blocks that the player will collide with.
For collisions with rectangles, you can use some trig to find the outline of a rectangle. However, you can get around this by setting the background to white after defining each block, drawing the block once, and using two 400x for loops to loop through each x and y value. Also, you’ll need another set of nested arrays to record if a spot is rectangle or background after you use get.
It’ll look something like this:
function defineBlock(x,y,blahblahblah){
background(255,255,255);
var b = new block(x,y,blahblahblah);
drawBlock(b);
checkOutline();
}
function checkOutline(){
var arr = [];
for(var i=0; i<400; i++){
arr.push([]);
for(var q=0; q<400; q++){
arr[i].push([]);
}
}
for(var i=0; i<400; i++){
for(var q=0; q<400; q++){
if(get(q,i) === rgb(255,255,255)){
arr[q][i] = false;
}else{
arr[q][i] = true;
}
}
}
}
Now, the only problem with this is if the block is more than 400 wide or more than 400 tall. To solve this we'll use a while loop to keep repeating this sequence of checking the outline of the block until none of the outline is on the side.
It should look something like this:
var touchingEdge = false;
while(touchingEdge === true){
touchingEdge = false;
push();
img = get();
translate(random(-100000,100000),random(-100000,100000));
scale(random(0,1));
image(img,0,0);
checkOutline();
pop();
for(var i = 0; i < arr.length; i++){
if(arr[ i ][0] arr[ i ][arr[ i ].length-1]){
touchingEdge = true;
}
}
for(var i = 0; i < arr[0].length; i++){
if(arr[0][ i ] || arr[arr.length-1][ i ]){
touchingEdge = true;
}
}
}
Now, the only thing that could possibly go wrong with this is the random() not moving the screen enough in the event you have a really big block.To solve this, you can change random(-100000,100000);
to random(-100000000,100000000);
or whatever you think is big enough to fit all of your rectangles
Ok now, onto collisions.
You should now have a 400x400 array containing a bunch of true's and false's. To actually utilize this array, we're going to utilize a little thingy I like to call the utilizer. Here's how it works:
var array = [/* un-utilized gobbledygook in here */];
function utilize(e){
var plusPlusExp = new RegExp("__env__\\.InfiniteLoopCount\\+\\+;\\n", "g"), ifClauseExp = new RegExp("\\n\\s*if \\(__env__\\.InfiniteLoopCount > 1000\\) {[\\s]+__env__\\.InfiniteLoopProtect\\('[^']*'\\);[^}]+}", "g"),newExp = new RegExp("__env__\\.PJSCodeInjector\\.applyInstance\\((\\S+), '\\S+'\\)", "g"),envExp = new RegExp("__env__\\.", "g"),noBreakSpace = "\u00a0";return [];
}
array = utilize(array);
It always works, no matter the circumstance.
Ok now, time to apply this stuff in order to answer your question. In order to prevent the player from jumping in mid air, you need to first disable all jumping while the player isn't touching any part of a block.
To do this we need to calculate all possible trajectories that could be created by player input. To do this, all we need to do is have the player input all possible trajectories. Then, once you have all of those trajectories, go through and determine in which ones the player should be able to jump and in which ones the player shouldn't.
I know, now that' i've explained it, it's all so simple.
No need to thank me, I do stuff like this all the time. It's like second nature to me.