Reputation: 31
My Collision code for platforms in my Player class isn't working and sometimes glitches out. I created different methods for applying gravity and whatnot.
When the player is under a platform and they jump, I want them to collide with the bottom of the platform and fall back down using the gravity method I created.
When the player walks into both ends of the platform I want them to just stop beside it.
I want to make sure at all times that the player cannot go through the platform and that seems to be my downfall.
I would really appreciate it if anyone could help me out.
Here is my Player class, the applet screen size is 600x400:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Player implements KeyListener
{
int x, y, width, height;
boolean jump, left, right;
PlayerThread playerThread;
int maxHeight = 40;
double heightC = 0;
boolean onPlatform = false;
boolean landed = true;
int prevY;
public Player()
{
x = 500;
y = 350;
width = 40;
height = 50;
playerThread = new PlayerThread();
playerThread.start();
}
public void paint(Graphics g)
{
String str = String.valueOf(x);
String str2 = String.valueOf(y);
g.setColor(Color.RED);
g.fillRect(x, y, width, height);
g.drawString("X: " + str + ", Y: " + str2, 100, 100);
}
public void update(Platform p)
{
CheckForCollision(p);
}
public void CheckForCollision(Platform p)
{
int pX = p.getX();
int pY = p.getY();
int pWidth = p.getWidth();
int pHeight = p.getHeight();
//THIS IS COLLISION DETECTION CODE THAT DOES NOT WORK
if (y + height > pY && y < pY + pHeight && x + width > pX && x < pX + pWidth)
{
y = prevY;
landed = true;
}
}
public class PlayerThread extends Thread implements Runnable
{
public void run()
{
while (true)
{
if (left)
{
x -= 2;
}
if (right)
{
x += 2;
}
if (jump)
{
if (heightC >= maxHeight)
{
System.out.println(heightC);
jump = false;
heightC = 0;
}
else
{
heightC += 1.5;
prevY = y;
y -= 5;
landed = false;
}
}
//GRAVITY CODE
if (!jump)
{
if (y < 400 - height && !landed)
{
prevY = y;
y += 5;
landed = false;
}
}
if (y >= 400 - height)
{
y = 400 - height;
landed = true;
}
try
{
sleep(17);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Override
public void keyPressed(KeyEvent e)
{
switch (e.getKeyCode())
{
case KeyEvent.VK_LEFT:
left = true;
break;
case KeyEvent.VK_RIGHT:
right = true;
break;
case KeyEvent.VK_SPACE:
if (landed)
{
jump = true;
}
break;
}
}
@Override
public void keyReleased(KeyEvent e)
{
switch (e.getKeyCode())
{
case KeyEvent.VK_LEFT:
left = false;
break;
case KeyEvent.VK_RIGHT:
right = false;
break;
case KeyEvent.VK_SPACE:
break;
}
}
public int getX()
{
return x;
}
public void setX(int x)
{
this.x = x;
}
public int getY()
{
return y;
}
public void setY(int y)
{
this.y = y;
}
public int getWidth()
{
return width;
}
public void setWidth(int width)
{
this.width = width;
}
public int getHeight()
{
return height;
}
public void setHeight(int height)
{
this.height = height;
}
@Override
public void keyTyped(KeyEvent e)
{
// TODO Auto-generated method stub
}
}
Upvotes: 1
Views: 921
Reputation: 7634
Your problem is with your detection code. I assume, you are using basic rectangle collisions, based on the if statement, however your clauses seem to be a bit strange.
Lets break them down:
y + height > pY
- If the bottom of the player is below than the top of the platform.
y < pY + pHeight
- If the top of the player is above than the bottom of the platform
x + width > pX
- If the right of the player is to the right of the left of the platform
x < pX + pWidth
- The the left of the player is to the left of the right of the platform
Now note the logic here: This will only trigger iff the player is completly inside the platform, which I expect is not what you want. What you probably want is more akin to a proof by negation:
if(!(y > pY + pHeight || y + height < pY || x > pX + pWidth || x + width < pX)){
//There is a collision
}
On a side note, although I would generally agree with Mr. D and his comment of using a game engine, it is often useful from a learning perspective to know how these things work, so as you are not constrained to someone elses codebase.
Upvotes: 1