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
}
}
