adaptive range based on some parameters

R_Gol's icon

This is an idea to develop and I wonder how to implement this.

I have data from two users touching and moving hand on sensor.

x y and flag on off for touched screen.

In this patch I have a default range of 30 that only if users are inside that range a sound is playing.

I would like to make the range adaptable based on some conditions.

assuming range set to 30.

user can choose time windows to look at and increment/decrement steps

time window set to 2 sec - increment/decrement step set to 3.

if user are within range in the 2 sec window, another 2 sec window start - if they pass the second window then range is reduced to 27. if they continue to hold together inside the range for another 2 second and another 2 second - range is reduced by another 3. if not (users went out of range) then range is increased by 3.

so in order to reduced range user should manage to be inside 2x window time while to increase range used should be out of window only for 1 window time.

Is this make sense?

How can I make this?

Max Patch
Copy patch and select New From Clipboard in Max.

R_Gol's icon

Here is the system I came up with which looks over complicated..

Max Patch
Copy patch and select New From Clipboard in Max.

Edited patch

Source Audio's icon

as first remove all that complicated distance detection.

you have 3 values: 2 touch states and distance.

In fact input list can not exist without users touching the screen,

so if you made this proper in first place, you would not need touch state at all.

like this :

Next thing is to define maximum number of increase- decrease steps

to stop the process once the limits are reached.

And next question - when does detector return to originally set value = 30 ?

And why should it do so ?

R_Gol's icon

to stop the process once the limits are reached.

limits are 2 and 100

And next question - when does detector return to originally set value = 30 ?

And why should it do so ?

it should return to orignal value if reset button has pressed or when patch is first loaded.

This adaptive range should be able to activate/deactivate on a button press

R_Gol's icon

What set the maximum distance? why is more then 100?

R_Gol's icon

The thing is not the increase range if both user hands are not touching because then we can assume they are not in the game.

if only 1 user hand is on screen we can assume game is running but they are outside range. if both user hand is ON we can assume the game is running and they are inside range. I hope this make sense

Source Audio's icon

I mean this are not technical but logical questions.

if range is 30, users made 28 = IN RANGE

and stay there, after 4 seconds range will drop to 27 - means users are out of range .

What then ?

after 2 seconds they are in range again if they don't move ...

and so on.

R_Gol's icon

I mean this are not technical but logical questions.

if range is 30, users made 28 = IN RANGE

and stay there, after 4 seconds range will drop to 27 - means users are out of range .

What then ?

after 2 seconds they are in range again if they don't move ...

and so on.

I see what you mean.

But if range is 30 and users inside range and the position of thier hand is equal to 15 then after 4 second it drops to 27 and they are still in range. the range 30 is the boarder range but user can be more precise inside the range.

so range drop to 27 but user position is still 14 so range in decrease in each 4 seconds. Is this a prolem?

R_Gol's icon

the thing is that instead of constant range of 30 that inside this range there is an output (sound or whatever) the range is decreasing in x interval every x seconds as long user in that range. so if the position of users is 14 but range is 30 it will decrease until it will which 13 or less then user must be more precise in order to have the output sound. otherwise range will increase if they are outside the range more then x seconds

Source Audio's icon

"What set the maximum distance? why is more then 100?"

you have square 100x100

what is maximal possible distance within the square ?

R_Gol's icon

what is maximal possible distance within the square ?

200***

TFL's icon

141.42... is the size of the diagonal of a square of size 100x100.

Here's what I would do to reduce the range while players are in-range.

This is the base logic, you still need to handle the case where the range reaches its minimum, and how to automatically reset the range reduction.
Max Patch
Copy patch and select New From Clipboard in Max.

But from a gameplay perspective, I would more continuously decrease the range, like -0.2 every 100ms (which will give a -2 reduction in 1 seconds), and find a way to display that 2 seconds delay before the range starts reducing.


R_Gol's icon

The problem in the example you shared that in order to increase by another 2 user should be outside of range then go inside range again..

this not make sense? (apart of the probably inefficient of this)

Max Patch
Copy patch and select New From Clipboard in Max.

Source Audio's icon

in your logic problem is that you think only about state when users are closer

then set threshold: in range.

what if they are outside of range ?

What makes the difference for you if users NEVER were in range

or they got kicked out because that decreament took place ?

Source Audio's icon

you should go back to primary school.

maximal distance within a square is diagonal.

for 100x100 = 141.421356

or in max

expr sqrt(pow($f1, 2)+pow($f2, 2))

R_Gol's icon

what if they are outside of range ?

ok I think I understand what you say.

if they are outside of the range set initial (30) then it won't increase. but if range was already decrease due to long contact and now range is set to 20 - when users outside the new range of 20 it will increase until reaching 30 again in time window and value decided.

so If to try and write it again -

range set to some number - let say 30.

user start play and both user hands on screen and their actual distance is 15. in that case timer running and range is decrease in 2 units every 4 seconds. assuming range was reduced until 18 then their hand position yeld a distance of 25 which is outside the new range of 18. then every 2 sec range is increase in value of 2 .

If users outside initial range - no effect on increasing range. if users outside new range (that is lower then initial range) - range will increase.

if users inside range - range will decrease until met the limit of 2.

Is this more clear? is this possible to make?

Source Audio's icon

That process could work only in

a condition where users keep touching the screen without interruption.

Once in range keep decreasing the range till they get kicked out,

start increasing the range till they manage in again.

reset the process when any of the users lifts hand off screen.

And stop when limits get reached.

why 4 seconds down and 2 seconds up ?

R_Gol's icon

why 4 seconds down and 2 seconds up ?

to make it more difficult to decrease range (need to stay longer inside range to decrease it)

That process could work only in

a condition where users keep touching the screen without interruption.

Once in range keep decreasing the range till they get kicked out,

start increasing the range till they manage in again.

reset the process when any of the users lifts hand off screen.

And stop when limits get reached.

Make sense for me

Source Audio's icon

here is rough example using mouse state instead of touch state.

have no time now to make 4 sec / 2 sec

Max Patch
Copy patch and select New From Clipboard in Max.

as you requested initial range turns process off

TFL's icon

Here's my attempt at the increasing/decreasing value depending of if users are in range or out of range.

Max Patch
Copy patch and select New From Clipboard in Max.

I added the initial delay depending of if we are in or out of range.

You still need to add the mechanics to stop/reset when one user is not touching the screen or when the game ends.

R_Gol's icon

here is rough example using mouse state instead of touch state.

Thanks!

Something odd happens when value is close to each other (toggle on off)

Screen Recording 2025-01-06 at 18.53.29.mov

TFL's icon

Something odd happens when value is close to each other (toggle on off)

This is absolutely normal:

  1. distance gets out of range

  2. so the range increases

  3. but then distance becomes inside the range

  4. so the range decreases

  5. but then distance becomes out of range: back to 2.

At this point it's a matter of how you want your game to feel and play.

R_Gol's icon

This is absolutely normal:

ok I see but this was toggling even when input was not change so it is can be an issue.. Is this fixable? other idea how to make this positive.negative feedback loop based on the above?

Is there a way to avoid that on off toggling in that scenario

R_Gol's icon

here is rough example using mouse state instead of touch state.

This working good!

Question - Is it possible that when distance is out of range it won't manually back in range rather only when users actively back in range? so the manual on off toggling will be avoided?

another question -

how is possible to manual change this increase decrease part?

TFL's icon

Is there a way to avoid that on off toggling in that scenario

You could make the range get bigger only when the distance is bigger than at least one increment.

Like if range is 26 and distance is 27, nothing happens. Distance has to become at least 28 so the range grows to 28. I would even add a small margin, like the distance have to get above at least 1.5 times the increment. In my example, the range wouldn't grow to 28 until the distance is at least 29.

Here's how I do it:

Max Patch
Copy patch and select New From Clipboard in Max.


how is possible to manual change this increase decrease part?

use [int] (or the shortcut [i]) instead of [t]) and you'll get an extra inlet to set the number as you want.

Source Audio's icon

This is first ask then discover what you asked for...

I mentioned from beginning that under your conditions

if users remain at same position,

it will simply toggle on / off while inc or dec takes place.

Again - this is logical and not technical problem.

Check well what you want also in respect to the whole story,

I know that this is for your Animal and (or) Play Song Test , where distance

is not only triggering playback on / off, but also control volumes etc.

In Animal test you even use 2 distance thresholds .

But instead of finishing that 2 tests - which are currently NOT in working condition,

you add more and more traps.

And at least for me this does not make much sense.

I mean ok if you want to kick users not to stay at same point long time,

and gradually decrease threshold to challenge them to move closer,

but to increase threshold again is confusing.

Also - do you really think users will keep touching same spot for such a long time ?

if they move fast enough all this scenario will not have time to even start

R_Gol's icon

but to increase threshold again is confusing.

This in case user have hard time to stay in the decrease range..

Also - do you really think users will keep touching same spot for such a long time ?

if they move fast enough all this scenario will not have time to even start

I guess I should first try the basic idea and see how it feels..

This is first ask then discover what you asked for...

I gess I should see how the basic system works then see if or what adaption is needed...

TFL's icon

I guess I should first try the basic idea and see how it feels..

I gess I should see how the basic system works then see if or what adaption is needed...

YES and YES, always do that in game design and UX. As Source Audio suggested, these are not Max-specific problems, they're interaction design problems.

R_Gol's icon

How can I make a minimum of distance 5 where it will not consider out of range anymore?

Max Patch
Copy patch and select New From Clipboard in Max.

Source Audio's icon

I looked short on that chaos and closed it immediately.

If you have a question, post minimal construct that easily deals with what you want.

And post a question in understandable form.

how should one understand this :

"How can I make a minimum of distance 5 where it will not consider out of range anymore?"

I don't get it.

R_Gol's icon

Is this looks better?

edit - added interpolation

Max Patch
Copy patch and select New From Clipboard in Max.

"How can I make a minimum of distance 5 where it will not consider out of range anymore?"


Right now only if users distance is less then 2 the song will play without any pausing because users are within the minimum step and the range can't decrease lower then that.

I want to set that point to be 5 instead of 2. So if users distance is less then 5 (this should be manual change if needed) the song will play constantly.

another point is if range is 24 and distance is 25, nothing happens. Distance has to become at least 26 so the range grows to 26. This is in order to prevent the on off toggling I pointed earlier in the thread

edit: Also the change of the range should be with interpolation between one value to the other instead in a discrete steps

TFL's icon

I think I answered this in my previous answer already. See the number labeled "Min range" , and also the one labeled "Extra margin that the distance has to reach before starting increasing the range again".

Also, I changed the duration between each range change to 500ms so it's easier to test on my side, but you can easily set it back to 2000.

In my example patch, that interval delay is separate from the initial delay required before the range starts increasing or decreasing. And you can set a different initial delay before the range increases or decreases (I set it is to a 2000ms delay before start decreasing the range, and 4000ms before start increasing the range).

Also, instead of interpolation, you could set much smaller steps at smaller intervals, like ±0.1 every 100ms (same speed as ±2 every 2000ms, but more frequent steps), as I suggested in an earlier post.

Source Audio's icon

that last upload is by no means any better.

upload ONLY stuff that you asked for.

before you proceed fix that max distance to 0 - 100

I posted allready about that in another thread.

if you want to set floor for distance then insert something like this

R_Gol's icon

I edited the patch and add your fix. Is this better?

Max Patch
Copy patch and select New From Clipboard in Max.

Source Audio's icon

No.

I allready have that part of the patch - the only one needed.

I even made it for you together with local mouse testing included etc

and you post this mess ?

what should I do with this :

I can't even completely see it on 2560x1440 screen

.......

To help you I need exact behavior of that dynamic distance range, whatever you call it.

Explained from beginning to the end, and considering all conditions.

for example to start with :

when users are below set threshold there is a difference between threshold itself and current distance.

You want to decrease threshold as long as users distance is lower than threshold.

you want to do it in this or the other way.

explain that in detail.

does that difference play a role ?

is threshold decrease speed dependent on the difference or not ?

.....

Once threshold is lower than distance again ( or equal as well ????)

result is negative, playback stops.

what then ?

don't tell me it has to rise again -> you will again get into up/down trap.

.....

next factor is when users distance is lower than 5. (or equal as well ???)

then NO change in threshold takes place. I understood that.

..........

don't forget to write what happens when both users are NOT touching the screen.

Reset ?

.......

Did you consider that you control volume of sound playback

by scaling distance ?

And one needs to adapt it to changing threshold - distance relation ?

if current threshold is 34 and current distance is 32 what value should volume have ?

........

I need no patches, only detailed explanation. ok ?

R_Gol's icon

You want to decrease threshold as long as users distance is lower than threshold.

you want to do it in this or the other way.

yes, if users below threshold decrease it in x value with interpolation rather discrete steps.

does that difference play a role ?

is threshold decrease speed dependent on the difference or not ?

If this is not complicated to make it sounds as a good option to add. But not necessary for now.

next factor is when users distance is lower than 5. (or equal as well ???)

then NO change in threshold takes place. I understood that.

Yes. 5 or other value that can be manual change.

Once threshold is lower than distance again ( or equal as well ????)

result is negative, playback stops.

what then ?

don't tell me it has to rise again -> you will again get into up/down trap.

I have no clear idea. Any suggestions? assuming I want to avoid the up/down trap.

don't forget to write what happens when both users are NOT touching the screen.

Reset ?

If no touch above x time then reset - otherwise - continue with same threshold

if current threshold is 34 and current distance is 32 what value should volume have ?

The volume should be effectedc from the new threshold (the adaptive threshold)

I need no patches, only detailed explanation. ok ?

OK!

Source Audio's icon
The volume should be effected from the new threshold (the adaptive threshold)

I am not sure how well this will work.

I will try and let you know.

I think it wouldn't be bad to use scaled ramp for threshold decrease.

if thresh is 50, users at 10, then it would take looooong time to make

threshold change effective.

If one scaled it to more difference = faster decrease ?

and simply forget about lifting threshold again.

that's what I would do.

R_Gol's icon

I think it wouldn't be bad to use scaled ramp for threshold decrease.

if thresh is 50, users at 10, then it would take looooong time to make

threshold change effective.

If one scaled it to more difference = faster decrease ?

This sound right!

and simply forget about lifting threshold again.


I would like to have this option so if users are failed too much when threshold is small it will give them more chance when threshold will increase..

Source Audio's icon

they can not fail too much if one would reset

threshold when they don't touch screen.

And threshold decrease STOPS when it gets same or lower then current distance.

sorry, but I am not willing to get into raising threshold again.

Accept it or leave it all together.

R_Gol's icon

sorry, but I am not willing to get into raising threshold again.

Accept it or leave it all together

Accepted!

Source Audio's icon

I will post later anoher tester.

It is actually simple if one sets clear rules.

Source Audio's icon

Here is test patcher, it does both decrease and increase.

move left slider to simulate input distance

Distancer.maxpat
Max Patch


R_Gol's icon

This looks so good... I'm just amazed..!

Few question to understand better - > Yellow bar represent the live distance between users

Blue bar represent the threshold that is updated based on the time we set in "reset delay" ( 3 sec).

Red bar is the default range threshold

I don't get so much the behavoiur of the pink bar (volume control)

It should be that the more close the yellow bar (the acctual distance) to the blue bar ( the adaptive threshold) volume should be closer to 1 while the more far the yellow bar from the blue bar volume should be 0, isn't it?

R_Gol's icon

Sorry for asking something that it probably crystal clear for you but no much for me..

How do I connect my data from the two sensor (UDP 2222 and 2223) to the patch you make?

Max Patch
Copy patch and select New From Clipboard in Max.

Source Audio's icon

Violet slider represents volume that you use to

amplify playback level depending on relation

threshold - distance - floor.

.........................................

In the test runner patchers I've sent to you

you have optimised OSC receiving patch.

from that it is easy to create switch if both users touch the screen or not,

and 4 axes scaled to 0 - 100.

when all stuff needed for recording and display get removed :

........................................

but I want you to first test this without any other input but mouse

in order to check what it does and what should be changed.

I had no time to post all rule details, same as you never did !

so just test and enjoy ...

Joke by side

reset delay time means how long it takes when users input is = 0

to reset follower threshold to threshold.

inc/dec start delay means how long it takes till

follower starts to move when conditions are met.

it should work quite ok,

what needs to be tested is scaled chasing speed depending on difference

and maybe if conditions to start chasing need some adjustments...

R_Gol's icon

OK Thanks, I Must check it with some sound source so I can clearly hear and understand the behavior. Will do so

R_Gol's icon

I have question - See capture as example:

Distance is 21.7 and threshold is 27.7 . Play is 1 (should play) but the pink bar shows 0 meaning no sound will be heard.

What am I missing?

Source Audio's icon

you are missing that volume is scaled by potentiometer

which acts between floor (set at 3 now) and follower (blue )

which is at 27.7.

yellow is at 21.7 means very low.

you can set scale to linear.

here is new version with a little fix for playback anyway.

actually play is also dependent of users touching screen or not

I did not take it here at al into play switch,

all was only about thresholds etc

here it is coupled.

Max Patch
Copy patch and select New From Clipboard in Max.

Also volume needs to be reflected to avoid abrupt changes when playback switches.

R_Gol's icon

Ok I got confused.

I need to try it with sound. will do so first

Also volume needs to be reflected to avoid abrupt changes when playback switches.


This can be done by using line object?

R_Gol's icon

Do I connect the right way the sensor inputs?

Max Patch
Copy patch and select New From Clipboard in Max.

Source Audio's icon

I am not at home - can’t look at your patch

I can upload a version with audio player included tomorrow

R_Gol's icon

That will be appreciated! thanks