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.

Code Snippets / Sliding box physics

Author
Message
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 26th Mar 2007 18:49 Edited at: 3rd Apr 2007 13:31
<Update: code updated 3rd April 07.>

Most recent code here:-




This code snippet demonstrates how to perform accurate collision between 3d boxes in a plane, with reasonably realistic linear and angular motion. No media needed, but you must have Sparky's sc_collision.dll in your user plugins folder.

Left mouse click - drag the boxes around
Right mouse click - rotate the boxes




For those who are interested, this method uses raycasting from the centre of each box to each of its four corners (in the plane of motion). The advantage of this method over spherical or elipsoid raycasting is that you get more realistic interactions between the corners of the boxes, and less calculations are required. The code also calculates the moment (or torque) applied to each box, so that objects posess angular as well as linear momentum.

Attachments

Login to view attachments
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 26th Mar 2007 18:57
That's really neat. The boxes don't turn so realistically, but I'm sure that's fixable. I'd look forward to this getting improved

TEH_CODERER
21
Years of Service
User Offline
Joined: 12th Nov 2003
Location: Right behind you!
Posted: 26th Mar 2007 23:41
Its so close to working! So close! It sometimes jumps and turns oddly like Zotoaster said. I hope you can fix it because I know if it was me it would be bugging me so much! Lol! If I got something so close to working I wouldn't be able to think about anything else until it was fixed! I'm far too obssesive...
Nice work! I've recently been thinking about how to go about making something like this without using a physics dll.

Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 27th Mar 2007 18:19 Edited at: 27th Mar 2007 18:20
Ok - it's still not perfect by any means, but this is hopefully an improvement. Boxes now retain linear and angular momentum, and the rotation is hopefully a bit more realistic. Also neatened the code up to make it more transferable to other applications.



See what you think.

TEH_CODERER
21
Years of Service
User Offline
Joined: 12th Nov 2003
Location: Right behind you!
Posted: 27th Mar 2007 19:41
Awesome! Much better! It really isn't far from perfect. The only time it messes up really is when I moved the cube too fast with the mouse.

Essence
22
Years of Service
User Offline
Joined: 12th Oct 2002
Location: The Netherlands
Posted: 29th Mar 2007 11:57 Edited at: 29th Mar 2007 11:57
Thats awesome!
I'm getting a pretty high framerate too =) 162 constant =p.

Maybe a nice idea to make the boxes fall over too?
If you can do this with ALL types of objects, it could be used for a physics wrapper XD

Current project: MiRevu Visual Gaming Engine
status: || Visuals: 60% || Physics: 100% || Gameplay: 40% || Audio: 15% || AI: 0% ||
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 3rd Apr 2007 13:42
Updated the code (see first post at top of thread). Collision nodes, previously 4 glued dummy objects at the corners of each box, have now been removed, with node positions now calculated mathematically. This should improve speed a little. My ancient pc now handles 100 sliding boxes quite comfortably.

@Essence: this is probably about as far as I will take this for now, as it works for my own needs. If a complete physics wrapper is what you need, there are already several options. I wasn't trying to reinvent the wheel - I just needed something simple for a specific task.

TEH_CODERER
21
Years of Service
User Offline
Joined: 12th Nov 2003
Location: Right behind you!
Posted: 3rd Apr 2007 16:08
Lol! I love 're-inventing the wheel' just to work out the code behind it all. I do it so often and it sometimes results in me getting something more suited to my needs or with added functionality or ease than the existing options. Admittedly 70% of the time I just end up with something the same or worse but its worth it to me! If you don't mind, I would like to at some point learn from and work on this. Of course you would get credit if I get something releasable.

Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 3rd Apr 2007 16:17
Feel free to use the code and take it in whatever direction you like. If you want any particular part of the code remarking more thoroughly, let me know.

TEH_CODERER
21
Years of Service
User Offline
Joined: 12th Nov 2003
Location: Right behind you!
Posted: 3rd Apr 2007 17:09
Thanks and thanks for the offer but I'm pretty good at coding and maths so I'm sure I'll figure it out.

Login to post a reply

Server time is: 2024-11-22 19:15:36
Your offset time is: 2024-11-22 19:15:36