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 43 of 43

Thread: Checking if an object contains a point

  1. #26
    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 pasted your code over mine, it returned this over and over:

    com.gravitygamesinteractive.kylethecaiman.Tile[x=192,y=583,width=0,height=0]
    com.gravitygamesinteractive.kylethecaiman.Tile[x=224,y=583,width=0,height=0]
    com.gravitygamesinteractive.kylethecaiman.Tile[x=256,y=583,width=0,height=0]
    com.gravitygamesinteractive.kylethecaiman.Tile[x=288,y=583,width=0,height=0]
    com.gravitygamesinteractive.kylethecaiman.Tile[x=320,y=583,width=0,height=0]
    com.gravitygamesinteractive.kylethecaiman.Tile[x=352,y=583,width=0,height=0]
    com.gravitygamesinteractive.kylethecaiman.Tile[x=384,y=583,width=0,height=0]
    com.gravitygamesinteractive.kylethecaiman.Tile[x=416,y=583,width=0,height=0]
    com.gravitygamesinteractive.kylethecaiman.Tile[x=448,y=583,width=0,height=0]
    com.gravitygamesinteractive.kylethecaiman.Tile[x=480,y=583,width=0,height=0]
    com.gravitygamesinteractive.kylethecaiman.Tile[x=512,y=583,width=0,height=0]
    com.gravitygamesinteractive.kylethecaiman.Tile[x=544,y=583,width=0,height=0]
    com.gravitygamesinteractive.kylethecaiman.Tile[x=576,y=583,width=0,height=0]
    com.gravitygamesinteractive.kylethecaiman.Tile[x=608,y=583,width=0,height=0]
    com.gravitygamesinteractive.kylethecaiman.Tile[x=640,y=583,width=0,height=0]
    L.T.size=24
    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. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Checking if an object contains a point

    Did you look at what was printed? If width and height = 0, I don't think contains() can be true.

    Where are the values of pt1 and pt2?


    What is a Tile class object? What does its toString() method return?
    How does its contains() method work?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #28
    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 guess that means the width and height is somehow not being set, I could have sworn that I did in the drawRect for it:

    package com.gravitygamesinteractive.kylethecaiman;
     
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.JFrame;
    import javax.imageio.*;
    import java.io.*;
     
    public class Tile extends Rectangle{
    	private static final long serialVersionUID=1L;
     
    	public static int[] g1top={0,0};
    	public static BufferedImage tileset1ground;
    	public Tile(int x, int y){
    		this.x=x;
    		this.y=y;
    		try {
    		    tileset1ground = ImageIO.read(new File("res/tileset1.png"));
    		} catch (IOException e) {
    		}
    	}
     
     
     
    	public void render(Graphics g){
    		g.setColor(Color.red);
    		g.drawRect(x, y, 16, 16);
    		g.drawImage(tileset1ground,x-Component.sx,y-Component.sy,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)

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Checking if an object contains a point

    Where are they being set?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #30
    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 thought that the g.drawRect line is what sets them...
    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. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Checking if an object contains a point

    I guess it doesn't.

    There is no relationship between some hardcoded values in a call to the drawRect() method and the values saved in an extension of the Rectangle class. You have to set them.
    You should use the class variables to hold the values and not use the hardcoded 16 in the method call.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #32
    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

    That worked! The tiles collision is working perfectly now...when the scrolling is disabled. Unfortunately, for some reason when I scroll the tiles graphics up or down, the collision doesn't move with it.

    Tile code:
    package com.gravitygamesinteractive.kylethecaiman;
     
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.JFrame;
    import javax.imageio.*;
    import java.io.*;
     
    public class Tile extends Rectangle{
    	private static final long serialVersionUID=1L;
     
    	public static int[] g1top={0,0};
    	public static BufferedImage tileset1ground;
    	public Tile(int x, int y){
    		this.x=x;
    		this.y=y;
    		this.width=16;
    		this.height=16;
    		try {
    		    tileset1ground = ImageIO.read(new File("res/tileset1.png"));
    		} catch (IOException e) {
    		}
    	}
     
    public void tick(){
    }
     
    	public void render(Graphics g){
    		g.setColor(Color.red);
    		g.drawRect(x, y, 16, 16);
    		g.drawImage(tileset1ground,x-Component.sx,y-Component.sy,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)

  8. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Checking if an object contains a point

    Time for some more debugging.
    Do you see how the printing out of the values of the tiles before the if statement showed you what the problem was for detecting collisions?
    With the new problem, you need to see what variables control the scrolling of the tiles and see how their values are changing and what is happening or not happening.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #34
    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

    From what I can tell, its not actually reading from the individual tiles, but from the tile objects in the arraylist. I'm guessing that fixing this either requires rewriting the level format or change the game's design so I don't need scrolling levels...
    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. #35
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Checking if an object contains a point

    I have no idea what you are talking about.
    If you have any questions, you'll have to show the code and ask specific questions about it.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #36
    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 the code:

    Level:
    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();
    		for(int e=0;e<tile.size();e++){
    			tile.get(e).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))));
    			}
    		}
    		}
    	}
    }

    Tile:
    package com.gravitygamesinteractive.kylethecaiman;
     
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.JFrame;
    import javax.imageio.*;
    import java.io.*;
     
    public class Tile extends Rectangle{
    	private static final long serialVersionUID=1L;
     
    	public static int[] g1top={0,0};
    	public static BufferedImage tileset1ground;
    	public Tile(int x, int y){
    		this.x=x;
    		this.y=y;
    		this.width=16;
    		this.height=16;
    		try {
    		    tileset1ground = ImageIO.read(new File("res/tileset1.png"));
    		} catch (IOException e) {
    		}
    	}
     
    public void tick(){
    	if(!Character.isTouchingFloor){
    	}
     
    	//System.out.println(this.x);
    	//System.out.println(this.y);
    }
     
    	public void render(Graphics g){
    		g.setColor(Color.red);
    		g.drawRect(x, y, 16, 16);
    		g.fillRect(0, 200, 16, 16);
    		g.drawImage(tileset1ground,x,y,null);
    	}
    }
    How do I apply changes to the tiles objects to the tile arraylist as well?
    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)

  12. #37
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Checking if an object contains a point

    How do I apply changes to the tiles objects to the tile arraylist as well?
    I don't understand what the problem is or what you are trying to do.
    An ArrayList contains references to objects. You can change the contents of any of those objects and not effect the ArrayList. The only changes you would make to the ArrayList would be to reorder its contents, add new objects or delete objects from it.

    As I said, changing a Tile object that is in an ArrayList does nothing to the ArrayList.
    And removing a Tile object from the ArrayList does nothing to the Tile object.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #38
    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

    Well, I'm trying to affect the arraylist as well as the tiles. How would I do that?
    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)

  14. #39
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Checking if an object contains a point

    What changes do you want to make to the arraylist? Your options are limited to what Norm described in post #37. Iterate over the list and make the desired changes.

    What changes do you want to make to the tiles? Again, iterate over the list of tiles and optionally modify them as you go. Or grab specific objects and modify them without going through the whole list.

  15. #40
    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 change the position of the objects represented in the arraylist.

    For example:

    In the arraylist, I add a tile at x=100, y=100.
    Then I move the object in the objects coding to x=200, y=200.
    The arraylist I try to read from still says the object is at x=100, y=100.
    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)

  16. #41
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Checking if an object contains a point

    Can you be changing a Tile that is not in the ArrayList?

    It is very hard to understand what you are doing. There are many ways the code could do what you are describing.
    If you get a reference to a Tile object that is in the arraylist and change it. Then the next time you get that Tile object from the arraylist, it should have the change. If you have Tile objects that are not in the arraylist and change them, that should have no effect on any of the Tile objects in the arraylist.

    You will have to post code that compiles, executes and shows the problem
    If you don't understand my answer, don't ignore it, ask a question.

  17. #42
    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

    Nevermind, I seem to have figured out the problem. I was neglecting to change where the tiles were drawn in the render method. Apparantly, that's what the Character uses for collision or something...

    EDIT (Off-topic): I had to slow down the jump speed to 2 to keep the player from getting stuck in the ground, but with the ground scrolling as well now, it looks better that way anyways.
    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)

  18. #43
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Checking if an object contains a point

    neglecting to change where the tiles were drawn in the render method.
    See post #31
    If you don't understand my answer, don't ignore it, ask a question.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. regarding floating point
    By deependeroracle in forum Java Theory & Questions
    Replies: 3
    Last Post: January 10th, 2012, 10: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, 04:13 PM