Velocity Reviews > Java > How to see if an object is part of a specific vector?

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

caleb
Guest
Posts: n/a

 05-30-2005
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
Guest
Posts: n/a

 05-30-2005

"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

// Bjorn A

Johan Poppe
Guest
Posts: n/a

 05-31-2005
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++) {
> 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;
> }
> }

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.

--
Johan Utne Poppe

caleb
Guest
Posts: n/a

 05-31-2005
Thanks for the replies. When I say I'm new I mean I've taken an
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
Guest
Posts: n/a

 05-31-2005
Actually if I just put the source code for the whole thing on a website
could you guys maybe look through it?

Johan Poppe
Guest
Posts: n/a

 05-31-2005
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.

--
Johan Utne Poppe