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.

Page 2 of 2 FirstFirst 12
Results 26 to 30 of 30

Thread: Collision Detection between Sprites and Images?

  1. #26
    Member
    Join Date
    Mar 2012
    Location
    Billings, MT
    Posts
    47
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Collision Detection between Sprites and Images?

    Thanks, I've done that, though the person is still going through the cobblestone images. What part of the code shall I post to you?

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Collision Detection between Sprites and Images?

    Time for some debugging. Print the values of the variables that control where and when the person moves. Be sure to indicate the place and time when the person should stop moving. If the person's position continues to change after that point, look at the code that is changing the position and see why it is doing it.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #28
    Junior Member
    Join Date
    Jun 2012
    Location
    Birmingham
    Posts
    20
    My Mood
    Depressed
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Collision Detection between Sprites and Images?

    hey im new but i been working on collision too.. id like to show you have i do collision detection an see if it helps you....
    // so i write my collision detection in my paint method..... i dont know if that a bad thing but it works perfect for me...

    Image sprite();
    Rectangle SpriteRect = new Rectangle(X,Y,100,100);
    Rectangle Two = new Rectangle(100,100,100,100);

    public void loadImage(){
    sprite = new ImageIcon("C:\\place on HD").getImage;

    }

    public void paint(Graphics g){
    super.paint(g);
    Graphics2D g2 =(Graphics2D)g;

    g2d.drawRect(sprite,SpriteRect.x, SpriteRect.y, SpriteRect.width, SpriteRect.height);
    g2d.drawRect(Two, 50,50,200,200);

    if(SpriteRect.intersects(Two)){CODE YOU WANT IS INTERSECT = TRUE};


    }







    really hope this helps and any questions just ask p.s i just typed this in so if i missed anything let me now i will look over it in a sec

  4. #29
    Junior Member
    Join Date
    Jun 2012
    Location
    Birmingham
    Posts
    20
    My Mood
    Depressed
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Collision Detection between Sprites and Images?

    hey im new but i been working on collision too.. id like to show you have i do collision detection an see if it helps you....
    // so i write my collision detection in my paint method..... i dont know if that a bad thing but it works perfect for me...

    Image sprite();
    Rectangle SpriteRect = new Rectangle(X,Y,100,100);
    Rectangle Two = new Rectangle(100,100,100,100);

    public void loadImage(){
    sprite = new ImageIcon("C:\\place on HD").getImage;

    }

    public void paint(Graphics g){
    super.paint(g);
    Graphics2D g2 =(Graphics2D)g;

    g2.drawRect(sprite,SpriteRect.x, SpriteRect.y, SpriteRect.width, SpriteRect.height);
    g2.drawRect(Two, Two.x,Two.y,Two.width,Two.height);

    if(SpriteRect.intersects(Two)){CODE YOU WANT IF INTERSECT = TRUE};

    loadImage();
    }







    really hope this helps and any questions just ask p.s i just typed this in so if i missed anything let me now i will look over it in a sec

    sorry for double post this post is correct first one i made one or two little mistakes

  5. #30
    Junior Member
    Join Date
    Jun 2012
    Posts
    1
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Collision Detection between Sprites and Images?

    why don't you write a collision metod in entites class???

    you should use a superclass for sprite where you will write all the common methods so you can override them if it will be necessary

    example :
    class player extends Sprite{

    @Override
    public void collide(){
    dx = 0;
    dy = 0;
    }
    }


    class enemy extends Sprite{

    @Override
    public void collide(){
    dx = 0;
    dy = 0;
    }
    }

    and in your collision manager method....

    if(isCollided(sprite1,sprite2){
    sprite1.collide;
    sprite2.collide;
    }

    "isCollided is the hypothetical method that checks collisions but i will not write it but if you need it i will XD"

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Pong game - Collision detection
    By Hokap in forum Java Theory & Questions
    Replies: 73
    Last Post: May 13th, 2012, 04:11 PM
  2. AI, Collision Detection, and Timing
    By Staticity in forum Java Theory & Questions
    Replies: 0
    Last Post: March 20th, 2012, 02:12 PM
  3. collision detection not working...
    By skberger21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2011, 09:02 PM
  4. Collision Detection difficulties
    By Uritomi in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 20th, 2011, 10:10 AM
  5. 2D Collision Detection
    By Cuju in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2010, 10:39 AM