Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2005

Tip: Looking for answers? Try searching our database.

How to see if an object is part of a specific vector?

Thread view: 
caleb - 30 May 2005 22:03 GMT
I'm making a space invader-like game, and for one method
getLocationOfNearestAlien, which lasers use in part to see if they are
touching an alien, I need some help.

public NearestAlienInfo getLocationOfNearestAlien(double x, double y) {
       if (aliens.isEmpty()) {
           return new NearestAlienInfo((int) x, (int) y, 100000000);
       }
       double closestDistanceSoFar = 100000000;
       Alien closestAlien = null;
       double d =0;
       double e =0;
       for(int i=0; i<aliens.size(); i++) {
           Alien a = (Alien) aliens.elementAt(i);
           if(a.contains(ufos)) {
               for(int z=0; z<ufos.size(); z++) {
                   Ufo u = (Ufo) ufos.elementAt(i);
                   d = u.getSizeX() /2;
                   e = u.getSizeY() /2;
               }
           }
           else if(a.contains(smartAliens)) {
               for(int z=0; z<smartAliens.size(); z++) {
                   SmartAlien s = (SmartAlien)
smartAliens.elementAt(i);
                   d = s.getSizeX() /2;
                   e = s.getSizeY() /2;
               }
           }
// doubles b and c should point to a pixel in the center of the alien
           double b = a.getX() + d;
           double c = a.getY() + e;
//this is the actual ammt for Ufo before I decided I would need to make
it more flexible for more alien types.
           //double b = a.getX() + 32; //that's half of the Ufo x
pixels
           //double c = a.getY() + 18.5; // and half of the Ufo y
pixels
           double dx = x - b;
           double dy = y - c;
           double distance = Math.sqrt(dx*dx + dy*dy);
           if (distance < closestDistanceSoFar) {
               closestDistanceSoFar = distance;
               closestAlien = a;
           }
       }
       return new NearestAlienInfo(closestAlien.getX(),
closestAlien.getY(),
        closestDistanceSoFar);
   }

There are different aliens. And each has a different size image that
I'm using. The Alien class is the basic class that the class Ufo and
SmartAlien extend. The Vector aliens includes all aliens but I want the
if statements to work so that if the a object is also part of ufos (not
just part of aliens), then it will add it's pixel size divided by two
and then add that number to where it is so you end up with a pixel
right in the middle of the Ufo. If it is not a Ufo I want it to check
if it is a SmartAlien and so on with all the aliens I have made. I
experimented with contains and equals but I couldn't get either to work
if they are even the right way to do it. So if anyone could help me and
perhaps show me some source code for how I should do it that would be
fantastic. I'm new to java(and programming in general) though so I
don't know all of the lingo yet. Also if anyone had any good links to
starting java game programmers that would be great.
Bjorn Abelli - 30 May 2005 22:48 GMT
"caleb" wrote...

> I'm making a space invader-like game, and for one method
> getLocationOfNearestAlien, which lasers use in part to see
> if they are touching an alien, I need some help.

A quick glance reveals your problem:

>    Alien a = (Alien) aliens.elementAt(i);
>    if(a.contains(ufos)) {

...

>            else if(a.contains(smartAliens)) {

> I experimented with contains and equals but I couldn't get
> either to work if they are even the right way to do it.

Yes, it should work, but the other way around.

An alien doesn't contain an ArrayList, but rather the other way around...

 Alien a = (Alien) aliens.elementAt(i);
    if(ufos.contains(a)) {

...

    else if(smartAliens.contains(a)) {

....

I haven't checked for any other possible errors in the code, as this was
what you asked for...

// Bjorn A
Johan Poppe - 31 May 2005 03:24 GMT
caleb wrote:

1.
>        for(int i=0; i<aliens.size(); i++) {
>            Alien a = (Alien) aliens.elementAt(i);
2.
>            if(a.contains(ufos)) {
>                for(int z=0; z<ufos.size(); z++) {
[quoted text clipped - 11 lines]
>                }
>            }

Quite aside from the question you actually ask, this code ha a lot of
unnecessary things and I doubt it is doing what you intend it to do.

At the end of each of the inner loops, (and after you have fixed the
'contains' bug) d and e will contain the size of _the last_ element in
either the smartAliens vector or the ufos vector. Is that what you
want?

I am not quite sure I understand your thinking, but I _think_ you
should replace the whole if (ufos.contain(a)) .... else if ...
structure with these two lines:

                   d = a.getSizeX() /2;
                   e = a.getSizeY() /2;

Then you should make sure that the getSizeX and ...Y-methods returns
the correct values for the various types of aliens.

>I'm new to java(and programming in general) though so I
>don't know all of the lingo yet. Also if anyone had any good links to
>starting java game programmers that would be great.

If you are new to programming, you should start by just learning
programming and not focus too much on the "game" part just yet.

Signature

 Johan Utne Poppe

caleb - 31 May 2005 03:43 GMT
Thanks for the replies. When I say I'm new I mean I've taken an
introductory course already but haven't really had any experience
making games which is what I want to do. Anyway I fixed it and it works
great. I've been basically using the reference material at java.sun.com
to make my programs so it's takes a great deal of trying stuff to
figure out how to use different things.

Anyway have the vector contain the object makes perfect sense and I
can't believe I couldn't think of that.

Johan I'm not sure what you're saying but I'm sure what I'm doing
probably isn't the best way.  See the thing is, a is part of Vector
aliens but aliens also contains ufos and smartAliens which both have
different sizes. If you can show me a better way to do it I would
appreciate it.
caleb - 31 May 2005 03:48 GMT
Actually if I just put the source code for the whole thing on a website
could you guys maybe look through it?
Johan Poppe - 31 May 2005 05:20 GMT
caleb wrote:

>Johan I'm not sure what you're saying but I'm sure what I'm doing
>probably isn't the best way.  See the thing is, a is part of Vector
>aliens but aliens also contains ufos and smartAliens which both have
>different sizes.

Yes, I understand that they have different size. But the things you
are doing now to get the size is unnecessary, and not even very
precise. Try to really _look_ at your code. Follow the flow of
execution, line by line. Think about which methods it is you are
calling, on which objects.

Signature

 Johan Utne Poppe



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.