collision with rectangle bounds.[solved]

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

collision with rectangle bounds.[solved]

Postby djgriff » Tue Jun 26, 2012 11:39 am

Hi all, and thanks for the patience some of you have shown with my posts.

I am trying to implement collision detection between an object that falls(raindrop) and hits a boat.

I have followed an example of a rain falling and hitting a bucket, and have pretty much similar code, except the boat sits on water that moves up the screen.

I have managed to get the water to remove the rain drop and in doing so move the water up 20 pixels for every rain drop that hits the water. but when the rain drop hits( not happening for some reason) the boat, i want the water to move down the screen at 30 pixels.

I have setup the code so the boat sits on the top of the water and moves with the water as it goes up the screen, but when a rain drop falls it goes through the boat rectangle, hits the water rectangle bounds and moves the water further up .

so for some reason the boat rectangle bounds are not interacting with the rain drop rectangle bounds.

I have recently posted on the forum and got clarification about OOP and the basics, but i can't see that there is an issue with that to my knowledge.

So yeah i am kind of perplexed about this one..

If anyone can point out or suggest where i am going wrong i would much appreciate it.

hope you can understand what i am trying to do..

Many Thanks

Daniel

The code is below:

Code: Select all
import java.util.Iterator;
import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;

public class Thing implements Screen {

   MainThing g;
   public OrthographicCamera cam;
   public SpriteBatch batch;
   public Texture t;
   public TextureRegion tR;
   
   public Texture tt;
   public TextureRegion ttR;
   
   public Texture cloud;
   public TextureRegion cloudR;
   
   public Texture boat;
   public TextureRegion boatR;
   
   Array<Rectangle> raindrops;
   long lastDropTime;
   
   Rectangle cloudRec;
   
   Texture rainDrop;
   TextureRegion rainDropR;
   
   Rectangle water;
   
   Rectangle boatRec;
   
   Random rand;
   
   float moveY;
   
   public Thing(MainThing g){
      this.g=g;
      cam = new OrthographicCamera(480,800);
      cam.position.set(480/2, 800/2, 0);
      batch = new SpriteBatch();
      t = new Texture(Gdx.files.internal("bg.png"));
      tR = new TextureRegion(t,0,0,480,700);
      
      tt = new Texture(Gdx.files.internal("bgCloud.png"));
      ttR = new TextureRegion(tt,0,0,480,350);
      
      cloud = new Texture(Gdx.files.internal("cloud.png"));
      cloudR = new TextureRegion(cloud,0,0,256,140);
      
      boat= new Texture(Gdx.files.internal("boat.png"));
      boatR = new TextureRegion(boat,0,0,64,32);
      
      rainDrop = new Texture(Gdx.files.internal("RainDrop_sml.png"));
      
      rand = new Random();
      
      moveY = 0;
      
      
      
      
      raindrops = new Array<Rectangle>();
      spCloud();
      
      
      
      
      water = new Rectangle();
      water.x = 0;
      water.y = -700;
      water.width = 480;
      water.height = 700;
      
      boatRec = new Rectangle();
      boatRec.x = cam.viewportWidth / 2 ;
      boatRec.y = 0;
      boatRec.height = 32;
      boatRec.width = 64;
      
      spawnRaindrop();
      
      
   }
   private  void spCloud(){
      cloudRec = new Rectangle();
      cloudRec.x = cam.viewportWidth /2 - 256 / 2;
      cloudRec.y = 600;
      cloudRec.height = 140;
      cloudRec.width = 256;
   }
   
   private void spawnRaindrop() {
         Rectangle raindrop = new Rectangle();
         raindrop.x = MathUtils.random(cloudRec.x+32, cloudRec.x + cloudRec.width -32);
         raindrop.y = cloudRec.y;
         raindrop.width = 16;
         raindrop.height = 16;
         raindrops.add(raindrop);
         lastDropTime = TimeUtils.nanoTime();
      }
   
   
      
   
   
      
   
   
   @Override
   public void render(float delta) {
      Gdx.gl.glClearColor(1, 1, 1, .05f);
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
      cam.update();
      batch.setProjectionMatrix(cam.combined);
      batch.begin();
      batch.draw(ttR,0,450,480,350);
      batch.end();
      
      
      batch.begin();
      
      
      batch.draw(boatR, boatRec.x, moveY + boatRec.y);
      
      
      for(Rectangle raindrop: raindrops) {
            batch.draw(rainDrop, raindrop.x , raindrop.y);
         }
      
      
      
      batch.draw(cloudR, cloudRec.x, cloudRec.y, cloudRec.width, cloudRec.height);
      
      
      
      
      
      if(moveY < cam.viewportHeight -200){
         moveY += 50  * Gdx.graphics.getDeltaTime();
      }else
      {
         moveY = 0;
      }
      batch.draw(tR, water.x, moveY + water.y , water.width, water.height);
      
      batch.end();
      
      if(Gdx.input.isTouched()) {
            Vector3 touchPos = new Vector3();
            touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
            cam.unproject(touchPos);
            boatRec.x = touchPos.x - 64 / 2;
         }
         if(Gdx.input.isKeyPressed(Keys.LEFT)) boatRec.x -= 200 * Gdx.graphics.getDeltaTime();
         if(Gdx.input.isKeyPressed(Keys.RIGHT)) boatRec.x += 200 * Gdx.graphics.getDeltaTime();
        
         // make sure the bucket stays within the screen bounds
         if(boatRec.x < 0) boatRec.x = 0;
         if(boatRec.x > cam.viewportWidth - 64) boatRec.x = cam.viewportWidth - 64;
      
      
      if(TimeUtils.nanoTime() - lastDropTime > 1000000000) spawnRaindrop();
      
       Iterator<Rectangle> iter = raindrops.iterator();
         while(iter.hasNext()) {
            Rectangle raindrop = iter.next();
            raindrop.y -= 200 * Gdx.graphics.getDeltaTime();
           if(raindrop.y < moveY -64) {
              moveY += 20;
                 iter.remove();
           }
              
              
            if(raindrop.overlaps(boatRec)) {
                 moveY -= 30 ;
                  iter.remove();
              }
         }
   }
   
        
        
        
      
   
   

   @Override
   public void resize(int width, int height) {
      // TODO Auto-generated method stub

   }

   @Override
   public void show() {
      // TODO Auto-generated method stub

   }

   @Override
   public void hide() {
      // TODO Auto-generated method stub

   }

   @Override
   public void pause() {
      // TODO Auto-generated method stub

   }

   @Override
   public void resume() {
      // TODO Auto-generated method stub

   }

   @Override
   public void dispose() {
      // TODO Auto-generated method stub

   }

}
Last edited by djgriff on Wed Jun 27, 2012 11:09 am, edited 1 time in total.
djgriff
 
Posts: 261
Joined: Tue Jan 03, 2012 8:11 am

Re: collision with rectangle bounds.

Postby soronthar » Tue Jun 26, 2012 9:23 pm

Without running the code, my bet would be that the coordinates for raindrop and the boat have a different point of reference (perhaps raindrop has coordinates relative to the camera, and the boat has the coordinates relative to the screen).
soronthar
 
Posts: 7
Joined: Tue Jun 12, 2012 7:06 am

Re: collision with rectangle bounds.

Postby soronthar » Tue Jun 26, 2012 9:55 pm

ok, I kind of lost the bet :D It turns out that you are never updating the boat y-coordinate. That''s why it always fails to detect the overlap. Change

Code: Select all
batch.draw(boatR, boatRec.x, moveY + boatRec.y);


to

Code: Select all
batch.draw(boatR, boatRec.x, boatRec.y);


And add

Code: Select all
 boatRec.y=moveY+water.y;


at the end of the render method. That should solve your current problem.

After that, get rid of the moveY variable and increase or decrease water.y instead. It will be faster, and the code will be cleaner. Of course, you'll need to implement collision detection between raindrops and the water, but that will be a piece of cake now ;)

I strongly recommend you to read about MVC.

good luck and happy coding.
soronthar
 
Posts: 7
Joined: Tue Jun 12, 2012 7:06 am

Re: collision with rectangle bounds.

Postby djgriff » Wed Jun 27, 2012 9:06 am

Thanks for the reply i will implement it right away and let you know how i get on .Cheers

Best

Daniel
djgriff
 
Posts: 261
Joined: Tue Jan 03, 2012 8:11 am

Re: collision with rectangle bounds.

Postby djgriff » Wed Jun 27, 2012 9:36 am

hi I have implemented the code you suggested but it doesn't seem to want to work. By This i mean, The rain drops have stopped falling, and the boat image stays on 0 y axis. it seems the y is not being updated by the moveY. Also as the water image is 700px i have to off set the water.y by -700 to make the image move upwards. but by setting the boat image to moveY + water.y it puts the boat on the bottom of the water image. some thing is not happening correctly.. i will try re jigging all the code into separate classes so it is more logical and then get rid of the moveY and change it to water.y as suggested.

will let you know how i get on.

Best

Daniel
djgriff
 
Posts: 261
Joined: Tue Jan 03, 2012 8:11 am

Re: collision with rectangle bounds.

Postby djgriff » Wed Jun 27, 2012 11:08 am

Woohoo, right for all of you who might have the same issue or want to know how it was resolved here goes..

basically the moveY loop for incrementing the Rectangle on the Y axis will only work with one rectangle. you have to increment them individually for each rectangle y position.

This has solved my issue.

code below

Thanks for the suggested help though.

Thanks

Daniel


Code: Select all
if(water.y+700  < cam.viewportHeight -200 ){
         water.y += 20  * Gdx.graphics.getDeltaTime();
         boaty.y += 20  * Gdx.graphics.getDeltaTime();
      }else
      {
         boaty.y = 0;
         water.y = -700;
      }
djgriff
 
Posts: 261
Joined: Tue Jan 03, 2012 8:11 am


Return to General Development

Who is online

Users browsing this forum: No registered users and 3 guests

cron