I am currently having a problem with collision, I do not want an object to pass through another of which I understand the basics of. For instance:
nt[] player1;
int[] player2;
int p1width = 50;
int p1height = 50;
int p2width = 50;
int p2height = 50;
int sandbagwidth = 30;
int sandbagheight = 200;
int sand11 =200;
int sand12 = 0;
void setup(){
size(1200, 600);
player1= new int[] {250, 250};
player2= new int[] {930, 250};
}
void draw(){
//draw players
fill(0);
rect(0,0,width/2,height);
fill(255,0,0);
rect(600,0,width/2,height);
//move players
fill(255);
rect(player1[0], player1[1], p1width, p1height);
if (keyPressed) {
switch(keyCode) {
case LEFT:
player1[0] -=10;
break;
case RIGHT:
player1[0] +=10;
break;
case UP:
player1[1] -=10;
break;
case DOWN:
player1[1] +=10;
break;
}
}
if(player1[0]+25>1150){
player1[0]-=25;
}
if(player1[0]-25<0){
player1[0]+=50;
}
if(player1[1]+25>550){
player1[1]-=25;
}
if(player1[1]-25<0){
player1[1]+=50;
}
fill(0,255,0);
rect(sand11, sand12, sandbagwidth, sandbagheight);
if(player1[0] + p1width/2 > sand11 - sandbagwidth/2
&& player1[0] - p1width/2 < sand11 + sandbagwidth/2
&& player1[1] + p1width/2 > sand12 - sandbagheight
&& player1[1] - p1width/2 < sand12 + sandbagheight){
player1[0]-=1;
}
}
This would be good however I don’t want it reverse the direction of the object by one on all sides, that doesn’t stop an object from getting through. So to try to make it so if it hits the right wall for instance it adds 1 direction, I tried splitting it up. But splitting up the collision code makes it move on its own:
So instead of doing reverse direction is there any way I can stop moving object instead? Or could someone lend me a hand on better understanding how to get collision working for all sides
In your current example, what is player1[0]-=1;? Maybe you meant to implement direction inversion, which requires multiplication instead of subtraction: player1[0] *= 1;
Would this example give you an idea of another approach: https://processing.org/examples/bouncybubbles.html This link calculates change of direction using core Physics concepts. You are welcome to explore this an any other examples to better understand previous implementation. If something is not clear, you can try asking here in this post.
player1[0]=-1 is the movement of one space backwards after collision. But -1 is only to the left in direction which is ok if I collide into the left of the block. But if I collide into the right it should go +1 so it bounces to the right, which is effectively not what my code is doing which is why I tried splitting up the collision between the left, right, and bottom walls. Doing player1[0] x= 1 effectively does nothing.
Basically I want to have if collides with left -1 in x direction, if it is right +1 in x direction, if it hits bottom -1 in y direction, etc.
I used this same collision code to pick up objects which I am having no trouble with but picking things up is different then collision, for each side the item is grabbed the result is the same. For each side of collision the result should not be the same. I have other collisions working too including wall collisions but I cannot for the life of me figure out how to get these two objects to collide properly.
i think what the idea was to suggest following structure:
you have a
final float speed =10.0;
float dir = 1 or -1 or 0 like go left, right, stop
so the player movement will be like
player.x += speed*dir;
later you will find that bouncing in an angle requires more, like
speedx and speedy…
may i suggest you search little bit more here for examples ( ok, take the easy ones )
I managed to fix the collision direction in a different way, however as soon as I added a second object on the same axis it is registering the whole axis as a collision. That and holding down the key instead of pressing will glitch you through the object eventually: