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 1 of 2 12 LastLast
Results 1 to 25 of 43

Thread: Checking if an object contains a point

  1. #1
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Checking if an object contains a point

    Hi, I'm having a little trouble seeing if an object contains a point. Here's my code:

    package com.gravitygamesinteractive.kylethecaiman;
     
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
     
    import javax.imageio.ImageIO;
     
    public class Character {
    	private int x,y;
    	public int fallspeed = 3;
     
    	public static BufferedImage kyle;
    	public static BufferedImage cali;
    	public static BufferedImage steve;
    	public static boolean isTouchingFloor;
    	public Character(int x, int y){
    		this.x=x;
    		this.y=y;
    		try {
    		    kyle = ImageIO.read(new File("res/Kyle.png"));
    		} catch (IOException e) {
    		}
    	}
     
    	public void tick(){
    		if(!isCollidingWithFloor(new Point(x,(y+44)),new Point((x+16),(y+44)))){
    		y+=fallspeed;
    		Component.sy+=(fallspeed);
    		}
    	}
     
    	public boolean isCollidingWithFloor(Point pt1, Point pt2){
    		for(int f=0;f<Level.tile.size();f++){
    			if(Level.tile.get(f).contains(pt1) || Level.tile.get(f).contains(pt2)){
    				return true;
    			}
    		}
    		return false;
    	}
     
    	public void render(Graphics g){
    		g.drawRect(x, y, 16, 44);
    		g.drawImage(kyle,Component.sx+x-8,Component.sy+y,null);
     
    	}
    }
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Checking if an object contains a point

    Are you asking about the location of shapes drawn on a Graphics context?
    What kind of shapes are you working with?

    Can you explain what happens when the code is executed?

    Try debugging the code by adding println statements to show what the values of the variables are so you can see what the computer sees and know why it is executing the way it is.
    For example print these: Level.tile.get(f), pt1 and pt2
    Last edited by Norm; September 1st, 2012 at 05:07 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Checking if an object contains a point

    I'm trying to get the player object to stop moving vertically when there is a tile object directly underneath it. The object will not only keep falling, but it also winds up throwing an error when it goes below the objects.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Checking if an object contains a point

    I'd use the term shape instead of object. In OOP an object relates to an instance of a class.
    A shape is something drawn on a screen.

    What prints out when you print the contents of the variables as I suggested?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Checking if an object contains a point

    The number keeps going on. Also, for some reason the crash doesn't happen anymore, in case that helps.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Checking if an object contains a point

    Can you explain what the problem is and post some print outs that shows it?
    You haven't provided any useful information yet about what is happening.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Checking if an object contains a point

    The problem is that the Character object won't stop falling when it touches a ground object. All the printline shows is the y position and that it keeps increasing.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  8. #8
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Checking if an object contains a point

    As your fall speed is +3, what is to stop

    !isCollidingWithFloor(new Point(x,(y+44)),new Point((x+16),(y+44)))

    from returning true 1 pixel above the floor, then incrementing 2 pixels past it?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  9. #9
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Checking if an object contains a point

    Nothing I guess. You think that might be the problem, and if so, how would I fix it?
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  10. #10
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Checking if an object contains a point

    I don't know, it could be.
    We'd need to see your Level class first so we know how big your Shape object is. When norm keeps asking you for all these printlns, he's expecting you to do it, and then post the results back on these forums.
    We cannot read your mind.

    If that is the problem, then you simply check the Y position of your character vs the Y position of the floor. If charY >= floorY, collision = true, set charY = floorY.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Checking if an object contains a point

    Check a virtual move before doing the actual move.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Checking if an object contains a point

    Alright, here's the level class:

    package com.gravitygamesinteractive.kylethecaiman;
     
    import java.awt.*;
    import java.util.*;
    import java.io.File;
     
    public class Level {
    	public static ArrayList<Tile> tile=new ArrayList<Tile>();
    	public static Character kyle;
    	private Scanner s;
    	private int objectId;
    	private int spx;
    	private int spy;
    	private String oidstr;
    	private String xstr;
    	private String ystr;
    	private String placelinetile;
    	private ArrayList<String> objarray= new ArrayList<String>();
    	public static ArrayList<String> xarray= new ArrayList<String>();
    	private ArrayList<String> yarray= new ArrayList<String>();
    	public Level(){
    		stageFile();
    		setStage();
    	}
     
    	public void tick(){
    		kyle.tick();
    	}
     
    	public void render(Graphics g){
    		g.setColor(Color.darkGray);
    		g.fillRect(0, 0, Component.size.width, Component.size.height);
    		kyle.render(g);
    		for(int d=0;d<tile.size();d++){
    			tile.get(d).render(g);
    		}
    	}
     
    	public void stageFile(){
    		try{
    			String stage = new String("assets/stage1.txt");
    			System.out.println("stageset");
    			s = new Scanner(new File("assets/world1stage1.txt"));
    			System.out.println("scanset");
    			while(s.hasNextLine()){
    				objectId=s.nextInt();
    	            oidstr=Integer.toString(objectId);
    				objarray.add(oidstr);
    				spx=s.nextInt();
    				xstr=Integer.toString(spx);
    				xarray.add(xstr);
    				spy=s.nextInt();
    				ystr=Integer.toString(spy);
    				yarray.add(ystr);
    				System.out.println("Object: " + objectId + " At: " + spx + "," + spy);
    	}
    			s.close();
    	}catch(Exception e){
    		System.out.println("error");
    	}
     
    	}
     
    	public void setStage(){
    		for(int oa=0; oa<objarray.size(); oa++){
    			if(Integer.parseInt(objarray.get(oa))==0){
    				kyle=new Character(Integer.parseInt(xarray.get(oa)),Integer.parseInt(yarray.get(oa)));
    			}else{
    			tile.add(new Tile(Integer.parseInt(xarray.get(oa)),Integer.parseInt(yarray.get(oa))));
    			}
    		}
    		//Stages.stage1.setStage();
    		//for (int a = 0; a < objarray.size(); a++) {
    		//new Tile(Integer.parseInt(xarray.get(a)),Integer.parseInt(yarray.get(a)));
    		//}
    	}
    }

    Also, I don't think its the fallspeed, as the Character object falls through even when I set it to 1.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Checking if an object contains a point

    What is printed out by the printlns of the variables?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Checking if an object contains a point

    Here's all the printline results, including the one that prints the y value of the player:

    stageset
    scanset
    Object: 0 At: 10,40
    Object: 1 At: 0,583
    Object: 1 At: 16,583
    Object: 1 At: 32,583
    Object: 1 At: 48,583
    Object: 1 At: 64,583
    Object: 1 At: 80,583
    Object: 1 At: 96,583
    Object: 1 At: 128,583
    Object: 1 At: 160,583
    Object: 1 At: 192,583
    Object: 1 At: 224,583
    Object: 1 At: 256,583
    Object: 1 At: 288,583
    Object: 1 At: 320,583
    Object: 1 At: 352,583
    Object: 1 At: 384,583
    Object: 1 At: 416,583
    Object: 1 At: 448,583
    Object: 1 At: 480,583
    Object: 1 At: 512,583
    Object: 1 At: 544,583
    Object: 1 At: 576,583
    Object: 1 At: 608,583
    Object: 1 At: 640,583
    43
    46
    49
    52
    55
    58
    61
    64
    67
    70
    73
    76
    79
    82
    85
    88
    91
    94
    97
    100
    103
    106
    109
    112
    115
    118
    121
    124
    127
    130
    133
    136
    139
    142
    145
    148
    151
    154
    157
    160
    163
    166
    169
    172
    175
    178
    181
    184
    187
    190
    193
    196
    199
    202
    205
    208
    211
    214
    217
    220
    223
    226
    229
    232
    235
    238
    241
    244
    247
    250
    253
    256
    259
    262
    265
    268
    271
    274
    277
    280
    283
    286
    289
    292
    295
    298
    301
    304
    307
    310
    313
    316
    319
    322
    325
    328
    331
    334
    337
    340
    343
    346
    349
    352
    355
    358
    361
    364
    367
    370
    373
    376
    379
    382
    385
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Checking if an object contains a point

    Which are the values of Level.tile.get(f),
    which are pt1
    and which are pt2


    I would expect to see the values of all three printed just before this line:

    if(Level.tile.get(f).contains(pt1) || Level.tile.get(f).contains(pt2)){

    so you could see what that line of code is testing and understand why it works like it does. Is the problem that the above line never is true?

    Just a list of numbers like you posted has no use.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Checking if an object contains a point

    Printlining the points returns a very similar list. However, the Level.tile.get(f) doesn't print at all.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Checking if an object contains a point

    Level.tile.get(f) doesn't print at all.
    Please explain. Something must print or is there an error?

    Can you post the println statement and the next few lines of code that shows where the println statement is located?

    Is this the statement that is not working as you expect:

    if(Level.tile.get(f).contains(pt1) || Level.tile.get(f).contains(pt2)){
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Checking if an object contains a point

    Yes, as far as I know, that's the problem line. The printline doesn't print anything there either.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Checking if an object contains a point

    If the println line doesn't print anything that would indicate that the statement is not being executed.

    Check the logic to see if it is possible that the println is not being executed. Add lots of println statements to show the execution flow and the value of variables that would control execution flow getting to the println and the following if statement.
    For example what is the value of Level.tile.size()?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Checking if an object contains a point

    Level.tile is an arraylist of tile objects, and the size value is the number of tile objects in the arraylist. I'm not sure why its not getting called though, as it is referenced in the tick method.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Checking if an object contains a point

    Did you print out the value of Level.tile.size() in the isCollidingWithFloor() method?
    Is the tick() method being called? Add a println first thing in tick() that prints a message to see.

    That's it for tonight. Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #22
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Checking if an object contains a point

    When I use the printline, it correctly shows the number of objects in the arraylist, so that's not the problem. The problem surely has to be the line you said earlier, I must be using the wrong method or something...
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Checking if an object contains a point

    problem surely has to be the line you said earlier,
    Can you post the code with printlns in the isCollidingWithFloor() method
    and also post the output from those printlns?
    Last edited by Norm; September 2nd, 2012 at 12:04 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Checking if an object contains a point

    Code with the printlines:

    public boolean isCollidingWithFloor(Point pt1, Point pt2){
    		for(int f=0;f<Level.tile.size();f++){
    			System.out.println(Level.tile.size());
    			if(Level.tile.get(f).contains(pt1) || Level.tile.get(f).contains(pt2)){
    				System.out.println(pt1);
    				System.out.println(pt2);
    				return true;
    			}
    		}
    		return false;
    	}

    The printline for the Level.tile.size() is the only one that prints. All that nets me is a bunch of 24s.

    If it helps, what I'm trying to do is:

    Check if there is an object touching the player.
    If the object is beneath the player objects points (pt1 and pt2), return true.
    If there is not an object beneath the player that fits this description, return false.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  25. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Checking if an object contains a point

    The printlns should be BEFORE the values of the variables are used, not AFTER:
    public boolean isCollidingWithFloor(Point pt1, Point pt2){
    		System.out.println("L.T.size="+Level.tile.size());
    		for(int f=0;f<Level.tile.size();f++){
    		   System.out.println("L.T.get="Level.tile.get(f) +" pt1="+pt1 +" pt2="+pt2);
    			if(Level.tile.get(f).contains(pt1) || Level.tile.get(f).contains(pt2)){
    				return true;
    			}
    		}
    		return false;
    	}
    Also there should be ID Strings with the numbers so you know what is printed. A long list of unlabelled numbers is VERY hard to understand.

    You are trying to see why the if statement never returns true. Print out all the values used in the if to see what the computer sees.
    Last edited by Norm; September 2nd, 2012 at 12:35 PM. Reason: spelling
    If you don't understand my answer, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. regarding floating point
    By deependeroracle in forum Java Theory & Questions
    Replies: 3
    Last Post: January 10th, 2012, 11:18 AM
  2. [SOLVED] Someone point me to the right direction..
    By ineedhelp in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: June 30th, 2011, 10:03 PM
  3. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  4. Point constructor
    By upad in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 5th, 2010, 07:34 PM
  5. how do i use point 3d?
    By antrax in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 6th, 2010, 05:13 PM