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

Thread: Simple Collision with no Rect package import

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Simple Collision with no Rect package import

    Hi I am trying to write some code that will detect a collision between two rectangles(no import packages) and have them change color as soon as they touch. I currently have 2 instances (rectangles) appearing (barrier1 and barrier2) and another rectangle instance that moves around with the keyboard (playerObject). Without import packages I'm not sure how to make the playerObject change color when it touches another one of the stationary rectangles.

    I have posted two of the classes but there are another 3 but I don't think they are necessary to post.

    Any help would highly appreciated.

    //MAIN CLASS

    public class Main 
    {
    	public static GameObject playerObject;
    	public static GameObject barrier1;
    	public static GameObject barrier2;
    	public static MUGEGameVA1 mugeTest; // The game engine
     
     
    	public static void main(String[] argv) 
    	{
    		//initialise the player object
    		playerObject = new GameObject();
    		GameObjectComponent pi = new MainControls();
    		playerObject.addComponent(pi);
     
    		//initialise the game world.
    		mugeTest = new MUGEGameVA1();
     
    		//add some objects into it
    		barrier1 = new GameObject();
    		barrier2 = new GameObject();
     
    		mugeTest.addGameObject(playerObject);
    		mugeTest.addGameObject(barrier1);
    		mugeTest.addGameObject(barrier2);
     
     
    		//BEGIIIN!
    		mugeTest.start();
    	}
     
     
    	public static class MainControls extends GameObjectComponent
    	{
     
    		public void update(int delta)
    		{
     
     
    			if (MUGEGameVA1.isKeyDown(MUGEGameVA1.KEY_LEFT)) 
    				gameObject.setX( gameObject.getX() - 0.35 * delta);
     
    			if (MUGEGameVA1.isKeyDown(MUGEGameVA1.KEY_RIGHT)) 
    				gameObject.setX( gameObject.getX() + 0.35 * delta);
     
    			if (MUGEGameVA1.isKeyDown(MUGEGameVA1.KEY_UP)) 
    				gameObject.setY( gameObject.getY() + 0.35 * delta);
     
    			if (MUGEGameVA1.isKeyDown(MUGEGameVA1.KEY_DOWN)) 
    				gameObject.setY( gameObject.getY() - 0.35 * delta);
     
     
     
    				//IMPLEMENT THE COLLISON HERE????
     
     
    		}
    	}
    }




    //CLASS OBJECT
    import java.util.ArrayList;
    import java.util.Random;
     
    import org.lwjgl.opengl.GL11;
     
    public class GameObject
    {
     
     
    	private float x_,y_;//object world space coordinates
     
    	private float r_,g_,b_;//red, green, blue component colours
     
    	private ArrayList <GameObjectComponent> components_ = 
    			new ArrayList<GameObjectComponent>();  
     
    	private static int count_ = 0; // count of calls to constructor
     
    	/**
    	 * This is the default constructor. This will set all your world attributes
    	 * to some pre-determined values; i.e. x,y coordinates, colour. 
    	 */
    	public GameObject()
    	{
    		initialise_();
    		float[][] data = {{400, 300, 0.5f, 0.5f, 1.0f}, // player
    		  	  	 {100, 250, 0.5f, 0.25f, 0.5f},	  		// barrier1
    		  	  	 {200, 500, 0.5f, 0.75f, 0.5f}};	    // barrier2
    		x_ = data[count_][0];
    		y_ = data[count_][1];
    		r_ = data[count_][2];
    		g_ = data[count_][3];
    		b_ = data[count_][4];
    		count_++;
    	}
     
    	/**
    	 * This constructor allows you to pre-set your GameObject's coordinates
    	 * 
    	 *  @param  newX  the new x coordinate
    	 *  @param  newY  the new y coordinate
    	 */
    	public GameObject(float newX, float newY)
    	{
     
    		initialise_();
     
    		x_ = newX;
    		y_ = newY;
    	}
     
    	/**
    	 * This constructor allows you to pre-set your GameObject's coordinates
    	 * 
    	 *  @param  newX  the new x coordinate
    	 *  @param  newY  the new y coordinate
    	 *  @param  newR  the new value of the red component
    	 *  @param  newG  the new value of the green component
    	 *  @param  newB  the new value of the blue component
    	 */
    	public GameObject(float newX, float newY, 
    			           float newR, float newG, float newB )
    	{
    		initialise_();
     
    		x_ = newX;
    		y_ = newY;
     
    		setColour(newR,newG,newB);
    	}
     
     
    	/**
    	 * This constructor allows you to pre-set your GameObject's coordinates
    	 * and its colour.
    	 * 
    	 *  @param  newX  the new x coordinate
    	 *  @param  newY  the new y coordinate
    	 *  @param  newR  the new red component
    	 *  @param  newG  the new green component
    	 *  @param  newB  the new blue component
    	 */
    	public GameObject(double newX, double newY, 
    			           double newR,double newG, double newB )
    	{
    		initialise_();
     
    		x_ = (float)newX;
    		y_ = (float)newY;
     
    		setColour(newR,newG,newB);
    	}
     
    	public void setX(double newX)
    	{
    		x_ = (float)newX;
    	}
     
     
    	public void setY(double newY)
    	{
    		y_ = (float)newY;
    	}
     
     
    	public float getX()
    	{
    		return x_;
    	}
     
     
    	public float getY()
    	{
    		return y_;
    	}
     
     
    	public void setColour(double newR, double newG, double newB)
    	{
     
    		setColour((float)newR,(float)newG,(float)newB);
    .
    	}
     
     
    	/**
    	 * Sets the red, green and blue colour components all at once. 
    	 * 
    	 *  @param  newR  the new red component
    	 *  @param  newG  the new green component
    	 *  @param  newB  the new blue component
    	 */	
    	public void setColour(float newR,float newG, float newB)
    	{
    		float [] colArr = {newR,newG,newB};
     
    		r_ = newR;
    		g_ = newG;
    		b_ = newB;
     
    		//The r, g, b values need to be somewhere between 0 and 1.
    		//but there is another convention that allows them to be
    		//between 1 and 255. This takes care of that eventuality.
    		for(int i = 0; i < 3; i++)
    		{
    			if(colArr[i] > 1.0f)
    			{
    				if(colArr[i] <= 255)
    				{
    					setColour(i,(float)(colArr[i]/255.0f));
    				}
    				else	
    				{
    					setColour(i,1f);
    				}
    			}
    		}
    	}
     
    	/**
     
    	public void setColour(int channel, double value)
    	{
    		setColour(channel,(float)value);
    	}
     
    	/**
    	 * Sets the colour channels individually. 
    	 * 
    	 *  @param  channel  0 for red, 1 for green, 2 for blue.
    	 *  @param  value    this needs to be between 0 and 1
    	 */
    	public void setColour(int channel, float value)
    	{
     
    		if(value > 1f && value < 255f)
    		{
    			value = value/255f;
    		}
    		else if(value > 255f)
    		{
    			value = 1f;
    		}
     
    		switch(channel)
    		{
    			case 0:
    			{
    				r_ = value;
    				break;
    			}
     
    			case 1:
    			{
    				g_ = value;
    				break;
    			}
     
    			case 2:
    			{
    				b_ = value;
    				break;
    			}
    		}
    	}
     
    	/**
    	 * Gets the colour value of a certain channel.
    	 * 
    	 *  @param  channel  0 for red, 1 for green, 2 for blue
    	 *  @return          the value of the specified colour channel 
    	 */
    	public float getColour(int channel)
    	{
    		switch(channel)
    		{
    			case 0:
    			{
    				return r_;
    			}
     
    			case 1:
    			{
    				return g_;
    			}
     
    			case 2:
    			{
    				return b_;
    			}
    		}
    		return -1; // error value
    	}
     
    	/**
    	 * Randomises the attributes of the GameObject
    	 * 
     
    	public void randomiseDeltas(int newSeed)
    	{
    		 Random rnd = new Random();
    		 rnd.setSeed(newSeed); 
     
    		 r_ = rnd.nextFloat();
    		 g_ = rnd.nextFloat();
    		 b_ = rnd.nextFloat();
    	}
     
    	/**
    	 * Adds a component to the game object
    	 * @param newComp the component to add
    	 */
    	public void addComponent(GameObjectComponent newComp)
    	{
    		components_.add(newComp);
     
    		//make sure the component itself knows who it belongs to
    		newComp.gameObject = this;
    	}	
     
    	public void draw()
    	{
    		// draw quad
    		GL11.glPushMatrix();
    			GL11.glColor3f(r_, g_, b_);
    			GL11.glTranslatef(x_, y_, 0);
    			GL11.glTranslatef(-x_, -y_, 0);
    			GL11.glBegin(GL11.GL_QUADS);
    				GL11.glVertex2f(x_ - 50, y_ - 50);
    				GL11.glVertex2f(x_ + 50, y_ - 50);
    				GL11.glVertex2f(x_ + 50, y_ + 50);
    				GL11.glVertex2f(x_ - 50, y_ + 50);
    			GL11.glEnd();
    		GL11.glPopMatrix();
    	}
     
    	/**
    	 * This function is responsible for object movement, rotations and
    	 * other object behaviours. This function should ideally be called
    	 * once per frame. But that depends on the engine that's using it.
    	 * 
     
    	public void update(int delta)
    	{
    		//move the object, step by step
    		//x += dx * delta;
    		//y += dy * delta;
     
     
    		//now run through all the components and do them
    		for(int i = 0; i < components_.size(); i++)
    		{
    			components_.get(i).update(delta);
    		}
    	}
     
     
     
    	private void initialise_()
    	{
    		x_ = 0f;
    		y_ = 0f;
     
    		r_ = 1f;
    		g_ = 1f;
    		b_ = 1f;
    	}
     
    }
    Last edited by Dreamer999; August 30th, 2012 at 08:22 AM.


  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: Simple Collision with no Rect package import

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

    how to make the playerObject change color
    How do you chose the playerObject's color? Add some logic that will change the color when a collision is detected.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple Collision with no Rect package import

    Thnx norm, its the collision detection that I'm not too sure about.

  4. #4
    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: Simple Collision with no Rect package import

    Do you want to do the detection yourself or could you use methods from Java classes like the Shape interface?

    If you want to do it, take a piece of graph paper and draw two shapes in all the possible ways they can collide and work out the tests that need to be made using their x,y locations and boundaries.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Simple dynamic-static circle collision response
    By tukhes in forum Java Theory & Questions
    Replies: 3
    Last Post: October 10th, 2011, 11:54 AM
  2. So im making a freehand Line,Rect,Oval; but problems
    By Matta in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 20th, 2011, 07:25 AM
  3. Simple package problem, package does not exist error
    By Farmer in forum Object Oriented Programming
    Replies: 3
    Last Post: August 23rd, 2011, 11:18 AM
  4. how to compile java and import it as package
    By clementnas in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 19th, 2011, 05:53 AM
  5. Simple import problem
    By Mekster in forum Java IDEs
    Replies: 3
    Last Post: November 13th, 2009, 09:17 PM