noobHelp: Simple Collision detection help.
in
Programming Questions
•
2 years ago
Hey guys,
So I'm creating a game for my first game design class at uni in processing. Where it is currently at is an object "sheep" is moving from the centre and must be collected by the object "truck" which is controlled by the player along a half diameter. Eventually I will make it so there are two players facing each other collecting sheep and firing them but for now I am struggling just being able to get the collection working. Any suggestions or direction would be greatly appreciated.
The biggest problem I think with what is going on is in the truck class in the captureSheep boolean. I don't know the proper parameters I should be placing in to overlap each other when one is translating and rotating and the other is just moving straight out.
Truck rPlayer;
int rightScoreBoard = 0;
ArrayList sheepList;
void setup() {
size(800,800);
smooth();
rPlayer = new Truck(350);
sheepList = new ArrayList();
sheepList.add(new Sheep());
}
void draw() {
background(#16DB09);
translate(width/2, height/2);
for(int i = 0; i <sheepList.size(); i++){
Sheep tempSheep = (Sheep)sheepList.get(i);
tempSheep.sheepUpdate();
if(rPlayer.captureSheep(tempSheep)){
sheepList.remove(i);
}
}
rPlayer.movement();
}
void keyPressed() {
rPlayer.keyPressed();
}
//---------------------------------------------------------------
//----------------------CLASS_TRUCK------------------------------
//---------------------------------------------------------------
class Truck {
float posX,posY;
float angle;
float speed = 0;
float playerSide;
float truckWidth, truckLength;
Truck(float _playerSide) {
playerSide = _playerSide;
truckWidth = 80;
truckLength = 120;
}
void movement() {
angle = angle + speed;
angle = constrain(angle, -75, 75);
rectMode(CENTER);
rotate(radians(angle));
posX = playerSide;
println(posX);
translate(playerSide, 0);
rect(0, 0, truckWidth, truckLength);
println(posX + angle);
}
boolean captureSheep(Sheep tempSheep) {
if (tempSheep.posX + tempSheep.yBody >= 350){
rightScoreBoard = rightScoreBoard + 1;
println(rightScoreBoard);
return true;
}
else{
return false;
}
}
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
speed = -1.2;
}
if (key == CODED) {
if (keyCode == DOWN) {
speed = 1;
}
}
}
}
}
//---------------------------------------------------------------
//------------------SHEEP_GENERATOR------------------------------
//---------------------------------------------------------------
class Sheep {
float xBody, yBody;
float angle;
float speed;
float posX, posY;
float xDirection, yDirection;
Sheep() {
angle = random(0,TWO_PI);
xBody = 70;
yBody = 55;
speed = 3;
xDirection = cos(angle) * speed;
yDirection = sin(angle) * speed;
}
void sheepUpdate() {
pushMatrix();
posX = xDirection + posX;
posY = yDirection + posY;
translate(posX, posY);
rotate(angle);
ellipse(0,0,xBody,yBody);
popMatrix();
// println(posX + posY);
}
}
Thanks for any help guys. : )
1