THIS IS JUST A RAW PAGE, sorry
Here I added useful information, which I found in forum threads or found out by using Newton.
Later I want to improve this page for good overview. --Beauty
Note: Don't be confused of the beta remarks for Newton 2. The page content a little bit outdated. Newton version 2 is the default now and works well.
If you just want to use Newton for collision detection (and NOT physics), use the collision tools functions I mentioned.
If you want the physics too, you need to look at the contactCallback system. It is a very powerful system that will notify you when objects collide. Basically you create materials, and then choose to be notified when bodies of certain materials interact. Inside the callback you get pointers to the 2 specific bodies that are colliding.
Quick explanation: you create a material, let's call it "boxMaterial". Let's say you have 2 bodies that you make, and you set them both to this material.
Then you make a contactCallback and assign that callback for interactions of boxMaterial vs. boxMaterial.
Now, in your callback you have 3 functions:
They work like this:
When you call NewtonUpdate(), it moves bodies according to the forces acting on them. It then checks to make sure nothing is colliding. Now let's say it finds that your 2 bodies are likely to collide (their bounding boxes are intersecting).
First, it calls userBegin() to let you know that there is a good chance that the 2 bodies will collide. At this point it tells which 2 bodies it is talking about, and the m_Body0 and m_Body1 members of the contactCallback class are valid. At this point you can do some checks, and if you want to allow the collision, you return 1 from the function. If you want to let Newton know its OK to ignore the collision, you return 0.
Okay, so let' assume you've returned 1 from begin(). Now Newton will calculate the actual contact between the 2 bodies, and for each discrete contact between them, it will call userProcess(). In this function all of the member functions are available, so you can determine the location, normal, speed, etc. of the contact.
You can also return 0 or 1 to tell Newton to ignore or apply this contact.
Finally after all contacts have been processed, Newton calls the userEnd() function, to give you a chance to act one last time, now that you have had a chance to gather all of the data about the collision.
So generally the idea is to use userBegin() and userProcess() to gather data about the collision (and possibly choose to ignore the collision), and then use userEnd() to act on that data, by playing a sound effect, or spawning particle effects, etc.
Anyway, you get 2 pointers to the 2 bodies interacting from the start (userBegin()), so you can determine what to do from there.
You'll see that in userProcess() I check the ID (an int value) of the bodies to figure out which one is the conveyor belt. You can do something similar yourself to determine which bodies are interacting.
userEnd() is called even when only the -AABB of the 2 bodies collides, not a *true* collision.
The are 3 possible scenarios:
1) The bodies -AABB never overlap:
Nothing is called.
2) The bodies -AABB overlap, but the actual bodies are not called.
3) The bodies physically collide
userProcess() <- once for each contact
So you can see that if you want to do something only when a "true" collision has occured, you need to flag that in the userProcess() function, then check in userEnd() if it was a true collision, and if it was, make your particles, etc.
Look to the Newton API documentation for details.
Note: Their function names are NewtonContactBegin(), NewtonContactProcess(), and NewtonContactEnd()
Note for Mogre:
MogreNewt is a bit weird. Instead of the three methods, you only have two.
public virtual int UserAABBOverlap(ContactMaterial material, Body body0, Body body1, int threadIndex); public virtual void UserProcess(ContactJoint contact, float timestep, int threadIndex);
The first one is like userBegin and the second is like userProcess, apparently.
this thread might be a bit helpful.
Newton (and OgreNewt) have a very powerful collision response system. it is achieved through setting Materials on objects, and then creating callbacks for when objects of a specific pair of materials collide.
In the case of OgreNewt, you have to inherit from the ContactCallback class, and create 3 functions: userBegin, userProcess and userEnd.
Here's what happens:
During OgreNewt::World::update(), it moves all of the bodies, and then checks for collision. When it does, it calls your callbacks for you, like so:
userBegin is called if the -AABB of the 2 bodies in question overlap during this frame.
- This does NOT mean that they will necessarily collide, but there's a good chance. At this point, if you return "true" from the function, it tells Newton you want to keep processing this possible collision. If you return "false", it will not process the actual collision, and skip onto the next bodies to collide.
userProcess is called for every discreet contact between the 2 bodies.
- In most cases this will only be once, but in some cases you may get more than one contact per update(). Anyway, inside this function you can get lots of information about the contact (contact normal, speed of collision along the normal, tangent vectors, etc). You can also alter the collision here (set a custom friction setting for this collision, apply acceleration along the contact normal, etc). Again, returning "true" from this function tells Newton to accept this contact, "false" ignores it (the bodies will not collide, they will interpenetrate).
userEnd is called after all of the contacts have been processed.
- This gives you a chance to "act" on the information gathered above. Generally you would want to keep track of the contact info from the above calls, and in here do something with that data. So in your case, this is where you might want to tell your game logic that 1 object hit another object, and give it the point of impact, etc. Most people also play collision sound effects at this point, with the volume scaled by the largest contact speed found in the previous callbacks.
Note: Don't dispose Bodies inside of these callback functions. (Also not indirectly by call of other functions.) You can get exceptions. Instead save delete information somewhere and dispose the Body later (e.g. in the render loop).
Anyway, it's actually quite simple and extremely powerful. I use it a lot in Stunt Playground, so if you want to download the source [where?] to that and have a look, be my guest. There is also a simple demo that comes with OgreNewt (the conveyor belt demo). That demo alters the contacts, but you can see how you can use those same callbacks to simply register collision as well.
You have to use materials. All objects use the "default" material by default, so if you want to get a callback on ALL collisions, create a ContactCallback class, and register it with a MaterialPair of (default vs. default).
You can use Newton as a collision detection library if you want.
Just use the low-level collision functions, only create Collision objects, and no Bodies.
It allows for collision vs. collision object detection, and per-collision object raycasting.
Can I use the collision system separate from the dynamics?
Yes, the Newton collision system is one of the basic low level components of the engine and it is completely independent from the dynamics.
This property makes it possible to use Newton as a standalone low level collision package that is fully featured, fast and robust.
The part of the collision system exposed to the application is the low level, meaning the application can create any number of collision primitives and just call collision on any primitive pair, and Newton will calculate the contacts, contacts normal and penetration depth for those primitives. These properties together with the ability of Newton to stretch, and or scale any collision primitive along one or more arbitrary axis can be used by an application as the basis for implementing novel high level collision systems like for example swept volumes system without backtracking.
Other features supported by the collision system are:
- Extrapolation of contact calculation based of object velocity
- Closest distance to a point
- Closest distance between convex primitives
- -AABB calculation
These features are ideal for applications that already have a dynamics system in place whether it is proprietary physics or another third party dynamics system, but that are having a difficult time with the low level contact calculation of collision primitives or that do not have enough variety collision primitives to play with.
Newton collision system is based on our on proprietary algorithm that does not rely on previous values to calculate new results or existing closest features or GJK implementations, it is also robust mathematically correct and numerically stable.
If a collision object is completely inside of an other one (no contact of surfaces), then there is also a collision callback.
(tested with Callback method and also with checking by Iterator)
If you set Body.AttachToNode() then this will happen:
- if the Body is moved by Newton physics -> the position/orientation of the Ogre SceneNode will be updated automaticly
- if the SceneNode will be moved in Ogre -> the Body in Newton world will not be moved
- The position / orientation of Newton Body are related to the SceneNode.WorldPosition / SceneNode.WorldOrientation
- (Remember it, when you attach a SceneNode to a Body. If the parent of the SceneNode is not the RootNode you will get in trouble, because the SceneNode can be moved to somewhere else in the world)
- since Ogre 1.6 the World params are renamed to SceneNode._getDerivedPosition and SceneNode._getDerivedOrientation
- Use the debugger lines to validate size / position / orientation of the Body.Collision in Newton world
- In Newton 2 the method was renamed to Body.AttachNode()
Show debug lines of Newton collision hulls
OgreNewt::Debugger::getSingleton().init( m_SceneManager ); // call only once OgreNewt::Debugger::getSingleton().showLines( m_World ); // call every frame for updating
- With Mogre
MogreNewt.Debugger.Instance.Init(mySceneManager); // call only once // ... MogreNewt.Debugger.Instance.ShowLines(myNewtonWorld); // call every frame for updating // ... MogreNewt.Debugger.Instance.DeInit(); // call before disposing root/scene at application end MogreNewt.Debugger.Instance.Dispose(); // otherwise you'll get a ''System.AccessViolationException''
For speed up on multicore CPUs you can try out to set
World.setThreadCount(2) (or higher value)
--> 36% speed up (but this value can be different)
On CPUs with unshared cache the speed up is higher (said the author of Newton).
More information about the benchmark is this thread.
Keep in mind that multithreading may make your debug lines not display correctly.
An other speed up is possible for convex hulls. Use the tolerance parameter to reduce the count of vertices. (only available in Newton 2)
More information about the param you can read here.
Newton 2.0 (Archimedea) will be much more flexible for collision detection. Now the beta version is public. The wrappers for Ogre/Mogre are updated.
- Collision detection works without use of physically functions (this seems not to be possible with Newton 1.x)
- Some new collision features: in this thread
- Improvements: major additions, speed optimizations and support for multicore CPUs
- the collision primitive convex hull was much improved and now the processing is faster
- new functionality: World.collisionUpdate()
- needed if you want to detect collisions without using physics functions
- e.g. call it for every frame
- OgreNewt 2
- OgreNewt forum
- www.runehunter.phpnet.us/NewtonHelp.html - Newton API documentation
- www.ogre3d.org/addonforums/viewtopic.php?p=42695#p42695 - Mogre example code
- newtondynamics.com - website of the physics library
- Newton wiki
- Detect collisions of Axis aligned bounding boxes
- Minimal Ogre Collision
- OgreOde Collision Handling
- PhysX Candy Wrapper
- beastie - A collision detection library for Ogre (without other depencies than Ogre)
THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR COPYRIGHT LAW IS PROHIBITED.
BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
- "Collective Work" means a work, such as a periodical issue, anthology or encyclopedia, in which the Work in its entirety in unmodified form, along with a number of other contributions, constituting separate and independent works in themselves, are assembled into a collective whole. A work that constitutes a Collective Work will not be considered a Derivative Work (as defined below) for the purposes of this License.
- "Derivative Work" means a work based upon the Work or upon the Work and other pre-existing works, such as a translation, musical arrangement, dramatization, fictionalization, motion picture version, sound recording, art reproduction, abridgment, condensation, or any other form in which the Work may be recast, transformed, or adapted, except that a work that constitutes a Collective Work will not be considered a Derivative Work for the purpose of this License. For the avoidance of doubt, where the Work is a musical composition or sound recording, the synchronization of the Work in timed-relation with a moving image ("synching") will be considered a Derivative Work for the purpose of this License.
- "Licensor" means the individual or entity that offers the Work under the terms of this License.
- "Original Author" means the individual or entity who created the Work.
- "Work" means the copyrightable work of authorship offered under the terms of this License.
- "You" means an individual or entity exercising rights under this License who has not previously violated the terms of this License with respect to the Work, or who has received express permission from the Licensor to exercise rights under this License despite a previous violation.
- "License Elements" means the following high-level license attributes as selected by Licensor and indicated in the title of this License: Attribution, ShareAlike.
Nothing in this license is intended to reduce, limit, or restrict any rights arising from fair use, first sale or other limitations on the exclusive rights of the copyright owner under copyright law or other applicable laws.
Subject to the terms and conditions of this License, Licensor hereby grants You a worldwide, royalty-free, non-exclusive, perpetual (for the duration of the applicable copyright) license to exercise the rights in the Work as stated below:
- to reproduce the Work, to incorporate the Work into one or more Collective Works, and to reproduce the Work as incorporated in the Collective Works;
- to create and reproduce Derivative Works;
- to distribute copies or phonorecords of, display publicly, perform publicly, and perform publicly by means of a digital audio transmission the Work including as incorporated in Collective Works;
- to distribute copies or phonorecords of, display publicly, perform publicly, and perform publicly by means of a digital audio transmission Derivative Works.
- For the avoidance of doubt, where the work is a musical composition:
- Performance Royalties Under Blanket Licenses. Licensor waives the exclusive right to collect, whether individually or via a performance rights society (e.g. ASCAP, BMI, SESAC), royalties for the public performance or public digital performance (e.g. webcast) of the Work.
- Mechanical Rights and Statutory Royalties. Licensor waives the exclusive right to collect, whether individually or via a music rights society or designated agent (e.g. Harry Fox Agency), royalties for any phonorecord You create from the Work ("cover version") and distribute, subject to the compulsory license created by 17 USC Section 115 of the US Copyright Act (or the equivalent in other jurisdictions).
- Webcasting Rights and Statutory Royalties. For the avoidance of doubt, where the Work is a sound recording, Licensor waives the exclusive right to collect, whether individually or via a performance-rights society (e.g. SoundExchange), royalties for the public digital performance (e.g. webcast) of the Work, subject to the compulsory license created by 17 USC Section 114 of the US Copyright Act (or the equivalent in other jurisdictions).
The above rights may be exercised in all media and formats whether now known or hereafter devised. The above rights include the right to make such modifications as are technically necessary to exercise the rights in other media and formats. All rights not expressly granted by Licensor are hereby reserved.
The license granted in Section 3 above is expressly made subject to and limited by the following restrictions:
- You may distribute, publicly display, publicly perform, or publicly digitally perform the Work only under the terms of this License, and You must include a copy of, or the Uniform Resource Identifier for, this License with every copy or phonorecord of the Work You distribute, publicly display, publicly perform, or publicly digitally perform. You may not offer or impose any terms on the Work that alter or restrict the terms of this License or the recipients' exercise of the rights granted hereunder. You may not sublicense the Work. You must keep intact all notices that refer to this License and to the disclaimer of warranties. You may not distribute, publicly display, publicly perform, or publicly digitally perform the Work with any technological measures that control access or use of the Work in a manner inconsistent with the terms of this License Agreement. The above applies to the Work as incorporated in a Collective Work, but this does not require the Collective Work apart from the Work itself to be made subject to the terms of this License. If You create a Collective Work, upon notice from any Licensor You must, to the extent practicable, remove from the Collective Work any credit as required by clause 4(c), as requested. If You create a Derivative Work, upon notice from any Licensor You must, to the extent practicable, remove from the Derivative Work any credit as required by clause 4(c), as requested.
- You may distribute, publicly display, publicly perform, or publicly digitally perform a Derivative Work only under the terms of this License, a later version of this License with the same License Elements as this License, or a Creative Commons iCommons license that contains the same License Elements as this License (e.g. Attribution-ShareAlike 2.5 Japan). You must include a copy of, or the Uniform Resource Identifier for, this License or other license specified in the previous sentence with every copy or phonorecord of each Derivative Work You distribute, publicly display, publicly perform, or publicly digitally perform. You may not offer or impose any terms on the Derivative Works that alter or restrict the terms of this License or the recipients' exercise of the rights granted hereunder, and You must keep intact all notices that refer to this License and to the disclaimer of warranties. You may not distribute, publicly display, publicly perform, or publicly digitally perform the Derivative Work with any technological measures that control access or use of the Work in a manner inconsistent with the terms of this License Agreement. The above applies to the Derivative Work as incorporated in a Collective Work, but this does not require the Collective Work apart from the Derivative Work itself to be made subject to the terms of this License.
- If you distribute, publicly display, publicly perform, or publicly digitally perform the Work or any Derivative Works or Collective Works, You must keep intact all copyright notices for the Work and provide, reasonable to the medium or means You are utilizing: (i) the name of the Original Author (or pseudonym, if applicable) if supplied, and/or (ii) if the Original Author and/or Licensor designate another party or parties (e.g. a sponsor institute, publishing entity, journal) for attribution in Licensor's copyright notice, terms of service or by other reasonable means, the name of such party or parties; the title of the Work if supplied; to the extent reasonably practicable, the Uniform Resource Identifier, if any, that Licensor specifies to be associated with the Work, unless such URI does not refer to the copyright notice or licensing information for the Work; and in the case of a Derivative Work, a credit identifying the use of the Work in the Derivative Work (e.g., "French translation of the Work by Original Author," or "Screenplay based on original Work by Original Author"). Such credit may be implemented in any reasonable manner; provided, however, that in the case of a Derivative Work or Collective Work, at a minimum such credit will appear where any other comparable authorship credit appears and in a manner at least as prominent as such other comparable authorship credit.
UNLESS OTHERWISE AGREED TO BY THE PARTIES IN WRITING, LICENSOR OFFERS THE WORK AS-IS AND MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND CONCERNING THE MATERIALS, EXPRESS, IMPLIED, STATUTORY OR OTHERWISE, INCLUDING, WITHOUT LIMITATION, WARRANTIES OF TITLE, MERCHANTIBILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, OR THE ABSENCE OF LATENT OR OTHER DEFECTS, ACCURACY, OR THE PRESENCE OF ABSENCE OF ERRORS, WHETHER OR NOT DISCOVERABLE. SOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OF IMPLIED WARRANTIES, SO SUCH EXCLUSION MAY NOT APPLY TO YOU.
EXCEPT TO THE EXTENT REQUIRED BY APPLICABLE LAW, IN NO EVENT WILL LICENSOR BE LIABLE TO YOU ON ANY LEGAL THEORY FOR ANY SPECIAL, INCIDENTAL, CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES ARISING OUT OF THIS LICENSE OR THE USE OF THE WORK, EVEN IF LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
- This License and the rights granted hereunder will terminate automatically upon any breach by You of the terms of this License. Individuals or entities who have received Derivative Works or Collective Works from You under this License, however, will not have their licenses terminated provided such individuals or entities remain in full compliance with those licenses. Sections 1, 2, 5, 6, 7, and 8 will survive any termination of this License.
- Subject to the above terms and conditions, the license granted here is perpetual (for the duration of the applicable copyright in the Work). Notwithstanding the above, Licensor reserves the right to release the Work under different license terms or to stop distributing the Work at any time; provided, however that any such election will not serve to withdraw this License (or any other license that has been, or is required to be, granted under the terms of this License), and this License will continue in full force and effect unless terminated as stated above.
- Each time You distribute or publicly digitally perform the Work or a Collective Work, the Licensor offers to the recipient a license to the Work on the same terms and conditions as the license granted to You under this License.
- Each time You distribute or publicly digitally perform a Derivative Work, Licensor offers to the recipient a license to the original Work on the same terms and conditions as the license granted to You under this License.
- If any provision of this License is invalid or unenforceable under applicable law, it shall not affect the validity or enforceability of the remainder of the terms of this License, and without further action by the parties to this agreement, such provision shall be reformed to the minimum extent necessary to make such provision valid and enforceable.
- No term or provision of this License shall be deemed waived and no breach consented to unless such waiver or consent shall be in writing and signed by the party to be charged with such waiver or consent.
- This License constitutes the entire agreement between the parties with respect to the Work licensed here. There are no understandings, agreements or representations with respect to the Work not specified here. Licensor shall not be bound by any additional provisions that may appear in any communication from You. This License may not be modified without the mutual written agreement of the Licensor and You.