Implementing ability to shake items in a box

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
holophone3d
Posts: 13
Joined: Sun Aug 14, 2011 7:40 pm

Implementing ability to shake items in a box

Post by holophone3d »

I'm looking for some thoughts on how I could implement a new feature in my Windows Phone app HoloPhone3D:http://www.bepu-games.com/forums/viewto ... f=9&t=1405 In my last update, I created a pretty cool interative experience for the 'holographic' items in the box (watch the video in the other thread to see what I mean). Users can pivot the phone to make objects slide around and they can grab and flick objects as well. However, the one thing I didn't implement was the ability to 'shake' the phone and kick the objects around - as if they were in a virtual box. Since I'm just setting gravity equal to the value of the acclelerometer that mostly works to simulate gravity - but since there is no actual movement of the box, just changes in gravity, it's not really possible to shake and kick the objects around like you would expect. So two questions, first how would you suggest implementing this using only an accelerometer (most current gen Windows phones only have an accelerometer). Second, if more advanced phone orientation sensors would be required (available in the second gen phones coming out now) let me know your thoughts on that approach. Thanks in advance!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Implementing ability to shake items in a box

Post by Norbo »

how would you suggest implementing this using only an accelerometer
As the player shakes the phone, the accelerometer would detect significant changes which could be directly transferred into gravity. A quick, strong pull to the left on the phone would result in gravity pointing to the right, slamming things into the wall. It sounds like you might have been using the separated direction of gravity computed from the sensors, but the raw combined data should do the trick for shaking and gravity.

However, depending on the simulation, you may not want to feed every single acceleration into shake-gravity. There's no technical reason why you should or shouldn't; it would just be a design choice. Thresholds could be set for which the additional kick or shake would occur. It would be basically a simple 'shake gesture' recognizer. Since shaking the phone is not a very precise motion, the fact that they are discontinuous events as opposed to continuous 'true' accelerometer inputs would be mostly imperceptible.
Second, if more advanced phone orientation sensors would be required (available in the second gen phones coming out now) let me know your thoughts on that approach.
Orientation detection isn't necessary for the main (linear) effect. If you wanted to introduce nonuniform gravity due to angular accelerations, then yes, this would come in handy- but then again, it's pretty difficult to spin your phone quickly without dropping it :)

To introduce such nonuniform accelerations, you could find the axis of rotation, pick a spot in your space to represent the center of the rotation, and apply an extra impulse to objects based upon their positioning relative to the axis.
holophone3d
Posts: 13
Joined: Sun Aug 14, 2011 7:40 pm

Re: Implementing ability to shake items in a box

Post by holophone3d »

Norbo wrote:It sounds like you might have been using the separated direction of gravity computed from the sensors, but the raw combined data should do the trick for shaking and gravity.
Can you clarify what you mean by separated? Right now I am setting gravity to the raw value of the accelerometer, it's just that even a relatively big value just doesn't translate to the effect you'd expect because it's only for an instant. The best example is how if ou pulled a box towards you quickly with dice in it and then stopped it, the dice would hit the glass on top the box. Even setting the raw - relatively high - gravity values just doesn't make it jump since by the time it starts to respond, normal gravity is already acting on it again. I believe it has to do with the fact that in real life the box is moving and in my app, only gravity is changing. Perhaps I can detect these accelerometer spikes and then apply impulses to the objects in kind of a predefined way? Thoughts?
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Implementing ability to shake items in a box

Post by Norbo »

Can you clarify what you mean by separated?
The phone sensors can be used to determine the direction of gravity separate from all other accelerations. If only the direction of gravity is used, then shaking effects will not be taken into account. It basically acts as an attitude/orientation detector. This device-separated gravity and nongravity acceleration is used in the second demo below.

However, as I also explain later (the first demo below), the reason why it doesn't seem to work as expected is probably just scale.
I believe it has to do with the fact that in real life the box is moving and in my app, only gravity is changing.
It should not matter if the accelerometer is providing correct values.

The BEPUphysics Space is considered to be following the device. So, within the simulation, all values are local to the device. This means the mesh representing the world is static and unmoving (because it is, in fact, unmoving relative to the device).

So with a real box, if your frame of reference is that of the person shaking the box, you do indeed see the box moving. But imagine you had a camera attached to the inside of the box. The box would be stationary and the contents of the box would appear to be flying all over the place from changing accelerations. The source of those accelerations is the person-space gravity and shaking transformed into the the box's local space.

It's a shift in perspective, but it is equivalent.

The issue may simply be that the forces exerted by shaking are too small to register at the low scales involved. If the gravity is set to the acceleration scaled by 500, you can start to see objects behave more like you expect. You can pile them into a corner and then launch them up and out with a quick shake. That motion still happened at lower scaled gravity, but it only resulted in a tiny amount of displacement.

I made a little demo in the BEPUphysicsPhoneDemo to demonstrate this:
ShakeDemo.cs
(3.93 KiB) Downloaded 211 times
The above pure raw-input method does have side effects. Everything is consistent, but maintaining that consistency requires some really high relative gravity. It's almost as if there's tiny objects in a phone-sized box ;)

It uses a 120hz update rate to help out stability. This isn't really feasible for larger simulations on the phone, and I assume you'd prefer more controllable motion, where the gravity is more manageable but shaking still has a significant effect. This would break the consistency, so we need another approach.

So, what we'd like to do is separate the gravity direction (or just as usefully, orientation) from the raw acceleration data, leaving us with the remaining non-earth-gravity accelerations. Then, we could scale gravity a bit to make it just fast enough for the gameplay and separately scale the nongravitational accelerations to a greater degree to introduce usable shakes and kicks.

With a single datapoint from absolutely raw linear accelerometer input that includes some unknown gravity direction, it is not feasible to know exactly what part of the acceleration is due to shaking and what part is due to earth's gravity. So we have to cheat.

The 'simple shake gesture recognizer' approach I mentioned earlier is one option. A simple implementation that just watches magnitude could determine when a sufficiently large shake in the gravitational direction occurred, at which time some random impulses could be applied. A bit more work could be put in to extract some rough direction, perhaps based on previous frame gravities. If you notice a strong change in direction between frames, you can assume it to be a shake attempt. This would require extra care with thresholds and smoothing to avoid introducing shakes due to sensor noise.

The most robust way to separate gravity from nongravity is to leverage additional sensors. If we assume the phone supports Motion, all the work is done for us. I made another demo showing this:
ShakeMotionDemo.cs
(4.23 KiB) Downloaded 232 times
It works on my old Samsung Focus as expected; most of the other old phones should be able to run it too. Newer phones with fancier sensors will get higher quality data. If you encounter a phone which doesn't support Motion, the shake gesture recognition approach could be used as a backup.
holophone3d
Posts: 13
Joined: Sun Aug 14, 2011 7:40 pm

Re: Implementing ability to shake items in a box

Post by holophone3d »

Whoa! Amazing! I'm not sure you could be anymore specific! I really appreciate your the time you spend to provide this level of detail! I'll try out this approach right now and let you know how it goes. :D
Post Reply