Jump to content

Collisions in Panda.js


aliteff
 Share

Recommended Posts

Hi everyone, 

I am yet to work out how to do collisions in panda.js ! 

 

 

I have the following two objects : a Pluck and a Sheldon and trying to detect collision between the two. I really couldn't figure out how to use the Collisionsolver ? 

 

 I started up with the following example: http://vermeire.home.xs4all.nl/panda/fiddler.html

 

any clues would be much appreciated 

 

CODE

 

 

  game.createClass('Pluck', 'Sprite', {
 
            init: function(x, y) {
 
                this._super('img/pluck1.png', x, y, {anchor: { x: 0.5, y: 0.5 }});
                this.position = {x: x, y: y};
 
                //body
                this.body = new game.Body({
                    position: { x: x, y: y },
                    //velocityLimit: { x: 300, y: 1000 },
                    //velocity: {x:0,y:0},
                    collisionGroup: 0,
                    mass: 0
                });
                this.body.remove = this.remove.bind(this);
                this.body.addShape(new game.Circle(100));
                //add sprite to scene
                game.scene.addObject(this);
                //add body of this sprite to the world object
                game.scene.world.addBody(this.body);
                game.scene.world.addBodyCollision(this.body,0)
 
                //add sprite to display container
                game.scene.stage.addChild(this);
 
 
                game.scene.addTween(this.body.position, {
                    y: game.system.height
                }, 2000, {
                    easing: easings[currentEasing] + '.InOut',
                    repeat: Infinity,
                    yoyo: true
                }).start();
                //create a body using a body definition
 
            },
 
            update: function(){
                this.position.x = this.body.position.x;
                this.position.y = this.body.position.y;
            },
 
            remove: function() {
                game.scene.removeObject(this);
                game.scene.world.removeBody(this.body);
                game.scene.stage.removeChild(this);
            }
 
 
        });
 
------ SECOND OBJECT
 
   game.createClass('Sheldon', 'Sprite', {
 
            init: function(x, y) {
 
                this._super('img/sheldonright.png', x, y, {anchor: { x: 0.5, y: 0.5 }});
 
                this.body = new game.Body({
                    position: { x: x, y: y },
                    //velocityLimit: { x: 150, y: 150 },
                    //velocity: {x:-100,y:-50},
                    collisionGroup: 1,
                    collideAgainst: 0,
                    mass: 0
                });
                this.body.collide = this.collide.bind(this);
                this.body.addShape(new game.Rectangle(60, 60));
                //add sprite to scene
                game.scene.addObject(this);
                //add body of this sprite to the world object
                game.scene.world.addBody(this.body);
                game.scene.world.addBodyCollision(this.body,1);
                //add sprite to display container
                game.scene.stage.addChild(this);
 
 
 
                //create a body using a body definition
 
            },
            collide: function(opponent) {
                console.dir(arguments);
                console.log("Collision!");
                opponent.remove();
                return true;
            },
            update: function(){
                //The box2D world keeps track of the movement and position of the body.
                //use the update function to get the sprite in the right spot
 
 
                if(game.keyboard.down("UP")){
                }
                if(game.keyboard.down("DOWN")){
                }
                if(game.keyboard.down("LEFT")){
                    this.setTexture('img/sheldonleft.png');
                    this.position.x =  this.body.position.x;
                    this.body.position.x =  this.body.position.x -5;
 
                }
 
                if(game.keyboard.down("RIGHT")){
                    this.setTexture('img/sheldonright.png');
 
                    this.position.x =  this.body.position.x;
 
                    this.body.position.x +=5;
 
                }
 
            },
            remove: function() {
                game.scene.removeObject(this);
                game.scene.world.removeBody(this.body);
                game.scene.stage.removeChild(this);
            }
 
 
 
 
        });
 
 
GAME SCENE
 
    game.createScene('scene1', {
            backgroundColor: 0xffffff,
            init: function() {
                this.world = new game.World(0, 0);
 
                var background = new game.Background('img/background.png');
 
                var sat = new game.Sat(50, 50);
 
 
                var sheldon = new game.Sheldon(50,400);
 
                var clouds = new game.Clouds();
 
                //var score = new game.Score(650, 50);
 
                var pluck = new game.Pluck(50,50);
 
                var emitter = new game.Emitter();
                emitter.textures.push('img/bill2.png', 'img/bill1.png');
                emitter.position.set(sat.position.x, sat.position.y);
                emitter.rate = 1000;
                emitter.count = 2;
                emitter.target.set(300, sheldon.position.y);
                emitter.targetForce = 200;
                emitter.startAlpha = 1;
                emitter.endAlpha = 1;
                emitter.positionVar.set(300, 0);
 
                emitter.addTo(game.scene.stage);
                emitter.angle = 0.2;
                emitter.angleVar = 0;
 
                game.scene.addEmitter(emitter);
 
                var emitter2 = new game.Emitter();
                emitter2.textures.push('img/pluck2.png', 'img/pluck3.png', 'img/pluck4.png', 'img/pluck5.png');
                emitter2.position.set(sat.position.x, sat.position.y);
 
                emitter2.target.set(800, 800);
                emitter2.targetForce = 50;
                emitter2.rate = 500;
                emitter2.count = 1;
                emitter2.startAlpha = 1;
                emitter2.endAlpha = 1;
                emitter2.angle = 0.7;
                emitter2.angleVar = 0.5;
                emitter2.life = 3000;
                emitter2.lifeVar = 1000;
 
                emitter2.addTo(game.scene.stage);
 
                game.scene.addEmitter(emitter2);
                game.scene.addTimer(5000, this.complete.bind(this));
                this.solver = new game.CollisionSolver(sheldon, pluck);
 
                if(this.solver.hitTest(sheldon,pluck))console.log('HIT');
            },
            update: function(){
                //text1: Q-key
                this._super();
               // if(this.solver.hitTest(this.sheldon, this.pluck))console.log('HIT');
                //this.emitter.target.set(this.sat.position.x, this.sat.position.y);
 
 
 
            },
            click: function() {
                currentEasing++;
                if (!easings[currentEasing]) currentEasing = 0;
                game.system.setScene('Main');
            },
            keydown: function(e){
 
 
                if (e.toString() == 'ENTER')game.system.setScene('Main');
                if (e.toString() == '1')game.system.pause(); // Pause game engine
                if (e.toString() == '2')game.system.resume();
            },
            complete: function() {
                // Timer complete
                game.system.stop();
            }
 
        });
 
 
 
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...