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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: collision detection not working...

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default collision detection not working...

    I am coding a game that is a maze and im trying to figure out where to put my collision detection but anywhere it try it it doesnt work... i tried to put it int o multiple classes but it got to confuzing so i started basic. the code is... please help!
    package ballgame;

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;

    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    //create class that extends JFrame


    public class BALLGAME extends JFrame implements Runnable {


    double p1speed = .5, p2speed = .5;
    int x, y, xDirection, yDirection;
    private Image dbImage;
    private Graphics dbGraphics;
    Image face;



    public void run(){
    try{
    while(true){
    move();

    Thread.sleep(3);
    }

    }catch(Exception e){
    System.out.println("ERROR");
    }
    }

    public void move(){
    x += xDirection;
    y += yDirection;
    if(x <= 0)
    x=0;
    if(x>= 1180)
    x=1180;
    if(y <= 20)
    y = 20;
    if(y >= 780)
    y = 780;


    }
    public void setXDirection(int xdir){
    xDirection = xdir;

    }

    public void setYDirection(int ydir){
    yDirection = ydir;
    }

    public class AL extends KeyAdapter{
    public void keyPressed(KeyEvent e){
    int Keycode = e.getKeyCode();
    if(Keycode == e.VK_UP){
    setYDirection(-1);

    }
    if(Keycode == e.VK_LEFT){
    setXDirection(-1);

    }
    if(Keycode == e.VK_DOWN){
    setYDirection(+1);

    }
    if(Keycode == e.VK_RIGHT){
    setXDirection(+1);
    }


    }
    public void keyReleased(KeyEvent e){
    int Keycode = e.getKeyCode();
    if(Keycode == e.VK_UP){
    setYDirection(0);

    }
    if(Keycode == e.VK_LEFT){
    setXDirection(0);
    }
    if(Keycode == e.VK_DOWN){
    setYDirection(0);
    }
    if(Keycode == e.VK_RIGHT){
    setXDirection(0);
    }

    }
    }




    //constructor to create frame properties
    public BALLGAME(){


    setTitle("spanky babies");
    addKeyListener(new AL());
    setSize(1200,800);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    x = 150;
    y = 150;

    }
    //main method
    public static void main(String [] args){

    //calls the frame
    BALLGAME t = new BALLGAME();

    Thread t1 = new Thread(t);
    t1.start();
    }









    public void paint(Graphics g){
    dbImage =createImage(getWidth(), getHeight());
    dbGraphics = dbImage.getGraphics();
    paintCopmponent(dbGraphics);
    g.drawImage(dbImage, 0, 0, this);
    }
    // a graphics method allowing you to access the graphics to draw on the screen
    public void paintCopmponent(Graphics g){
    Rectangle p1 = new Rectangle(250,750,20,20);
    Rectangle ob = new Rectangle(200,250,90,90);

    g.setColor(Color.GRAY);

    g.fillRect(ob.x, ob.y, ob.width, ob.height);


    g.setColor(Color.blue);
    //draw player 1

    g.fillRect(x, y,p1.width, p1.height);


    if(p1.intersects(ob)){
    //to test if it works
    System.out.println("x");





    }
    //Collision












    //paint it all
    repaint();

    }

    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: collision detection not working...

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting.

    When you change the x and y values of the thing that is moving, you could test the new values to see if they will cause a collision.

Similar Threads

  1. Java USB detection
    By wrightm96 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 21st, 2012, 06:27 AM
  2. Collision Detection difficulties
    By Uritomi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 20th, 2011, 10:10 AM
  3. Collision detection - whats wrong with my code?
    By Javaz in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 22nd, 2011, 08:20 PM
  4. 2D Collision Detection
    By Cuju in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2010, 10:39 AM
  5. for Loop for my 2D collision not working??
    By DarrenReeder in forum Loops & Control Statements
    Replies: 1
    Last Post: March 7th, 2010, 10:05 AM

Tags for this Thread