Accurate collision detection - Lua Experiment

Cralant - Custom level - from Windows
PlayEdit17 players liked this.Log in to like this level.

An improvement of my collision detection method, this time using a single query to find all objects in the square.
The coordinates, angle and bbox of the shape are then used to find the corners of the shapes at all angles.

Hopefully this doesn't lag, I have tried to use the most reliable settings while still hopefully keeping it relatively lag free.
This took a while to wrap my head around the mathematical aspect of the script but it seems to work perfectly now.

Leaving this one open for derivatives if someone wants to use the code although for most scenarios this method is probably overkill.
If anyone can think of code optimizations, please let me know. My brain has scrambled trying to figure this out.

Views: 3422 Downloads: 1221 Unique objects: 6 Total objects: 19


Discuss this level

Log in to comment on this level.
  • incrazyboyy: @Cralant: okay, good to know
  • Cralant: @incrazyboyy: Well thats one of the features of the updated version, as well as it should only check for objects that have moved since they were last checked. But ultimately it needs a better way of checking if a part intersects a box. In this version it makes several boxes each .25m in every direction so you have loads of little boxes all over each shape. It then checks for each of these boxes and what square it is in. This worked for smaller objects but then you had objects like the large pixel it would make like 300 different boxes which would just be unnecessary. The version I am currently working on takes the lines surrounding each object and then finds out which boxes each square intersects and make the a wall piece. In testing it works perfectly and the lag is pretty much non-existant, but there are some things that need working out such as only positive vectors are working correctly. Ones that go negative are completely messed up. I probably could have something uploaded by now but I am busy in real life as well as busy in the game I have a ton of projects that I have started and not finished. I will figure it out eventually and get it uploaded though.
  • incrazyboyy: @Cralant: make an adjustable delay, maybe a slider which controls the amount of refreshes per second
  • Cralant: @incrazyboyy: It runs hundreds of thousands of commands each game tick. That's why this project is kind of on hold. I'm trying to figure a way to reduce the lag.
  • incrazyboyy: Why is it so laggy?
  • Demon666: you could also make a hybrid using the multiple query boxes idea for small objects where less accuracy is needed but have it switch over to this more accurate method if a larger object is on the field.
  • Demon666: also you might be able to cut down lag if you run your loops at fixed intervals. that might help the gpu/cpu to keep up.
  • Cralant: @Demon666: its OK for most applications but for this I wanted something really accurate.
  • Demon666: now that i tested a linear servo on my super damage field i realize what you were talking about lol. query does have a very weird way of registering objects.
  • Cralant: @Demon666: that's exactly what the first two versions of my auto drone wip uses. The issue with that is if the entity is angled the query will return a square from one corner to the other. Meaning for large objects like a full size linear motor, rotated at 45 degrees will return a large box as a collision. The purpose of this was to make an accurate as possible version.
  • Leis: Nice @@
  • Demon666: I've found that most devices can easily handle 100's of query boxes running at once even if they are constantly changing location.
  • Demon666: why not just make every square a query box and have each of them report to a table if there is an object overlapping them?
  • Cralant: @Demon666: Many brain wrecking hours were spent trying to get this to work correctly. The lag is due to the way the collision detection works. The script finds points with distances of 0.25 from the center of each object. Then this is used to detect if a point is in a square on a grid. I could lower the distance to improve speed but then smaller objects would not register correctly. When I wake up I shall try attempt to reduce the amount of points needed and see how that works. I will also try z's suggestion of only recalculating objects that at moving.
  • Demon666: I was trying something similar to this with multiple queries. yours is far beyond mine tho lol
  • ricardito08: Lag!!!!
  • Demon666: I'm sorry to say that this is one of the few levels to even remotely lag my galaxy note 2 lol. the lag is barely noticeable though.
  • Cralant: @zardOz: yeah this issue is that the script cycles through every object in the query and then has for loops with for loops within for loops. I think I can make the script run at least 4x faster with a few changes. Although maybe the best option if for the less accurate, multi queries. Although I'm not sure how that works out if the area is larger then 20 by 20. This version runs the same no matter the size.
  • Cralant: Just realized this lags quite a bit, even on my nexus 5. There was no lag when I was designing this on my PC, oh well back to the drawing board I guess.
  • zardOz2: Epic work Cralent! It's beyond my phone's capabilities though even without the draw portion.... Best suggestion I have at the moment is to calculate positions on first run and then only recalculate objects which have a +(math.abs(velocity or angular velocity)

LEVEL ID: 5438