Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Desperate Need of help

1. ## Desperate Need of help

Hi to anyone that is reading this, and thank you for taking the time to do so. I have been trying to get some code figured out and im going out of my mind.
THE GOAL: Have a playerobject (square) and a enemyobject(square) and get a vector between them to determine if they are in line of sight of each other. (there maybe terrain objects, also square in the way). All of these objects are derived from the same "Entity" super class. (I WISH I WAS BETTER AT MATH FOR THIS) Here is my code so far, it just determines if the player is close enough to the enemy for the enemy to pay attention to him.

I think the proper way to do this.. is to create a line between the player and enemy, get all the terrainobjects that are in bounds of the rectangle of possibles, then check the objects to see if any of thier boundries are touching the line between the player and enemy. Sadly this is beyond my skills.. can someone please help me get started. AND/Or tell me if it is possible to make a 360 degree projectile but using graphics.Drawline() with the vector line between the player and enemy.

public void creatureShoot() {
CreatureEntity creature;
int xDistance, yDistance;
int playerX, playerY, creatureX, creatureY, totalDistance;
for (int j = 0; j < currentCreatureObj.size(); j++) {
creature = (CreatureEntity) currentCreatureObj.get(j);
//get coords of the center of the sprites
playerX = (int) (playerEnt.getX() + (playerEnt.getWidth()) / 2);
playerY = (int) (playerEnt.getY() + (playerEnt.getHeight()) / 2);
creatureX = (int) (creature.getX() + (creature.getWidth()) / 2);
creatureY = (int) (creature.getY() + (creature.getHeight()) / 2);

xDistance = Math.abs(playerX - creatureX);
yDistance = Math.abs(playerY - creatureY);
totalDistance = (int) Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2));

if (totalDistance <= SHOOTING_RANGE) {
String ref = "null.jpg";
//NEED TO CHANGE THIS TO 360 DEGREE SHOOTING
int direction = (int) creature.getLastMovementDirection();
if (direction == Entity.NORTH || direction == Entity.SOUTH) { ref = "verticalmissile.gif"; }
else if (direction == Entity.WEST || direction == Entity.EAST) { ref = "horizontalmissile.gif"; }
System.out.println(totalDistance + " creature in range of player, should shoot now");
//CREATE A MISSLE OBJ IN THE LAST DIRECTION THE MONSTER WAS FACING.. NEEDS TO SWITCH TO 360 DEGREE VECTOR TO PLAYER
currentMissileObj.add(new Missile(ref, creature.getX() + creature.getWidth() / 2, creature.getY() + creature.getHeight() / 2, direction));
}
}
}

2. ## Re: Desperate Need of help

i figured out that what i need is to use the CohenSutherland method to check if my ArrayList of obstacles touch a line, i just need some help with implementationn

3. ## Re: Desperate Need of help

1. Use code tags to enclose your tag.
2. Use some editor to format the code.