All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fr.vergne.downhill.impl.ReductionDownhillCollider Maven / Gradle / Ivy

The newest version!
package fr.vergne.downhill.impl;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

import fr.vergne.downhill.DownhillCollider;

/**
 * The {@link ReductionDownhillCollider} aims at reducing the {@link Ball}s
 * received in a forceful way. Thus, once a {@link Ball} is created based on two
 * other {@link Ball}s, only the created one is kept, the two initial
 * {@link Ball}s begin discarded. This process is repeated iteratively until no
 * new {@link Ball} can be created. At the end of the colliding, only the
 * {@link Ball}s which have not been "consumed" to create new ones are returned.
*
* This implementation is particularly suited for cases where a {@link Ball} * a can be collided with a {@link Ball} b implies * that a can also be collided with any {@link Ball} coming from a * collision with b. On the opposite, it is totally inadapted for * cases where the same {@link Ball} should be reused for other collisions. * * @author Matthieu Vergne * * @param */ public class ReductionDownhillCollider implements DownhillCollider { @Override public Collection rolls(Collection balls, Collider... colliders) { LinkedList remain = new LinkedList(balls); boolean collisionOccurred; do { collisionOccurred = false; LinkedList merged = new LinkedList(); while (!remain.isEmpty()) { Ball rollingBall = remain.removeFirst(); Iterator iterator = remain.iterator(); while (iterator.hasNext()) { Ball ball = iterator.next(); for (Collider collider : colliders) { if (collider.areColliding(rollingBall, ball)) { rollingBall = collider.collide(rollingBall, ball); iterator.remove(); collisionOccurred = true; } else { continue; } } } merged.add(rollingBall); } remain = merged; } while (collisionOccurred); return remain; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy