Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Sight for an enemy  RSS feed

 
Ranch Hand
Posts: 81
Android Chrome Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an Enemy class for a game I am making from scratch. I have been pondering it for a while, but I need to make the enemy able to see the player. Specifically, I need to make it so that the enemy can test if there are any walls between it and the player. my code is kinda long (350 lines to be exact) and I haven't made anything in the enemy class yet.  I am not asking for code, but that is appreciated. I am just asking how I would go about doing this. The maps are randomly generated, and I use a 2d array to store the map. As for the enemy coordinates, I use a variable, and then it paints the enemy on the map at those coordinates. Thanks in advance.

Links to the code:
Main.java
Enemy.java
 
Rocky Rocha
Ranch Hand
Posts: 81
Android Chrome Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I thought maybe I could be more clear. I need to figure out a way to draw a line between the monster and the player. Then I need to test to see if there are any walls along that line. A way to move the monster along the most efficient path would also be appreciated.
 
Bartender
Posts: 9569
189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recommend you use an existing game engine to do this for you.

If you want to code this from scratch as a learning experience, you should check out the sub-classes of java.awt.Shape. You can easily create a line segment between an enemy and your player using one of the java.awt.geom.Line2D.Double constructors. If your walls are implemented using java.awt.Polygon, you can check if the line intersects the wall by constructing a java.awt.geom.Area from both, subtracting the line area from the wall area and then checking if the remainder is unequal to the wall's original area. You can limit the number of walls to perform this check for by first filtering out the walls that don't fall into a crude bounding box defined by the line of sight.

For path finding, you can implement an A* search algorithm that uses wall vertices or predefined "passage ways" as nodes for the algorithm.
 
Rocky Rocha
Ranch Hand
Posts: 81
Android Chrome Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your advice. I didn't consider this as I don't normally use java.awt in alot of things. I will try this out and see how it works.
 
Rocky Rocha
Ranch Hand
Posts: 81
Android Chrome Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

is this how you would do it?
 
Stephan van Hulst
Bartender
Posts: 9569
189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Almost.

First of all, if all your walls are rectangular in shape, use Rectangle2D.Double instead of Polygon. Secondly, you're not testing whether the line intersects the wall, you're testing whether the line intersects the bounding box of the wall. That's fine if your walls are always rectangular in shape, and you never rotate them, but otherwise it is a big mistake:



If the red lines represent your wall, the black lines on the outside represent the bounding box.

Thirdly, as the documentation for intersects() states, it's not precise and you're better off using the other methods of the Area class. Like I said, intersect() the line and the wall's areas, and if the result is empty, the two areas don't intersect.
 
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!