User talk:Awesomeness

From Robowiki
Revision as of 18:11, 2 May 2009 by Awesomeness (talk | contribs) (Anti-Gravity)
Jump to navigation Jump to search
Archived talks:
Archived Talk 20090502

Okay, I need to make a list of bullets. I have made a bullet class and everything, but I need a special list that will let me access all of the bullets in a while/for loop and be able to add and remove them at any time and never mess up. If I did this in a normal array I could do a for loop and access using an increasing number, but I wouldn't know when to stop. In a while loop, if I used while(bulletsArray[x] != null) and then increment x, after I remove my first bullet that say, hit a wall, it will stop there. I need help!

Thanks,
Awesomeness 14:02, 2 May 2009 (UTC)

I think this will do:

ArrayList<Bullet> bullets = new ArrayList<Bullet>();
for (int i = 0; i < bullets.size(); i++) {
    Bullet bullet = bullets.get(i);
    if (bullet.needRemove()) {
        // I think this will work
        // bullets.remove(i--);
        // but usually I do
        bullets.remove(bullet);
        i--;
    }
}

Here I assume that you use ArrayList to store you bullets and your bullet's classname is Bullet. If you use a plain array to keep the bullets, consider change to ArrayList. It is more flexible. » Nat | Talk » 14:09, 2 May 2009 (UTC)

Oh my gosh! You're right! ArrayList is way more flexible! I looked at the documentation; you don't even have to increment or decrement anything! The function remove() shifts the other elements to the left already! Thanks! Awesomeness 14:14, 2 May 2009 (UTC)
Be sure don't use for-each style (for(Bullet bullet : bullets) or you will get ConcurentModificationException when you remove element (this is per java spec, but I still use the for-each style and didn't get this exception actually). And when you scroll through the array like one I mentioned, be sure you do i--, or you will just skip one element. » Nat | Talk » 14:19, 2 May 2009 (UTC)
Oh, you're right. Awesomeness 14:27, 2 May 2009 (UTC)
One suggestion. Don't use bullets.remove(bullet);, use bullets.remove(i); instead because removing by index is far faster than removing by content. Well, with the size the list is in robocode it probably doesn't matter a ton, but removal of list by content, when you already have the index on hand, seems terribly wasteful to me. --Rednaxela 14:45, 2 May 2009 (UTC)
I changed that myself already. =D lol Awesomeness 14:56, 2 May 2009 (UTC)

I've grown very fond of iterators for Lists, this would be my implementation:

ArrayList<Bullet> bullets = new ArrayList<Bullet>();
Iterator<Bullet> i = bullets.iterator();
while (i.hasNext()) {
    Bullet bullet = i.next();
    if (bullet.needRemove()) {
        i.remove();
    }
}

--Skilgannon 15:00, 2 May 2009 (UTC)

Ahhh... I never knew that iterator has remove method so I always using ArrayLisy.get(i) to prevent ConcurentModificationException. Thanks you very much. » Nat | Talk » 15:06, 2 May 2009 (UTC)

How do you find the x and y of a robot you've scanned? I see no ScannedRobotEvent.getX() or getY() method. Awesomeness 15:19, 2 May 2009 (UTC)

Trig Trig Trig...

double absBearing = e.getBearingRadians() + getHeadingRadians();
double x = getX() + Math.sin(absBearing) * e.getDistance();
double y = getY() + Math.cos(absBearing) * e.getDistance();

That's all. » Nat | Talk » 15:21, 2 May 2009 (UTC)

Here's a method I use in my utils class (I first saw it in a PEZ bot):

    public static Point2D.Double project(Point2D.Double sourceLocation, 
        double angle, double length) {
        return new Point2D.Double(sourceLocation.x + Math.sin(angle) * length,
            sourceLocation.y + Math.cos(angle) * length);
    }
    // You'd pass it something like: 
    // myLocation = new Point2D.Double(getX(), getY()); 
    // enemyLocation = MyUtils.project(myLocation, absBearing, e.getDistance());

--Voidious 16:15, 2 May 2009 (UTC)


Lastly, I need a way to "dodge" these simulated bullets and to figure out what angle the oppenent bot would be shooting from if it used linear targeting against me. I don't know how to do either of these. Awesomeness 16:31, 2 May 2009 (UTC)

To dodge, use Anti-Gravity Movement, to assume linear firing, use this code:

angle = Utils.normalRelativeAngle(absBearing +  Math.PI + Math.asin((Math.sin(e.getBearingRadians()) * getVelocity())/(20 - 3 * BULLET_POWER)));

replace BULLET_POWER to which you get from energy drop. This prediction use non-iterative no-wall-stopping linear targeting. » Nat | Talk » 16:35, 2 May 2009 (UTC)

Thanks! This fits exactly what I was planning to do! Awesomeness 16:44, 2 May 2009 (UTC)
However anti-gravity confuses me greatly... Awesomeness 17:11, 2 May 2009 (UTC)

You cannot post new threads to this discussion page because it has been protected from new threads, or you do not currently have permission to edit.

There are no threads on this page yet.