Sign in to follow this  

[java] Copying variables?

This topic is 3488 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

It seems in Java, different than C++, it doesn't copy a variable, but makes a pointer. In the example below, 100(the y value) gets added to otherPointY1. I don't want to modify the y value, just get a copy of the value. The following code is to check collision, works perfectly in C++. How do I copy values in Java?
	public void checkCollision(List<Entity> objects) {
		ListIterator<Entity> itr = objects.listIterator();
		
		while(itr.hasNext()) {
			Entity entity = itr.next();
			
				
			Rectangle thisColBox = this.getCollisionBox();
			Rectangle otherColBox = entity.getCollisionBox();
				
			// Collision Boxes are reletive to the entitie's location
			thisColBox.x = thisColBox.x + this.location.x;
			thisColBox.y = thisColBox.y + this.location.y;
			otherColBox.x = otherColBox.x + entity.getX();
			otherColBox.y = otherColBox.y + entity.getY();
			
			// X Projection
			int thisPointX1 = thisColBox.x;
			int thisPointX2 = thisColBox.x + thisColBox.width;
			
			int otherPointX1 = otherColBox.x;
			int otherPointX2 = otherColBox.x + otherColBox.width;
				
			// Y Projection
			int thisPointY1 = thisColBox.y;
			int thisPointY2 = thisColBox.y + thisColBox.height;
				
			int otherPointY1 = otherColBox.y;
			int otherPointY2 = otherColBox.y + otherColBox.height;
				
			if(thisPointX1 > otherPointX2 ||
			    thisPointX2 < otherPointX1 ||
				thisPointY2 < otherPointY1 ||
				thisPointY1 > otherPointY2 ) {
				
				System.out.println("Not Collision!" + otherPointY1 + " - " + thisPointX1);
				return;
			} else {
				// We collided
				System.out.println("Collision!");
				inCannon = true;
				//parent = entity;
			}
				
		}
	}


Share this post


Link to post
Share on other sites
Quote:

It seems in Java, different than C++, it doesn't copy a variable, but makes a pointer.


True, but only for non-primitive types.

Quote:

In the example below, 100(the y value) gets added to otherPointY1.


I don't see anywhere any addition to otherPointY1.

Quote:

I don't want to modify the y value, just get a copy of the value.


If you're using primitive types, which you are (int), then using the assignment operator will return a copy of the y-value, not the y-value itself.

For example,

int x = 10;
int y = x;

++x;

System.out.println(x); // Prints 11
System.out.println(y); // Prints 10


BUT

If you're working with Rectangles in Java, you don't need to write an intersection method - the method intersects is defined for Rectangles.

All you need to do is this:


public void checkCollision(List<Entity> objects) {
ListIterator<Entity> itr = objects.listIterator();

while(itr.hasNext()) {
Entity entity = itr.next();


Rectangle thisColBox = this.getCollisionBox();
Rectangle otherColBox = entity.getCollisionBox();

if(thisColBox.intersects(otherColBox)){
// Collision
}
else
// No collision
}
}


Share this post


Link to post
Share on other sites
Quote:
Original post by Karan Bhangui
Most classes have a clone() method.


Correct, classes have a clone() method, but primitives do not (and he's working with primitives).

Share this post


Link to post
Share on other sites
It is still adding the Y value to it self...

otherColBox.y = otherColBox.y + entity.getY();

I want to make a copy of the other collision box, then modify the local copy so that it is relative to the entity's location.


while(itr.hasNext()) {
Entity entity = itr.next();


Rectangle thisColBox = this.getCollisionBox();
Rectangle otherColBox = entity.getCollisionBox();

thisColBox.x = thisColBox.x + this.location.x;
thisColBox.y = thisColBox.y + this.location.y;

otherColBox.x = otherColBox.x + entity.getX();
otherColBox.y = otherColBox.y + entity.getY();

if(thisColBox.intersects(otherColBox)) {
// We collided
System.out.println("Collision!");
inCannon = true;
//parent = entity;
}

System.out.println(otherColBox.y);

}

Share this post


Link to post
Share on other sites
I'm not exactly sure what you're trying to do, but if you just want to check if any 2 entities in your game have collided, then do this:

1) Give every entity a position (x, y) and an image with width and height
2) Define a method getBoundingBox() for each of these entities.
That method will look like this:

Rectange getBoundingBox(){
// Rectangle(int x, int y, int width, int height);
// (x, y) specify upper left corner of rectangle
// width, height specify the width and height of the rectangle
return new Rectangle(myPosition.x, myPosition.y, myImage.width, myImage.height);
}



Then, iterate over all your Entities and grab their bounding box and compare with this.getBoundingBox(), like so:


public void checkCollision(List<Entity> objects) {
ListIterator<Entity> itr = objects.listIterator();

while(itr.hasNext()) {
Entity entity = itr.next();

Rectangle thisBox = this.getBoundingBox();
Rectangle otherBox = entity.getBoundingBox();

// Check for collision
if(thisBox.intersects(otherBox)){
// Collision
else {
// No collision
}
}



Quote:

I want to make a copy of the other collision box


To make a copy of the other collision box, simply call the clone() method:

Rectangle copyOfOtherBox = otherBox.clone();

Now, changes made to copyOfOtherBox will not be applied to otherBox.

Quote:

...then modify the local copy so that it is relative to the entity's location.


Now you have your copy - copyOfOtherBox - so do with it what you please. I'm guessing this step is where you're having some trouble, so decide how exactly you want to modify it relative to the entity's location and if you can't get it working make another reply showing exactly what you want to do.


Share this post


Link to post
Share on other sites
	
// temporary rectangle
Rectangle curr = new Rectangle();

// our rectangle
Rectangle self = new Rectangle(getCollisionBox());
// offset it to absolute position
self.translate(location.x, location.y);

while(itr.hasNext()) {
Entity entity = itr.next();

// get entity relative position
curr.setBounds(entity.getCollisionBox());
// translate it into absolute coordinates
curr.translate(entity.getX(), entity.getY());

if(self.intersects(curr)) {
// We collided
System.out.println("Collision!");
inCannon = true;
}
System.out.println(otherColBox.y);
}

Share this post


Link to post
Share on other sites

This topic is 3488 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this