I am wondering if it is possible to get the colliding objects (contacts) of a specific object at any time (example when clicked).
Thanks,
Alexandros [import]uid: 4416 topic_id: 2299 reply_id: 302299[/import]
I am wondering if it is possible to get the colliding objects (contacts) of a specific object at any time (example when clicked).
Thanks,
Alexandros [import]uid: 4416 topic_id: 2299 reply_id: 302299[/import]
Could you be a bit more specific? I read your question two ways:
If you mean; is it possible to get the specific points of contact on two objects when a collision event is fired, then I believe you would have to work out their relative positions and decide from their distance from each other where the contact point is. If they are complex objects this will be hard, but you could always decorate them with a few sensors - ie: place some small circular objects at important points of the larger object, fixed in place, and have table listeners on the sensors only.
If, however, you mean; is it possible to get the two objects which have collided only when the user touches the device, well this would be as simple as having pre and post collision listeners add and remove objects to and from a collision list. When the touch event listener is fired simply look at the list of currently touching objects, ie: those objects which have been added but not yet removed from the list.
If I’m way off target here, please clarify your question.
Matt. [import]uid: 8271 topic_id: 2299 reply_id: 6967[/import]
Thank you Matt,
I mean the 2nd case.
That is what I am doing, if I understand correctly:
On the preCollision event, I set a variable
On the postCollision event, I unset it.
However, the 2 objects, even when colliding, have the variable unset. In your words, object is added and immediately removed as well.
Hope this is clear…
Alexandros [import]uid: 4416 topic_id: 2299 reply_id: 6974[/import]
Did you have any success?
matt [import]uid: 8271 topic_id: 2299 reply_id: 7333[/import]
Well, to some extend.
Using the pre and post collision handlers cannot be done. So I used the plain collision event, and I keep on an array the colliding objects.
The only drawback is that if 2 objects collide for a second but then move far away from each othen, the program thinks they still collide…
Alexandros [import]uid: 4416 topic_id: 2299 reply_id: 7334[/import]
I had to tackle this same problem yesterday and managed it, basically, by having a timer run every 500 milliseconds which checks the distances between all the objects on the screen. I basically wanted to have a simple Puzzle Bobble proof of concept, so I created a box and dropped modified super balls into it. (I started with the Chains sample code.)
Here’s my code for checking distances and removing those balls which are touching. The super ball .png is 50px wide, so I wanted to check for balls which were <=50 apart. 50x50 (the square of 50) is 2500, so that’s what I’m checking for. I could have used math.sqrt, but hey…
– this function could be set up to check only one list, but filtered for type, or whatever. it checks 3 lists because I have 3 types of coloured balls
function checkTouch( event )
CheckList(list1)
CheckList(list2)
CheckList(list3)
end
– calls the above function to check for touching balls
timer.performWithDelay( 500, checkTouch, 0 )
– called to check all the balls of one colour
– this function exits when it finds touching balls, a simple optimisation, but visually not good with a slow timer (like I’m using)
function CheckList(list)
for i=1, #list do
if (CheckTouching(i, list)) then
break
end
end
end
– takes a ball’s index and checks to see if it is touching any of the other balls in the same list
– this function builds a list of touching balls and removes them if the list size is >2 - basically, 3 or more balls touching and they get removed
function CheckTouching(i, list)
local touches = {}
touches[1] = list[i]
– check for touches
for s=1, #list do
if (s ~= i) then
if (IsTouching(list, i, s)) then
touches[#touches+1] = list[s]
end
end
end
– were there >2 touches?
if (#touches > 2) then
– print(i…’ is touching ‘…#touches…’ balls’)
– remove the balls from the list in reverse order because their index will change (-1) when balls at lower indices are removed
for t=1, #touches do
for l=#list, 1, -1 do
if (touches[t] == list[l]) then
list[l]:Destroy()
table.remove(list, l)
end
end
end
return true – we did remove some balls, so wait for the next timer call before checking again
else
return false – we didn’t remove any balls
end
end
– checks the distance between two balls only
function IsTouching(list, i, s)
local xdist = math.abs(list[i].x - list[s].x)
local ydist = math.abs(list[i].y - list[s].y)
– this line could wrap the equation in a math square root call and compare it against the width of the superball image, but i couldn’t be bothered
if (((xdist*xdist)+(ydist*ydist)) <= 2500) then
– print(((xdist*xdist)+(ydist*ydist)))
return true
else
return false
end
end
[import]uid: 8271 topic_id: 2299 reply_id: 7360[/import]