2D collision simulation-programming

 
Thread Tools Search this Thread
Special Forums UNIX and Linux Applications 2D collision simulation-programming
# 1  
Old 04-28-2010
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.heightthis,  options);
        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() > maxSpeed)  this.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 !! Image

Last edited by JackCrital2005; 04-28-2010 at 01:02 PM..
# 2  
Old 04-28-2010
It looks like javascript, maybe.
# 3  
Old 05-06-2010
Its Javascript
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Filename collision during backup

Hi, I am trying to backup all *.tar files from a legacy Linux to a portable harddrive. find . -name "*.tar" -exec cp {} /media/mypassport/backup \; I found that there are files with the same filenames and they were overwritten in the destination folder. They are coming from different... (4 Replies)
Discussion started by: cornellhumphrey
4 Replies

2. IP Networking

OLSR simulation in ns2

# Create the simulator object that we need in order to run NS set ns # Set the parameters that we will use for wireless communications set val(chan) Channel/WirelessChannel ;# channel type set val(prop) Propagation/TwoRayGround ;# radio-propagation model set... (0 Replies)
Discussion started by: amithkhandakar
0 Replies

3. Programming

Simulation using C/C++ and Java

Hi, I'm just start to learning simulate a network protocol using C/C++ and Java from scratch. Actually, I want to make two nodes can communicate using TCP and UDP protocol, for example http connection and video streaming, respectively. Can anyone help me find several references or guidance... (0 Replies)
Discussion started by: nica
0 Replies

4. Shell Programming and Scripting

Remote simulation and 'at' command

Hey, Task seems to be quite easy, but I'm still a bit green in shell scripting. I hope you can help me a bit. I have to run some simulation at the distance by remote terminal. Normally when I'm working on the server directly I just type: mpirun -np 8 compressibleInterFoam -parallel > log.txt... (7 Replies)
Discussion started by: PiPrus
7 Replies

5. Infrastructure Monitoring

collision backoff algorithm

hi there, im new to this forum, so just like to day hello to everyone!!! i know im not aloud to post homework questions, but is it ok to ask for a formula to use to answer a question? i looking for a formula for collison backoff algorithm to find the average time to successfully transmit a... (3 Replies)
Discussion started by: purejoker
3 Replies

6. Windows & DOS: Issues & Discussions

unix simulation

hello everybody.. im new to this forum.. i have sme basic knowledge about unix.. but not too much.. i would like to practice shell programs n perl using a unix simulator.. but then i don't know wht a unix simulator means? just a bald definition that it creates a unix working environment in windows... (5 Replies)
Discussion started by: esash
5 Replies

7. UNIX for Dummies Questions & Answers

access collision with shared file system

Hello ALL, In my system, there are 14 machines running the same version of Linux RHEL4. The 14 machines use a NFS file system, i.e., a shared file system. My question is that if the programs in individual machines can access a common file simutaneously. Or, they have to access the file... (1 Reply)
Discussion started by: cy163
1 Replies
Login or Register to Ask a Question