locked
Collision algorithm RRS feed

  • Question


  • Hi,

    I would like the following functionality:

    - None of the Ellipse elements may be at the same Grid.Row|Grid.Column combination as another Ellipse in the ellipseArray.

    - Each Ellipse has to find its unique position on the Grid [x,y].

    So I can't have two Ellipses both having the same value for <Border Grid.Row and Grid.Column> (e.g. both ["0","0"]).

     

    See code here:

    <Border Grid.Row="0" Grid.Column="0">
        <Canvas>
            <Ellipse Width="100" Height="100" Fill="Red"/>
        </Canvas>
    </Border>
    ..
    (This code is repeated 9 times, where .Row and .Column are variable. The last <Border> has no <Ellipse>)

    I have 8 Ellipse elements on a Grid of 9 squares.

     

    It looks sort of like a collision algorithm.

    How would I accomplish this?

     ---

    Edit: Fixed it.

    Here's the solution:

    function checkPositions() {
    
        var j = 0;
        var collision;
    
        do {
            var newRow = Math.floor(Math.random() * 3 + '');
            var newCol = Math.floor(Math.random() * 3 + '');
            borderArray[ j ]['Grid.Row'] = newRow;
            borderArray[ j ]['Grid.Column'] = newCol;
    
            collision = false;
            for (var i = 0; i < j; i++) {
    
                if (borderArray[ i ]['Grid.Row'] == (borderArray[ j ]['Grid.Row'])
                 && borderArray[ i ]['Grid.Column'] == (borderArray[ j ]['Grid.Column'])) {
                    collision = true;              
                    break;
                }
            }
    
            if (!collision) {
                j += 1;
            }
    
        } while (j < randomRowsArray.length+1);
    }
    Saturday, July 16, 2011 10:28 AM

Answers

  • function checkPositions() {
    
        var j = 0;
        var collision;
    
        do {
            var newRow = Math.floor(Math.random() * 3 + '');
            var newCol = Math.floor(Math.random() * 3 + '');
            borderArray[j]['Grid.Row'] = newRow;
            borderArray[j]['Grid.Column'] = newCol;
    
            collision = false;
            for (var i = 0; i < j; i++) {
    
                if (borderArray[ i ]['Grid.Row'] == (borderArray[j]['Grid.Row'])
                 && borderArray[ i ]['Grid.Column'] == (borderArray[j]['Grid.Column'])) {
                    collision = true;              
                    break;
                }
            }
    
            if (!collision) {
                j += 1;
            }
    
        } while (j < randomRowsArray.length+1);
    Saturday, July 16, 2011 11:52 AM