Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: collision between player and platform

1. ## collision between player and platform

my main goal to:
make player jump on platform.

(goal-1).if player jump on platform than player should stand on platform.

(goal-2).if player is below the platform than he jump. and if his head hits the platform than player should not go though it. it should bonce back down.

(goal-3). if player walk right or left of platform than player should stop.

some variabble information:
```x = platform x postion
y = platform y postion
width = platform width
height = platform width
playerX = player x postion
playerY = player y postion
playerW = player width
playerH = player height
p.setY() = set player y postion```

this code below create a collision around platform.
```if(playerX + playerW >= x && playerX <= x + width)
{
if(playerY+playerH >= y && playerY <= y+height)
{
}
}```

(goal-1) done below
now if player jump on platform than iam setting setting player y postion so that player is on platform.

```if(playerX + playerW >= x && playerX <= x + width)
{
if(playerY+playerH >= y && playerY <= y+height)
{
p.setJUMP(false);
p.setY(y - playerH);
}
}```

now i need help with goal-2,3:

platform.java
```public class Platform
{
private int dx;     //plat form speed
private int x;      //x postion
private int y;      //y postion
private int width;  //width
private int height;  //height

Image platform_image;
float frame = 0; //start from 0 frame-total 3 frame-012

public Platform(int x, int y)
{
this.x = x;
this.y = y;
width = 120;
height = 40;
dx = 1;
platform_image = Sprite_Sheet.platform_sheet;
}

/*** for player collsion - name stay same ***/
public Rectangle getBounds()
{ return  new Rectangle(x, y, width,height);}

public int getWIDTH()
{
return width;
}

public void PLATFORM_ANIMATION()
{
int tester = (int)(frame + .1); //.1 slow down
if(tester < 3)
{
frame += .1;
}
else
{
frame = 0;
}
}

//update method
public void PLATFORM_COLLSION(Main m, Player p)
{

x -= dx;  //move platform left

//loop back same platform left to right
if(x < 0 - width)
{
Random r = new Random();
x = m.getWidth() + r.nextInt(300);
y = m.getHeight() - r.nextInt(400);
}
checkForCollision(p); //in update bc this is in loop
}/*** end of platform_collsion ***/

private void checkForCollision(Player p) //we control Player class
{
int playerX = p.getX();
int playerY = p.getY();
int playerW = p.getWIDTH();
int playerH = p.getHEIGHT();

/* not a good idea but works fine
if(p.getBounds().intersects(this.getBounds()))
{
//we selected only the platform
p.setJUMP(false);
p.setY(y - playerH+9); //stay on platform height
}*/

if(playerX + playerW >= x && playerX <= x + width)
{
if(playerY+playerH >= y && playerY <= y+height)
{
p.setJUMP(false);
p.setY(y - playerH);
}
}
}

//draw ground
public void paint(Graphics g)
{
//g.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer)
//dx1 = x postion top left conver
//dy1 = y postion top left coner
//dx2 = x postion bottom right conver
//dy2 = y postion bottom right coner
//sx1 = x cut off postion top left coner
//sx1 = x cutt of postion top left coner
//sx2 = x cut off postion bottom right coner
//sy2 = y cut off postion bottom right coner
//to display 1st frame
//g.drawImage(platform_image, x, y, x+width, y+height,0, 0, width, height,Sprite_Sheet.m1);
//loop 3 frames
g.drawImage(platform_image,x, y, x+width, y+height, 0, height*(int)frame, width, height*(int)frame+height,Sprite_Sheet.m1);

}
}```

player.java
```public class Player
{
//Player movement variable
private int sx = 10;
private int sy = 200;
private int x;     //current x
private int y;     //current y
private int width = 30;
private int height = 40;
private int dx = 2;    //speed of player

private double dy = 6.0;  //change in y over time
private double gravity = 0.2;

private boolean look_right = true;
private boolean walk_right;
private boolean walk_left;
private boolean jump = false;
private boolean fall = false;   //fall without jump. so hold right on platform. it will fall
private boolean hitSPACE = false;

/*** image variable ***/
Image player_image;
URL url;
float frame = 0;

/*** constructor Method ***/
public Player()
{
}
/*** constructor Method2 ***/
public Player(int ix, int iy)
{
x = ix;
y = iy;
player_image = Sprite_Sheet.player_sheet;
}

/*** for player collsion - name stay same ***/
public Rectangle getBounds()
{ return  new Rectangle(x, y, 13,30);}

public int getX()
{ return x; }
public void setX(int value)
{ this.x =  value; }

public int getY()
{ return y; }
public void setY(int value)
{ this.y =  value; }

public int getDX()
{ return dx; }
public void setDX(int value)
{ this.dx =  value; }

public int getWIDTH()
{ return width; }
public void setWIDTH(int value)
{ this.width =  value; }

public int getHEIGHT()
{ return height; }
public void setHEIGHT(int value)
{ this.height =  value; }

public double getGRAVITY()
{ return gravity; }
public void setGRAVITY(double value)
{ this.gravity =  value; }

public double getDY()
{ return dy; }
public void setDY(double value)
{ this.dy =  value; }

public void setJUMP(boolean value)
{ this.jump = value; }

public void setFALL(boolean value)
{ this.fall = value; }

/*** main key methods ***/
/*** RIGHT ***/
public void hitRIGHT()  //if user hit right button
{
walk_right = true;
look_right = true;
}
public void stopRIGHT() //if user let go of right button
{
walk_right = false;  //stop
look_right = true;
}

/*** LEFT ***/
public void hitLEFT()  //move left
{
walk_left = true;
look_right = false;
}
public void stopLEFT()
{
walk_left = false;  //stop
look_right = false;
}

/*** JUMP ***/
public void hitJUMP(Ground g)
{
if(jump == false)//if on ground
{
jump = true;
dy = 6.0;	//reset
}
}

/**************************/
/****** LOOP METHODS ******/
/**************************/
/*** player move method ***/
public void PLAYER_MOVE(Main m, Ground g) //player move's
{
int tester = (int)(frame + .1); //.1 slow down
if(tester < 3)
{
frame += .1;
}
else
{
frame = 0;
}

if(walk_right == true)
{
x += dx;
}
else if(walk_right == false)
{
}

if(walk_left == true)  //walking left
{
if(x - dx < 0)  //cant go left
{
x = 0;
}
else
{
x -= dx;      //move left
}
}
else if(walk_left == false)
{
}

if(jump == true)
{
dy -= gravity;
y -= dy;

if(y+height >= g.getY()) //if on ground
{
jump = false;
}
}
else if(jump == false) //move player down when game starts
{
y += 2; //fall of platform speed or start of game going down
fall = true;
}
}/***end of update method ***/

/*** paint method ***/
public void paint(Graphics g)
{
if(jump == false)
{
if(look_right == true)
{
if(dead == false && walk_right == true) //walk right
{
g.drawImage(player_image, x, y, x+width, y+height,
40*(int)frame, 47, 40*(int)frame+40, 90 ,Sprite_Sheet.m1);

}
else if(dead == false && walk_right == false) //stand right
{

g.drawImage(player_image, x, y, x+width, y+height,
0, 0, 40 , 42,Sprite_Sheet.m1);
}
{
g.drawImage(player_image, x, y+10, x+width, y+height,
0, 230, 40, 268 ,Sprite_Sheet.m1);
}
}
else if(look_right == false)
{
if(dead == false && walk_left == true) //walk left
{
g.drawImage(player_image, x, y, x+width, y+height,
40*(int)frame, 87, 40*(int)frame+40, 125 ,Sprite_Sheet.m1);
}
else if(dead == false && walk_left == false) //stand left
{
g.drawImage(player_image, x, y, x+width, y+height,
40, 0, 80, 42 ,Sprite_Sheet.m1);
}
{
g.drawImage(player_image, x, y+10, x+width, y+height,
41, 230, 82, 268 ,Sprite_Sheet.m1);
}
}
}
else if(jump == true)
{
if (dead == false && look_right == true) //jump right
{
g.drawImage(player_image, x, y, x+width, y+height,
0, 129, 40, 172 ,Sprite_Sheet.m1);
}
{
g.drawImage(player_image, x, y+10, x+width, y+height,
0, 230, 40, 268 ,Sprite_Sheet.m1);
}
else if(dead == false && look_right == false) //jump left
{
g.drawImage(player_image, x, y, x+width, y+height,
0, 187, 40, 236 ,Sprite_Sheet.m1);
}
else if(dead == true && look_right == false)
{
g.drawImage(player_image, x, y+10, x+width, y+height,
41, 230, 82, 268 ,Sprite_Sheet.m1);
}
}
}
}/*** end of class ***/```

main.java
```public class Main extends Applet implements Runnable, KeyListener, MouseMotionListener, MouseListener
{
//Double buffering variables
Image dbImage;
Graphics dbGraphics;

//Class variables
Background background_class;
Score score_class;
Ground ground_class;
Sprite_Sheet sprite_sheet_class;
Player player_class;
Platform platform_class[] = new Platform[5];
Enemy  enemy_class;
Item item_class[] = new Item[3];
Shoot shoot_class;

//is if game is running or not
boolean running = true;
boolean mouseIn = false;

Container c;

/*** init method ***/
@Override
public void init()
{
setSize(900, 400);

}/*** end of init method ***/

/*** start method ***/
@Override
public void start()
{
/*** set variables ***/
Random r1 = new Random();

ground_class = new Ground(0, this.getHeight()-20,this.getSize().width, 20); //x, y, widith, height
sprite_sheet_class = new Sprite_Sheet(this);
background_class = new Background();
player_class = new Player(5, 250);  //x, y
for(int i = 0; i < platform_class.length; i++)
{
Random r = new Random();
platform_class[i] = new Platform(i*120, 300); //x,y --sub -40 so no cut off platform at bottom
}
//item_class = new Item_0_Background(200); //x print 1 item
for(int i = 0; i < item_class.length; i++)
{ item_class[i] = new Item_1_Fast(r1.nextInt(900)); } //x  - print 1 item than swich to 2nd item
shoot_class = new Shoot();
enemy_class = new Enemy(500, 345);   //x, y

score_class = new Score(5,30);

/*** play music in main ***/
sprite_sheet_class.theme_music.loop();

}/*** end of start method ***/

/*** run method ***/

public void run()
{
{
background_class.BACKGROUND_MOVE(this);

//set score
score_class.SCORE();
score_class.health(player_class, enemy_class, sprite_sheet_class);

Random r = new Random();
ground_class.GROUND_COLLISION(this, player_class);                         //ground + player collions
player_class.PLAYER_MOVE(this, ground_class);                                  //player moves

for(int i = 0; i < platform_class.length; i++)
{ platform_class[i].PLATFORM_ANIMATION();
platform_class[i].PLATFORM_COLLSION(this, player_class); }
shoot_class.PLAYER_SHOOT_MOVE(this, player_class, shoot_class);  //player shoot bullet moves
enemy_class.ENEMY_MOVE(this, sprite_sheet_class);
enemy_class.PLAYER_ENEMY_COLLISION(this, player_class, enemy_class);          //player + enemy collision
for(int i = 0; i < item_class.length; i++)
{
item_class[i].PLAYER_ITEM_COLLISION(this, score_class, player_class,  ground_class);
if(item_class[i].getY() == 0)         //if item is top left coner
{
item_class[i] = null;                 //delete item
item_class[i] = new Item_0_Background(r.nextInt(900)-50);
int ra = r.nextInt(4);
if(ra == 0)
{
item_class[i] = new Item_0_Background(r.nextInt(900)-10);  //create new item
}
else if(ra == 1)
{
item_class[i] = new Item_1_Fast(r.nextInt(900)-10);  //create new item
}
else if(ra == 2)
{
item_class[i] = new Item_2_SLOW(r.nextInt(900)-10); //	swich to different item
}
else if(ra == 3)
{
item_class[i] = new Item_3_Score(r.nextInt(900)-10);
}
}
}

/* msuic when player is dead */
{
sprite_sheet_class.theme_music.stop();
}

repaint();
try
{
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}/*** end of run method ***/

/*** update method ***/
@Override
public void update(Graphics g)
{
if(dbImage == null) //if image is empty than create new image
{
dbImage = createImage(this.getSize().width, this.getSize().height);
dbGraphics = dbImage.getGraphics();
}
dbGraphics.setColor(getBackground());  //set the background color
dbGraphics.fillRect(0, 0, this.getSize().width, this.getSize().height);
dbGraphics.setColor(getForeground());
paint(dbGraphics);     //call paint method
g.drawImage(dbImage, 0, 0, this);
}/*** end of update method ***/

/*** paint method ***/
@Override
public void paint(Graphics g)
{
background_class.paint(g, this);
ground_class.paint(g);             //call paint method from Ground class  //draw ground than player on top

player_class.paint(g);             //call paint method from Player classr
for(int i = 0; i < platform_class.length; i++)
{ platform_class[i].paint(g); }
shoot_class.paint(g); //draw bullet
{
enemy_class.paint(g);
}
for(int i = 0; i < item_class.length; i++)
{ item_class[i].paint(g); }
score_class.paint(g, this, player_class);

}/*** end of paint method ***/

/*** stop method ***/
@Override
public void stop()
{

}/*** end of stop method ***/

/*** destroy method ***/
@Override
public void destroy()
{
}/*** end of destroy method ***/

/************** key *********************/
@Override
public void keyPressed(KeyEvent e)
{
int keys = e.getKeyCode();
if(keys == KeyEvent.VK_RIGHT)
{
player_class.hitRIGHT();
sprite_sheet_class.running_music.loop();
}
else if(keys == KeyEvent.VK_LEFT)
{
player_class.hitLEFT();
sprite_sheet_class.running_music.loop();

}
else if (keys == KeyEvent.VK_UP)
{
player_class.hitJUMP(ground_class);
sprite_sheet_class.jump_music.play();

}
else if(keys == KeyEvent.VK_SPACE)
{
shoot_class.hitSHOOT();
}
}
@Override
public void keyReleased(KeyEvent e)
{
int keys = e.getKeyCode();
if(keys == KeyEvent.VK_RIGHT)
{
player_class.stopRIGHT();
sprite_sheet_class.running_music.stop();
}
else if(keys == KeyEvent.VK_LEFT)
{
player_class.stopLEFT();
sprite_sheet_class.running_music.stop();
}
else if(keys == KeyEvent.VK_SPACE)
{
shoot_class.stopSHOOT();
}

}
@Override
public void keyTyped(KeyEvent e){}

/*** mouse mostion ***/
@Override
public void mouseDragged(MouseEvent e) {}
@Override
public void mouseMoved(MouseEvent e)
{
if(e.getX() > 270 && e.getX() < 460)
{
if(e.getY() > 310 && e.getY() < 350)
{
mouseIn = true;
}
}

if(e.getX() < 280 || e.getX() > 460)
{
mouseIn = false;

}
if(e.getY() < 320 || e.getY() > 360)
{
mouseIn = false;
}
}

/************    MOUSE  **********/
@Override
public void mousePressed(MouseEvent e)
{
//start game over
if(mouseIn == true)
{
start();
}
//mouseIn = false;
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}

}```

2. ## Re: collision between player and platform

Originally Posted by hwoarang69
... now i need help with goal-2,3:...
What *specific* help do you need? What have you tried? How is this attempt not working? What specifically confuses you?

3. ## Re: collision between player and platform

no i didnt started it. i need help with logic first.

here is what iam thinking, my main code should be in these if statment

if (playerX <= x && playerY+playerH >= y) //player on right of platform and player feet is lower than platform
{
System.out.println("right");
p.setX(x - playerW);
}
else if(playerX >= x && playerY+playerH >= y) //player on left of platform
{
System.out.println("left");
p.setX(x + width);
}
else if(playerY+playerH <= y) //player on top of platform
{
System.out.println("top");
p.setJUMP(false);
p.setY(y - playerH);
}
but now the player on top if statment dont work. but player right and player left if statment works fine.