• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Gearslayer360

Java Collision Detection Help

3 posts in this topic

I copied the code from Eclipse because the adding it seemed to cut it off after 60 lines. I can fix it if you tell me how and you want it in the special box. Anyways this is the enemy class in my game. Currently what I have is a little tank that moves and shoots bullets at an enemy(moving square). When he shoots the enemy health decreases, it reverses the enemy direction and the score goes up by 100. My problem is basically that the collision detection seems to be random...Sometimes it will count the bullet as 1 hit...sometimes 2 or 3. I don't know why it's doing this. Can anyone help or give suggestions? All help appreciated. Thank you.


package game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;
public class Enemy implements Runnable
{
int x;
int y;
int xDirection;
int yDirection;
int health;
int p1Score;

Player p1 = new Player(290, 15, 10);
Rectangle enemy;

public Enemy(int x, int y, int health)
{
p1Score = 0;
this.x = x;
this.y = y;
this.health = health;

Random r = new Random();
int rDir = r.nextInt(1);

if(rDir == 0)
{
rDir--;
setXDirection(rDir);
}

int yrDir = r.nextInt(1);

if(yrDir == 0)
{
yrDir--;
setYDirection(yrDir);
}

enemy = new Rectangle(this.x, this.y, 30, 30);
}


public void setXDirection(int direction) {
xDirection = direction;
}
public void setYDirection(int direction) {
yDirection = direction;
}

public void draw(Graphics g)
{
//draw the enemy initially
g.setColor(Color.white);
g.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);

//if the enemy dies remove it
//Need to delete the current one and make a new enemy
if(health <= 0)
{
g.clearRect(enemy.x, enemy.y, enemy.width, enemy.height);
}
}

public void collision()
{
//Checks first if the player bullet is null
//Collision between the bullet and the player.
//Checks if the bounds of bullet and enemy intersect


if(p1.bullet != null && enemy.getBounds().contains(p1.bullet.getBounds()))
{
setYDirection(-1);
p1Score+=100;
health-=1;
}

}

public void move()
{
//enemy.x+=xDirection;
enemy.y+=yDirection;


//Checks if the enemy hits the left side of the screen
//if so reverse the direction to the right
if(enemy.x <= 0)
{
setXDirection(1);
}

//Checks if the enemy hits the right side of the screen
//if so reverse the direction to the left
if(enemy.x >= 780)
{
setXDirection(-1);
//Add to score
p1Score++;
}

//Checks if the enemy hits the top of the screen
//if so reverse the direction to the bottom
if(enemy.y <= 30)
{
setYDirection(1);

}

//Checks if the enemy hits the bottom of the screen
//if so reverse the direction to the top
if(enemy.y >= 570)
{
setYDirection(-1);

}
//collision();

}

public void run()
{
try
{
while(true)
{
move();
collision();
Thread.sleep(3);
}
}
catch(Exception e){System.err.println(e.getMessage());}
}

public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getP1Score() {
return p1Score;
}
public void setP1Score(int score) {
p1Score = score;
}
public Player getP1() {
return p1;
}
public void setP1(Player p1) {
this.p1 = p1;
}
public Rectangle getEnemy() {
return enemy;
}
public void setEnemy(Rectangle enemy) {
this.enemy = enemy;
}
public int getXDirection() {
return xDirection;
}
public int getYDirection() {
return yDirection;
}

} Edited by Gearslayer360
0

Share this post


Link to post
Share on other sites
perhaps yuo dont erase bullet imediately afer hiting something...
From this code just whis idea comes...
2

Share this post


Link to post
Share on other sites
Ah you could be right. I could probably do that but I don't know how to destroy it. I have looked on the internet but I haven't really found any good information. Any suggestions on how to do it?
0

Share this post


Link to post
Share on other sites
Thank you for the suggestion. I found a way to implement the destroying of the bullet upon impact. It now works correctly.
0

Share this post


Link to post
Share on other sites

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  
Followers 0