Collision is being detected before actual collision

So i am making a board knife game, shooting the knife from the bottom of the screen and sticking it to the rotating circular board

So, my idea is to have a knife with a collider and rigidbody and a board with a collider and rigidbody as well.
once i apply the impulse force to the knife in up direction, i expect it to hit the board and i once it hits it, it will trigger the “collisionEnter” event, and at that time ill make the rigidbody of knife as static, so that it doesnt fall down, but what i am experiencing is that, the knife stops before even it collides.

var Knife = pc.createScript('knife');
Knife.attributes.add("fireBtn",{type:"entity"});
Knife.attributes.add("force",{type:"number"});


Knife.prototype.initialize = function() 
{
    this.rb=this.entity.rigidbody;
    console.log(this.rb);

    this.fireBtn.button.on("click",this.ShootKnife,this);
    this.entity.collision.on('collisionstart', this.OnKnifeHitBoard, this);
};

// update code called every frame
Knife.prototype.update = function(dt) 
{
    
};

// update code called every frame
Knife.prototype.ShootKnife = function() 
{
    this.rb.type=pc.BODYTYPE_DYNAMIC;
    console.log(this.rb);
    this.rb.applyImpulse(0,this.force,  0);

};

Knife.prototype.OnKnifeHitBoard = function(result) 
{
    console.log(result);
    this.rb.enabled=false;
    this.rb.type=pc.BODYTYPE_STATIC;
    console.log("KinfHit");

};

Hi @Ashish_Rana!

I can’t really see what’s happening on your video.

Shouldn’t the knife move towards the board instead of falling down?

Check the actual size of the collision on both entities

My bad, and sorry, if the video was confusing, i had its bodytype as dynamic earlier and thus it was falling down before i hit the button to shoot it up.
Now i have made it static in the beginning in the editor and making it dynamic before i apply the force using the script(as shown in the above script in ShootKnife Function).

And the size of the collider is also proper

Does it have to use rigidbody impulses? How about just dont have any collision at all and translate the entity and stop it after it hits, then rotate.

1 Like

Here, I made a test of what I think you want. Just press space to throw the knives.

Junk dont steal | Launch (playcanvas.com)

2 Likes

I really appreciates the effort and thankyou
But yes it needs the collision detection, as further down the development, i will be instantiating 2nd knife once the 1st knife hits the board, and then shooting the 2nd knife will instantiate the 3rd one and so on…
if the player fail to hit the board and instead hits any of the knife which is already attached to the board the knife falls back,
So, physics simulation and collision detection with the other knifes is needed.

But its not about the alternative workaround as of now, its more about why its showing unexpected behavior.

This is the link of the game that i am trying to simulate.

I’ve seen the problem now. Does this happen all the time or only occasionally?

Can you share a sample project maybe?

its happening quite often.
But what i have noticed it, if the impulse force is high, the gap between them is more.
This is the project
https://playcanvas.com/project/1260776/overview/kinfeboard
and i have made you as an admin, is that fine by you?

I forked the project to prevent breaking your project. You can already remove me, thanks.

Maybe the topic below is related.

I appreciate it,
I too thought, this could be the case of CCD, but i would have considered this, if the knife would have passed trough the target instead, but as of now the scenario is that, its invoking the collision event even before it actually collides.
But thanks, leeme dig into this a bit more.

Did you at least look at the project I sent you?

I could also try adding a feature to break a knife if it hits another knife if thats what you want. Im just tryna help out man.

The thing to remember here is that it’s doing a physics simulation, its never going to be exactly touching because the physics has simulated the knife hitting the circle and bouncing off in the physics step/update

Th collision callback event should give you the contact point where they touched. What I would do is:

  • Wait for collision callback
  • Convert the knife to kinematic
  • Parent the knife to the spinning circle (assuming that it’s scale is 1, 1, 1)
  • Move the knife global position so that the tip is at the point of contact which is in the data returned from the collision callback
1 Like

yes, i did that :smile:, as soon as u shared it

I just moved a knife made of some planes that was a child of a point that when it reaches the same y value of the rotation object it stops moving and rotates.

var Knifetest = pc.createScript('knifetest');
Knifetest.attributes.add("next", {
type: "entity",
});
Knifetest.prototype.initialize = function() {
this.goinup = 0;
this.spinnin = 0;
(this.next).enabled = false;
};
Knifetest.prototype.update = function(dt) {
this.entity.rotateLocal(0*dt, 0*dt, (this.spinnin)*dt);
if(this.app.keyboard.wasPressed(pc.KEY_SPACE))
{
this.goinup = 4;
};
if((this.entity.getLocalPosition().y) >= 1)
{
//this.entity.setLocalPosition(0, 1, 0);
this.goinup = 0;
this.spinnin = (120);
(this.next).enabled = true;
}
else if(this.goinup > 0);
{
this.entity.translateLocal(0*dt, (this.goinup)*dt, 0*dt);
};
};

If I had to prevent knives from hitting other knives I would actually keep a separate collision and rigidbody disabled on every knife and enable it when it reaches 1, then just disable the entity so it “breaks”.

that’s a good alternative idea.

I could try to code that right now.