Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / box2d collision and forces

Author
Message
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 7th May 2011 18:29
My goal is to have an object blow up ONLY if it was hit with enough force, whether it be from falling from a certain height or another object hitting it.

My current method is subtracting the linear velocities between the two colliding fixtures, and if it's above a certain threshold, boom! I've searched online for doing this in Box2D but everything seems to use some kind of impulse command which I don't an alternative to with this plugin. My next thought is to look at friction joints and see what those can do, but I am unclear on how to use them.

The other thing I would like to do is have an object continue passing through another object if it has enough force. Say I throw a ball very hard at a wall. The ball will burst through the wall destroying it, and while it should lose some force from the impact, will continue moving in its direction rather than bouncing off from the collision. I really have no idea about this one.

Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 8th May 2011 12:57 Edited at: 8th May 2011 13:21
You can improve the accuracy by using "b2GetBodyLinearVelocityFromWorldPointX/Y" on each body with the position of one of the contact points ("b2GetContactManifoldWorldPointX/Y"). You can then subtract these velocities to see the "force" of the collision. You could also multiply the velocities by the masses of the bodies to see if that produces better results. I'll see if I can write some example code to make it easier.

edit:


As for breaking through a wall: you could store the balls velocity somewhere (in an array, or you could use a DBPro vector2 and store the ID of the vector with the body using b2SetObjectUserData). Then when you detect one of these collision where something breaks using the method above. You restore the old velocity to the body which didn't break, possibly slowing it down a bit based on how difficult the first body was to break.

[b]
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 8th May 2011 20:32
Thanks for the collision force code, that's just saved me a job!

Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 9th May 2011 12:01
kudos, seems more accurate than mine.

BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 8th Jun 2011 10:57
Just to let you know, this works brilliantly. The difference in a game is huge when you have sounds based on collision force.

baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 8th Jun 2011 13:44
Just what I needed! Thanks Diggsey!!

My worms clone (started when I first got the plugin) is coming along nicely...

Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 8th Jun 2011 20:05
I've noticed if one object just barely brushes passed another object, it'll register as being hit with a lot of force when it's really not even been hit directly enough to show any collision response. I haven't really spent any time yet tweaking the code though.

Login to post a reply

Server time is: 2026-07-10 22:54:22
Your offset time is: 2026-07-10 22:54:22