Jump to content
  • Advertisement
Sign in to follow this  
Cataract

Collision Overlay or Object->Object Collision?

This topic is 1418 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

I just started making a Game with Java and i got stuck at the collision.

Because i want to make a strategie game and not a platformer i thought about creating a collisionlayer, which is nothing else than a 2D int-array representing the information if there's something "occupying" the point x,y.

 

So everytime an object gets successfully moved i erase its collisioninfo at the old point and write it at the new point.

 

The Obstracle-class has a rectangle-shaped collision and the Moveable-class has a circular-shaped collision.

	final double  COL_SCALE = 1.0;
	
	public void calculateCollision(int newx , int newy ,Widget w){
		double x = newx / COL_SCALE;
		double y = newy / COL_SCALE;
		double cord_x = w.cord_x / COL_SCALE;
		double cord_y = w.cord_y / COL_SCALE;
		
		if (w instanceof Obstracle){
			Obstracle o = (Obstracle) w;
			double col_x = o.col_x / COL_SCALE;
			double col_y = o.col_y / COL_SCALE;
			
			double cos = Math.cos(Math.toRadians(o.angle));
			double sin = Math.sin(Math.toRadians(o.angle));
			
			for (int i = 0 ; i <= col_x; i++){
				for (int k = 0 ; k <= col_y; k++){
					double temp_i = i*cos - k*sin;
					double temp_k = i*sin + k*cos;
					collision_layer[(int) (cord_x+temp_i)][(int) (cord_y+temp_k )] -= o.col_height;
					collision_layer[(int) (cord_x+temp_k)][(int) (cord_y-temp_i )] -= o.col_height;
					collision_layer[(int) (cord_x-temp_k)][(int) (cord_y+temp_i )] -= o.col_height;
					collision_layer[(int) (cord_x-temp_i)][(int) (cord_y-temp_k )] -= o.col_height;
				}
			}
			
			for (int i = 0 ; i <= col_x; i++){
				for (int k = 0 ; k <= col_y; k++){
					double temp_i = i*cos - k*sin;
					double temp_k = i*sin + k*cos;
					
					collision_layer[(int) (x+temp_i)][(int) (y+temp_k )] += o.col_height;
					collision_layer[(int) (x+temp_k)][(int) (y-temp_i )] += o.col_height;
					collision_layer[(int) (x-temp_k)][(int) (y+temp_i )] += o.col_height;
					collision_layer[(int) (x-temp_i)][(int) (y-temp_k )] += o.col_height;
				}
			}				
		}
		
		
		
		if (w instanceof Moveable){		
			Moveable m = (Moveable) w;
			double col = m.col / COL_SCALE;	
			
			for (int i = 0 ; i<=col; i++){
				for (int k = 0 ; k <= col; k++){
					if( Math.pow(i, 2) +  Math.pow(k, 2) <= col*col){
						collision_layer[(int) (cord_x+i)][(int) (cord_y+k)] -= m.col_height;
						collision_layer[(int) (cord_x+i)][(int) (cord_y-k)] -= m.col_height;
						collision_layer[(int) (cord_x-i)][(int) (cord_y+k)] -= m.col_height;
						collision_layer[(int) (cord_x-i)][(int) (cord_y-k)] -= m.col_height;
					}
				}
			}
			
			for (int i = 0 ; i<=col; i++){
				for (int k = 0 ; k <= col; k++){
					if( Math.pow(i, 2) +  Math.pow(k, 2) <= col*col){
						collision_layer[(int) (x+i)][(int) (y+k)] += m.col_height;
						collision_layer[(int) (x+i)][(int) (y-k)] += m.col_height;
						collision_layer[(int) (x-i)][(int) (y+k)] += m.col_height;
						collision_layer[(int) (x-i)][(int) (y-k)] += m.col_height;
					}
				}
			}
		}
	}

This is working (more or less).
So now i can ask with "collision_layer[(int) (x/COL_SCALE)][(int) (y/COL_SCALE)]" if there's something "occupying" the point.

 

Now to my problem: 

I want a function with which i can "teleport" an object to a point and if this point is "occupied" by something it should find the nearest point where the object can be teleported to (regarding the collisionsize of the object).

 

Because i can't think of a solution which isn't a lot of calculations with a lot of loops and rounding-errors i have doubts that the whole collisionoverlay idea is a good solution. But how can i write the "teleport" function with "normal" collisionsystems like AABB?

Thanks for your help

Cataract

 

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!