Find the answer to your Linux question:
Results 1 to 4 of 4
Hello, everybody, I'm thankful for the great helps you gave during the past year. Now I'm facing a serious problem: I was obliged to write a 2D collision simulation applet, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2009
    Posts
    4

    Lightbulb 2D collision simulation-programming


    Hello, everybody, I'm thankful for the great helps you gave during the past year.

    Now I'm facing a serious problem:

    I was obliged to write a 2D collision simulation applet, and my experience is only in C,C++,Intelx86 assembly. I have no experience in Java and the like, and I don't know how to write animation programme and I have just found an example

    the source code is as following:
    PHP Code:
    var Field = function(){ this.init.apply(thisarguments); };
    Field.prototype = {
        
    particles: [],
        
    permissivity1,
        
    maxSpeed0,
        
    nextID0,
        
    scale.3,
        
    interval31,
        
    gridSize0,
        
    grid: [],

        
    init: function(eloptions){
            
    Object.extend(thisoptions || {});
            
    this.width el.offsetWidth;
            
    this.height el.offsetHeight;
            
    this.el el;
            
    this.el.style.position 'relative';
            
    this.update();
        },
        
        
    update: function(){
            for(var 
    i=this.particles.length-1i>=0i--) this.particles[i].update();
            
    this.timer window.setTimeout(this.update.bind(this), this.interval);
        },
        
        
    addParticle: function(options){
            if(!
    optionsoptions = {};
            var 
    particle = new Particle(options.|| Math.random()*this.widthoptions.|| Math.random()*this.heightthisoptions);
            if(
    this.bouncyWalls)
                
    particle.lockToBoundry(0this.width0this.height);
            
    this.particles.push(particle);
            return 
    particle;
        },
        
        
    getNextID: function(){
            return 
    this.nextID++;
        },
        
        
    getGridLocation: function(xy){
            return [
                
    Math.floor(this.gridSize) || 0,
                
    Math.floor(this.gridSize) || 0
            
    ]
        },
        
        
    getParticlesAtGridLocation: function(xy){
            if(!
    this.grid[x] || !this.grid[x][y]) return false;
            return 
    this.grid[x][y];
        }
        
    }

    var 
    Particle = function(){ this.init.apply(thisarguments); };
    Particle.prototype = {
        
    xVel:0,
        
    yVel:0,
        
    xAcc:0,
        
    yAcc:0,
        
    xForce:0,
        
    yForce:0,
        
    radius:5,
        
    mass:1,
        
    restraints: [],
        
    ignoredParticles: {},
        
        
    init: function(xyfieldoptions){
            
    this.x;
            
    this.y;
            
    this.field field;
            
    Object.extend(thisoptions || {});
            
    this.id field.getNextID();
            
    this.el document.createElement('img');
            
    this.el.src 'ball.gif';
            
    this.el.style.position 'absolute';
            
    this.resize(this.radius);
            
    this.update();
            
    field.el.appendChild(this.el);
        },

        
    update: function(){
            
    this.move();
            if(
    this.followMouse) return;
            
    this.processRestraints();
            
    this.xVel this.xVel*this.field.permissivity this.xForce/this.mass this.xAcc;
            
    this.yVel this.yVel*this.field.permissivity this.yForce/this.mass this.yAcc;
            
    this.+= this.xVel this.field.scale;
            
    this.+= this.yVel this.field.scale;
        },
        
        
    move: function(){
            
    this.el.style.left =  this.'px';
            
    this.el.style.top this.'px';
        },
        
        
    resize: function(radius){
            
    this.radius radius;
            
    this.el.style.width radius 'px';
            
    this.el.style.height radius 'px';
            
    this.el.style.marginLeft = -radius 'px';
            
    this.el.style.marginTop = -radius 'px';
        },
        
        
    processRestraints: function(){
            for(var 
    i=this.restraints.length-1i>=0i--) this.restraints[i]();
        },
        
        
    addRestraint: function(fn){
            
    this.restraints.push(fn);
        },
        
        
    lockToMouseDown: function(momentum){
            var 
    self this;
            
    this.el.style.cursor 'move';
            
    this.el.onmousedown = function(e){
                
    self.followMouse true;
                
    Mouse.lastXY Mouse.getXY(e);
                
    document.onmousemove = function(e){
                    var 
    xy Mouse.getXY(e);
                    var 
    xy[0] - Mouse.lastXY[0];
                    var 
    xy[1] - Mouse.lastXY[1];
                    if(
    momentumself.nudgeWithVelocity(xy);
                    else 
    self.nudge(xy);
                    
    Mouse.lastXY xy;
                    return 
    false;
                };
                
    document.onmouseup =  function(){
                    
    self.followMouse false;
                    
    document.onmousemove null;
                };
                return 
    false;
            };
        },
        
        
    nudge: function(xy){
            
    this.+= x;
            
    this.+= y;
        },
        
        
    nudgeWithVelocity: function(xy){
            
    this.xVel x;
            
    this.yVel y;
            
    this.nudge(xy);
        },
        
        
    lockSpringTo: function(particlemultiplier){
            if(!
    multipliermultiplier .5;
            var 
    currentDistance this.getDistFrom(particle);
            
    this.addRestraint(function(){
                var 
    dist this.getDistFrom(particle);
                var 
    diff dist currentDistance;
                
    this.accelerateTowards(particlediff*multiplier);
                
    particle.accelerateTowards(thisdiff*multiplier);
            }.
    bind(this));
        },

        
    lockGravityTo: function(particle){
            
    this.addRestraint(function(){
                
    this.gravitateTowards(particle);
            }.
    bind(this));
        },
        
        
    lockToBoundry: function(xMinxMaxyMinyMax){
            
    xMin xMin this.radius;
            
    yMin yMin this.radius;
            
    xMax xMax this.radius;
            
    yMax yMax this.radius;
            
    this.addRestraint(function(){
                if(
    this.xMin){
                    
    this.xVel = -1*this.xVel;
                    
    this.xMin;
                }
                else if(
    this.xMax){
                    
    this.xVel = -1*this.xVel;
                    
    this.xMax;
                }
                if(
    this.yMin){
                    
    this.yVel = -1*this.yVel;
                    
    this.yMin;
                }
                else if(
    this.yMax){
                    
    this.yVel = -1*this.yVel;
                    
    this.yMax;
                }
            }.
    bind(this));
        },
        
        
    lockToFieldSpeedLimit: function(){
            
    this.addRestraint(function(){
                if(
    this.getSpeed() > maxSpeedthis.setSpeed(this.field.maxSpeed);
            }.
    bind(this));  
        },
        
        
    lockToXForce: function(f){
            
    this.xForce f;
        },
        
        
    lockToYForce: function(f){
            
    this.yForce f;
        },
        
        
    lockToXAccel: function(a){
            
    this.xAcc a;
        },
        
        
    lockToYAccel: function(a){
            
    this.yAcc a;
        },
        
        
    moveTowards: function(particleamount){
            var 
    xy this.getUnitVectorTowards(particle);
            
    this.+= xy[0] * amount;
            
    this.+= xy[1] * amount;
        },
        
        
    accelerateTowards: function(particlemultiplier){
            var 
    xy this.getUnitVectorTowards(particle);
            
    this.xForce xy[0] * multiplier;
            
    this.yForce xy[1] * multiplier;
        },
        
        
    gravitateTowards: function(particle){
            var 
    dist this.getDistFrom(particle);
            
    //this.attractTo(particle, this.mass * particle.mass / dist * dist);
            
            
    this.accelerateTowards(particleparticle.mass dist dist);
        },
        
        
    attractTo: function(particleforce){
            
    this.accelerateTowards(particleforce this.mass);
        },
        
        
    getDistFrom: function(particle){
            var 
    d1 this.particle.x;
            var 
    d2 this.particle.y;
            return 
    Math.sqrt(d1*d1 d2*d2);
        },
        
        
    getUnitVectorTowards: function(particle){
            var 
    len this.getDistFrom(particle);
            return [
                (
    particle.this.x) / len,
                (
    particle.this.y) / len
            
    ];  
        },
        
        
    getSpeed: function(){
            return 
    Math.sqrt(this.xVel*this.xVel this.yVel*this.yVel);    
        },
        
        
    setSpeed: function(speed){
            var 
    currentSpeed this.getSpeed();
            
    this.xVel this.xVel currentSpeed speed;
            
    this.yVel this.yVel currentSpeed speed;
        },
        
        
    setDirection: function(xy){
            var 
    currentSpeed this.getSpeed();
            
    this.xVel this.xVel currentSpeed x;
            
    this.yVel this.yVel currentSpeed y;
        },
        
        
    bounceInto: function(particle){
            var 
    dist this.getDistFrom(particle);
            var 
    overlap this.radius particle.radius dist;
            if(
    overlap 0){
                var 
    s1 this.getSpeedAfterElasticCollisionWith(particle);
                var 
    s2 particle.getSpeedAfterElasticCollisionWith(this);
                
                var 
    xy this.getUnitVectorTowards(particle);
                
    this.xVel = -xy[0];
                
    this.yVel = -xy[1];
                
    particle.xVel xy[0];
                
    particle.yVel xy[1];
                
                
    this.setSpeed(s1);
                
    particle.setSpeed(s2);
                
                
    this.moveAwayFrom(particleoverlap/2);
                
    particle.moveAwayFrom(thisoverlap/2);
            }
        },
        
        
    getSpeedAfterElasticCollisionWith: function(particle){
            return (
    this.getSpeed() * (this.mass particle.mass) + particle.getSpeed() * particle.mass) / (this.mass particle.mass)
        },
        
        
    moveAwayFrom: function(particledist){
            var 
    xy this.getUnitVectorTowards(particle);
            
    this.+= -xy[0] * dist;
            
    this.+= -xy[1] * dist;
        },
        
        
    updateGridLocation: function(){
            var 
    xy this.field.getGridLocation(this.xthis.y);
            if(
    this.lastGridLocation)
                
    this.removeFromGrid();
            
    this.addToGrid(xy[0], xy[1]);
        },
        
        
    addToGrid: function(xy){
            var 
    grid this.field.grid;
            if(!
    grid[x]) grid[x] = [];
            if(!
    grid[x][y]) grid[x][y] = [];
            
    grid[x][y][this.id] = true;
            
    this.lastGridLocation = [xy];
        },
        
        
    removeFromGrid: function(){
            var 
    this.lastGridLocation[0];
            var 
    this.lastGridLocation[1];
            
    delete this.field.grid[x][y][this.id];
        },
        
        
    bounceOffNearbyParticles: function(){
            if(
    Math.abs(this.x-this.lastXY[0]) < this.field.scale && Math.abs(this.y-this.lastXY[1]) < this.field.scale) return;
            
    this.lastXY = [this.xthis.y]; 
            var 
    xmin this.lastGridLocation[0] - 1;
            var 
    ymin this.lastGridLocation[1] - 1;
            var 
    partsij;
            for(
    i=xmini<xmin+3i++){
                for(
    j=yminj<ymin+3j++){
                    if(
    parts this.field.getParticlesAtGridLocation(ij)){
                        for(var 
    k in parts){
                            if(
    this.id != k){
                                
    this.bounceInto(this.field.particles[k]);
                            }
                        }
                    }
                }
            }
        },
        
        
    registerToCollisionGrid: function(){
            
    this.lastXY = [0,0];
            if(
    this.radius*this.field.gridSize)
                
    this.field.gridSize this.radius*2;
            
    this.updateGridLocation();
            
    this.lockToGridCollisions();
        },
        
        
    lockToGridCollisions: function(){
            
    this.addRestraint(function(){
                
    this.updateGridLocation();
                
    this.bounceOffNearbyParticles();
            }.
    bind(this));
        }
    }

    Function.
    prototype.bind = function(toThisargs){
        if(!
    argsargs = [];
        var 
    self this;
        return function(){ 
    self.apply(toThisargs); }
    }

    var 
    Mouse = {
        
    getXY: function(e){
            if(!
    e) var window.event;
            return [
    e.clientXe.clientY];
        }
    }

    Object.extend = function(obextension){
        for(var 
    i in extension){
            
    ob[i] = extension[i];
        }

    But I don't know what language this is? And what compiler is needed to compute & execute the code? Thank you !!

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,591
    VB or matlab perhaps? My suggestion is to use this as pseudo-code and implement it in your language of choice.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Mar 2010
    Posts
    16
    If you are experienced with c and c++, learning java can be really quick. Search for some of the excellent tutorials, and you'll be up and running in no time. It will take more time to learn to program Java like a pro, but I wrote my first physics applet for one of my courses after spending one weekend with a Java book. The hard part of programming is thinking like a programmer, not the language!

  4. #4
    Just Joined!
    Join Date
    Mar 2010
    Posts
    16
    Oh, and by the way, the Java SDK is free from Sun. I would avoid an IDE at first - you won't need it for a simple applet. Myself - I'm kind of old school, and I like text files that do exactly what I expect. I even edit images with text files . . .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •