Jump to content

Some issues with positioning, and physics


dcooke616
 Share

Recommended Posts

Hello all, I've recently ran into some trouble with collision in a remake of worms that i'm creating.

 

Problem 1 - Bitmap collision

I am using p2.setPostBroadphaseCallback to set my function checkCollision as the callback.

this.physics.p2.setPostBroadphaseCallback(this.checkCollision, this);

 

Here is my function (well the important bit)

checkCollision: function(){

if(body1.sprite.id =="missile" && body2.sprite.id =="land" ||
                body2.sprite.id=="missile" && body1.sprite.id =="land"){

                var missile;
                var land;

                
                if(body1.sprite.id =="missile" && body2.sprite.id =="land"){
                    missile = body1.sprite;
                    land = body2.sprite;
                }
                else if(body2.sprite.id =="missile" && body1.sprite.id =="land"){
                    missile = body2.sprite;
                    land = body1.sprite;
                }

                //destroy stuff
                var x = Math.floor(missile.x);
                var y = Math.floor(missile.y);
                
                //this code is a work in progress (see problem 2)
                this.landBmp.blendDestinationOut();
                this.landBmp.circle(x,y,15, 'rgba(0,0,0,255');
                this.landBmp.blendReset();
                this.landBmp.update();
                this.land.reset(this.land.x,this.land.y,this.landBmp);
                

                //this method will destroy the missile removing it and triggering an explosion 
                this.explode(missile);

            }


}

My first problem is as follows, the landBmp data takes up a large portion of the screen (1500,1500) to be exact, and this causes the missile to collide with the land as soon as it has launched. Causing the missile to detonate on the player.

 

Problem 2 - Position foreground correctly

So I have the following imageland1_foreground.png

Which I am trying to insert at (0,0) , the problem is it doesn't align properly with the background image (a slightly darker version of the same image).
 

Capture.PNG

 createLand: function(){
            //create the land bitmap data
            this.landBmp = this.add.bitmapData(1500,1250);
            this.landBmp.draw('land1_foreground',0,0);
                     
            
            //convert to sprite landBmp -> land
            this.land = this.game.add.sprite(0,0,this.landBmp);

            // enable physics on land sprite
            this.physics.p2.enable(this.land, false);
            

            //add collision id
            this.land.id = "land";
            
            this.land.body.clearShapes();
            this.land.body.loadPolygon("land1_physics","land1_foreground");
            this.land.body.immovable = true;
            this.land.body.static = true;

            this.land.anchor.setTo(0.5);

            this.land.body.setCollisionGroup(this.landCollisionGroup);
            this.land.body.collides([this.wormsCollisionGroup, this.bulletCollisionGroup]);
            
            
            //define a single world material for the land objects
            this.worldMaterial = this.physics.p2.createMaterial('worldMaterial',this.land.body);

            this.landBmp.update();
            

        },

The weirdest part is when I remove physics from the land sprite it positions at the correct place. Also if you look at the Alpha value, alpha is >0 in all the expected places (for example if i hover my mouse over the outline of where i WANT the foreground image to be it is always greater than 0), so it must be bugged out or something.

 

Problem 3 - A long shot

Ultimately I would like to recreate worms, and in order to do that, I need destructible environment. If i can get my other 2 problems solved, it will eventually lead me to this stage. And I have already foreseen a problem, how can I recalculate polygon data for the land sprite after parts are removed?

 

Now I have a rough idea how I would destroy parts of the bitmap data, but in order for physics to be applied correctly I will need to recalculate polygon values (and i just used a 3rd party application to generate polygon values the first time)

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...