This is my new code, 360 degrees can be used.(He's a circle.)
Collision box:
Now, I have only two objects.
If the object is more.I am a learner.
So, I don't know how to solve it.
If you have time, can you help me?:)
]]>You want functions and objects, and arrays of objects!
So we have an array of similar objects. We have a function for checking whether ANY specified two objects are colliding (using a supplied collision criteria--like distance from the center point which is the easiest). And we have a function that checks ALL objects against ALL OTHER objects using that same collide function.
That's basically it.
]]>@Chris Katko
Yes, it's really good!thanks!;D
I don't think he is efficient, though.
But thank you for helping me.:D
That's the most basic, simplified version I can make to illustrate what's going on.
There are plenty of intermediate and advanced tricks for speeding that up.
- For example, you can keep a SORTED array, so that all alive ones come first and dead ones come second. Then, the second you encounter a "dead" one, you just stop parsing the array. But then I'd have to explain how and why sorting works, and how to add additional entries, and edge cases like pointers to objects get invalidated if you move them around in the array.
- You can also use a linked list which supports removing elements in the middle. However a linked list is actually slower than a vector.
- If you have C++, simply use a std::vector. It's an array that automatically resizes itself if it needs to grow.
https://stackoverflow.com/questions/4442477/remove-ith-item-from-a-c-stdvector
Are you using C or C++?
Those are also data structure optimizations. You can use quadtrees to keep far away objects from ever being checked against each other.
https://www.wikiwand.com/en/Quadtree
But that's a more advanced structure and muddles the core aspects:
- You have a array/list/etc of objects
- You have an algorithm/criterea for what counts as a collision (within radius, within bounding box, etc)
- You check each object against every other object
You can always google "basic collision detection tutorial" for tons of articles out there.
]]>