# Help with non physical collisions

hi everybody,
i have to implement collision to make my character collect coins and stuff…
i have the classes player and coin, both with a spatial inside and the player that has a betterCharacterControl.
I would like to keep coins not physical, so what’s the easiest way to implement this kind of collision? What exactly should i “bound” ?

i have read a lot of similar topic but seems i can’t get help from them…

The simplest way would simply be to check the distance between your character and the coins, if it is lower than a certain value then you have “collected” it. You don’t need to worry about bounds in this case.

1 Like

is it the easiest way? It seems so simple and i’ve never thought about it xD
Then what could be most efficient way to check distances if i have a node for the character and i’m planning to have a node for all the coins (then a node for each type of collectables) ?
Thanks for help @Neilos

Just check the distance for each single object, e.g.:
if(targetPosition.subtract(playerPosition).length() < 1){
// collided
}

There is always room for optimisations but it will all depend on your scene setup.

Not necessarily the best way

With that said, computing distances for moderate amounts of items is going to be very fast.

excuse me for the dummy question,
i tried to print :
playerNode.getWorldTranslation().distance(coin.Spatial().getWorldTranslation()) and it prints the distance, decreasing it when get closer, but if i put all that in a condition (example < 0.5f ) it gives me always true

You’d have to show your code I’m a little confused as to what you have done

//playerNode is a node with only the player character attached

float a=playerNode.getWorldTranslation().distance(coin.Spatial().getWorldTranslation());
if(a < 1f );
System.out.println("collides because distance is "+a);

i walk from towards the coin and i pass through it, and this is the output:

``````collides because distance is 16.355259
collides because distance is 16.25604
collides because distance is 16.156824
collides because distance is 16.057606
collides because distance is 15.95839
collides because distance is 15.859176
collides because distance is 15.759961
collides because distance is 15.6607485
collides because distance is 15.560926
collides because distance is 15.462449
collides because distance is 15.363275
collides because distance is 15.264038
collides because distance is 15.164803
collides because distance is 15.065571
collides because distance is 14.966345
collides because distance is 14.867124
collides because distance is 14.767305
collides because distance is 14.668816
collides because distance is 14.569637
collides because distance is 14.470401
collides because distance is 14.371163
collides because distance is 14.271933
collides because distance is 14.1727085
collides because distance is 14.073489
collides because distance is 13.973678
collides because distance is 13.875189
collides because distance is 13.776014
collides because distance is 13.676779
collides because distance is 13.577547
collides because distance is 13.47832
collides because distance is 13.3791
collides because distance is 13.279884
collides because distance is 13.180672
collides because distance is 13.081464
collides because distance is 12.982259
collides because distance is 12.883056
collides because distance is 12.7838545
collides because distance is 12.684656
collides because distance is 12.585459
collides because distance is 12.486262
collides because distance is 12.387068
collides because distance is 12.287875
collides because distance is 12.188683
collides because distance is 12.089492
collides because distance is 11.990302
collides because distance is 11.891114
collides because distance is 11.791927
collides because distance is 11.69274
collides because distance is 11.5935545
collides because distance is 11.494371
collides because distance is 11.395187
collides because distance is 11.296006
collides because distance is 11.196826
collides because distance is 11.097645
collides because distance is 10.998467
collides because distance is 10.899289
collides because distance is 10.800114
collides because distance is 10.700939
collides because distance is 10.601767
collides because distance is 10.502593
collides because distance is 10.403423
collides because distance is 10.304254
collides because distance is 10.205085
collides because distance is 10.105919
collides because distance is 10.006754
collides because distance is 9.90759
collides because distance is 9.808429
collides because distance is 9.709269
collides because distance is 9.610111
collides because distance is 9.510388
collides because distance is 9.41196
collides because distance is 9.312837
collides because distance is 9.213655
collides because distance is 9.114476
collides because distance is 9.015304
collides because distance is 8.916139
collides because distance is 8.81698
collides because distance is 8.717826
collides because distance is 8.618678
collides because distance is 8.519534
collides because distance is 8.420396
collides because distance is 8.3212595
collides because distance is 8.222128
collides because distance is 8.123
collides because distance is 8.023875
collides because distance is 7.9247546
collides because distance is 7.825638
collides because distance is 7.7265244
collides because distance is 7.6274147
collides because distance is 7.5283084
collides because distance is 7.429206
collides because distance is 7.330108
collides because distance is 7.231014
collides because distance is 7.1319246
collides because distance is 7.032839
collides because distance is 6.9337583
collides because distance is 6.8346825
collides because distance is 6.735611
collides because distance is 6.6365457
collides because distance is 6.537485
collides because distance is 6.4384303
collides because distance is 6.3393817
collides because distance is 6.240339
collides because distance is 6.1413026
collides because distance is 6.0422735
collides because distance is 5.943251
collides because distance is 5.844237
collides because distance is 5.74523
collides because distance is 5.6462317
collides because distance is 5.547242
collides because distance is 5.4497714
collides because distance is 5.3503256
collides because distance is 5.2506146
collides because distance is 5.1523733
collides because distance is 5.0534515
collides because distance is 4.9544835
collides because distance is 4.8555307
collides because distance is 4.7565994
collides because distance is 4.657688
collides because distance is 4.558798
collides because distance is 4.4599285
collides because distance is 4.36108
collides because distance is 4.2622538
collides because distance is 4.1634493
collides because distance is 4.0646687
collides because distance is 3.965913
collides because distance is 3.8671837
collides because distance is 3.768483
collides because distance is 3.6698122
collides because distance is 3.5712125
collides because distance is 3.472613
collides because distance is 3.3740482
collides because distance is 3.2755246
collides because distance is 3.177046
collides because distance is 3.0786169
collides because distance is 2.9802418
collides because distance is 2.8819268
collides because distance is 2.783396
collides because distance is 2.6858935
collides because distance is 2.587791
collides because distance is 2.489725
collides because distance is 2.3917677
collides because distance is 2.2939355
collides because distance is 2.1962428
collides because distance is 2.098706
collides because distance is 2.001833
collides because distance is 1.9038167
collides because distance is 1.8064103
collides because distance is 1.7099195
collides because distance is 1.6144952
collides because distance is 1.5186269
collides because distance is 1.4231299
collides because distance is 1.3281753
collides because distance is 1.2341388
collides because distance is 1.1412921
collides because distance is 1.0488719
collides because distance is 0.95778745
collides because distance is 0.8685283
collides because distance is 0.781718
collides because distance is 0.69826216
collides because distance is 0.61950994
collides because distance is 0.54748774
collides because distance is 0.48519546
collides because distance is 0.43681023
collides because distance is 0.40731364
collides because distance is 0.40089327
collides because distance is 0.4186094
collides because distance is 0.45766574
collides because distance is 0.51321125
collides because distance is 0.58053136
collides because distance is 0.65601027
collides because distance is 0.73714554
collides because distance is 0.822264
collides because distance is 0.91024894
collides because distance is 1.0003439
collides because distance is 1.0920267
collides because distance is 1.1849287
collides because distance is 1.278784
collides because distance is 1.3733974
collides because distance is 1.4686222
collides because distance is 1.5643466
collides because distance is 1.6604846
collides because distance is 1.7569658
collides because distance is 1.8537389
collides because distance is 1.9507598
collides because distance is 2.0479937
collides because distance is 2.1454113
collides because distance is 2.2429888
collides because distance is 2.3407063
collides because distance is 2.438547
collides because distance is 2.5364964
collides because distance is 2.634543
collides because distance is 2.7326753
collides because distance is 2.8308852
collides because distance is 2.9291644
collides because distance is 3.0275066
collides because distance is 3.1269312
collides because distance is 3.22456
collides because distance is 3.3229494
collides because distance is 3.4215736
collides because distance is 3.5202837
collides because distance is 3.6190658
collides because distance is 3.7179143
collides because distance is 3.816824
collides because distance is 3.9157908
collides because distance is 4.014811
collides because distance is 4.1138806
collides because distance is 4.2129974
collides because distance is 4.3121576
collides because distance is 4.4113584
collides because distance is 4.5105977
collides because distance is 4.609874
collides because distance is 4.7091837
collides because distance is 4.8085256
collides because distance is 4.907898
collides because distance is 5.007299
collides because distance is 5.1067266
collides because distance is 5.20618
collides because distance is 5.305658
collides because distance is 5.4051585
collides because distance is 5.5046806
collides because distance is 5.6042233
collides because distance is 5.703785
collides because distance is 5.803365
collides because distance is 5.902963
collides because distance is 6.002578
collides because distance is 6.102208
collides because distance is 6.2018533
collides because distance is 6.3015127
collides because distance is 6.4011846
collides because distance is 6.5008707
collides because distance is 6.600568
collides because distance is 6.7002773
collides because distance is 6.799998
collides because distance is 6.899729
collides because distance is 6.9994693
collides because distance is 7.0992193
collides because distance is 7.198979
collides because distance is 7.2995768
collides because distance is 7.398505
collides because distance is 7.4982576
collides because distance is 7.5980577
collides because distance is 7.697866
collides because distance is 7.7976785
collides because distance is 7.897496
collides because distance is 7.9973197
collides because distance is 8.097148
collides because distance is 8.196981
collides because distance is 8.296818
collides because distance is 8.396661
collides because distance is 8.496508
collides because distance is 8.596359
collides because distance is 8.696215
collides because distance is 8.796075
collides because distance is 8.895939
collides because distance is 8.995807
collides because distance is 9.095677
collides because distance is 9.195552
collides because distance is 9.295431
collides because distance is 9.395313
collides because distance is 9.495198
collides because distance is 9.595086
collides because distance is 9.694978
collides because distance is 9.794872
collides because distance is 9.894769
collides because distance is 9.994668
collides because distance is 10.09457
collides because distance is 10.194474
collides because distance is 10.29438
collides because distance is 10.394289``````

remove that semicolon

Basically what you have done is:

``````if (a < 1f) {
// do nothing
}
System.out.println("collides because distance is "+a);``````

i told you it was a dummy question

Some helpful advice:: learn to use the debugger to step through code and examine variables. You’d have probably found this issue in less time than it took to write forum post.

Hmmm I always use squaredDistance and compare it to a threshold…

Same concept, slightly faster to use squaredDistance.

I’m not sure whether that would be the most efficient way to do this, but you can always attach Ghost Collision Control Spheres etc to objects and report any collisions when they overlap.

and how could i use that?
@ia97lies

I’ll give you one guess how you modify the first code to use the second. You are allowed to use javadoc (in fact, encouraged to).