java - collision detection not working? -
im quite new java. trying make can control ball arrow keys , if collide ball, print line in console saying "you lost" or somthing.
i have done moving, struggling bit how can 2 balls collide. far have tried (i think put rectangle around ball, don't know really!):
public void collision() { rectangle rectp = new rectangle(player.x, player.y, player.width, player.height); rectangle recte = new rectangle(enemy.ex, enemy.ey, enemy.width, enemy.height); if(rectp.intersects(recte)) { system.out.println("game over"); } }
could me out , explain have done wrong.
ps. please don't give me code, want try , learn!!!.
thanks.
public void actionperformed(actionevent e) { repaint(); x += velx; y += vely; } public void up() { vely = -1.5; velx = 0; } public void down() { vely = 1.5; velx = 0; } public void left() { vely = 0; velx = -1.5; } public void right() { vely = 0; velx = 1.5; } public void keypressed(keyevent e) { int code = e.getkeycode(); if (code == keyevent.vk_up) { up(); } if (code == keyevent.vk_down) { down(); } if (code == keyevent.vk_left) { left(); } if (code == keyevent.vk_right) { right(); } }
if have coords x , y of 2 ball centers , know balls radiuses - should simple task.
just calculate distance between centers of balls , compare sum of balls radiuses.
you asked skip formulas that's :)
ps looks strange in code player
lowercased enemy
capitalized.. class or object?
Comments
Post a Comment