When Objects are Touching Variable

code
code_help

#1

Let’s get straight to the point. Does anyone know of a smooth method of setting a value to a number when an object is touching another but also setting it back to a different number when the objects are no longer touching? There was a method mentioned in the old joystick tutorial (can be found on THT’s YouTube channel), but that method isn’t very smooth because it switches back and fourth even when the objects are touching and it’s especially delayed when there are multiple sets of objects with their own touch values. I didn’t explain this very well, please ask for further clarification if need be.


#2

Have the variable be set when the distance between the objects is to the point that theyre touching and have it change when it’s not.


#3

Wow, the answer was much more simple than I expected, thanks! I didn’t think to avoid using the “when touching” rule condition and replace it with coordinates completely. The only downside is having to change it if I change the sprites’ sizes.


#4

It seems like this was solved, right?


#5

More or less solved, yes. Though I wouldn’t mind hearing other approaches since I’m trying to make handling as smooth as possible.


#6

Not necessarily, just include the object’s sizes in the calculation

When Touching = 1
    Set Touching = 0

When Distance < (Object1 Radius * (Obj1 size / 100)) + (Obj2 Radius * (Obj2 size / 100))
    Set Touching = 1

Of course you have to replace “distance” with the actual calculation: sqrt(deltaX^2 + deltaY^2)

Note: the two rules have to be in that order. Every frame the 1st rule resets the variable, but the second rule sets it right back if the objects are still touching. No other object’s rules “see” this quick toggling and the variable immediately resets (and stays) to 0 when the objects stop touching.


What’s wrong with my code?
#7

I understand everything apart from the distance and objects size and radius.
How do you calulate the objects size and radius and what does ‘ distance ’ refer to?
I.e if I were to use the square provided by hopscotch without adjusting its size what would be its radius and size?


#8

I’m pretty sure that radius would be the distance from an object’s center to its edge, and size would be the size as a % variable.


#9

I know that but how would you find that number. You don’t exactly have a ruler to measure that out.

So if I were just to use the square provided by hopscotch without adjusting its size with a set size block then it would just be 100?

What does this mean though?

Thanks for replying.


#10

Nindroid is correct.

Well, the size at 100% it’s 68 by 68 pixels (you have to determine that through testing).

If you wanted to calculated a radius for the square, you’d have to inscribe or circumscribe the circle. For an inscribed circle, the radius would be 68/2 or 34. For a circumscribed circle, it’s sqrt(68^2 + 68^2)/2

But the key point is that using a circle only approximates the object’s shape. Depending on the specific shape that could be good enough or not. And for some shapes, like a square there’s better methods than approximating them as a circle.


#11

That’s Pythagorean theorm. For a right triangle,
A^2 + B^2 = C^2
Or
C = sqrt(A^2 + B^2)

Delta just means “a change in”, so Delta X would be object 1 X position - object 2 X position


#12

Thank for Replying. I think I understand now.