One Cocos Developer Improves Their Hit Detection Algorithm With ChatGPT
2023.04.25 by COCOS
Tutorials

Which casual rogue-like game was the hottest in 2022? If you want to ask this question, it must be "Vampire Survivor"!

 

Year One of the Survivor Games

 

Maybe some people have never played Vampire Survivor, but they must have seen similar gameplay on YouTube videos or advertisements. Simple controls, various skill combinations, and blind-box rogue gameplay with dense monsters often let players show off to their friends, and they can't stop playing.

 

This magical never-ending gameplay has become popular in PC games on Steam and swept the 2D action game field around the world like a storm.

 

 

In 2022, "Survivor.io," released by Habby, quickly achieved a monthly turnover of over 100 million, dominating the rankings in various regions of the world for a long time.

The gameplay of this game is explained with "Although there are thousands of enemies, I will fight! ".

 

 

 

 

Also popular is another anime-style game, "Nine Trials,” made me experience what it means to be "I am invincible, I am immortal! I am above immortality!"

 

 

There are as many as hundreds of these games on mini-game platforms, Android and iOS platforms, but most similar games face performance problems. Whether thousands of enemies require a practical and custom physics engine or many particle effects, require elegant batch processing, a lot of development time is needed.

 

Using AI to help reduce R&D costs

 

To address the above pain points, I found a physics engine developer, 98K, who uses ChatGPT's AI chat conversations to customize a physics engine that can support thousands of enemies in HTML5 and mini-game platforms!

 

This article will explain how to use ChatGPT to create a commercial-grade physics engine (98K Enemy Cutting Physics)

 

GIF

 

Not only does 98k use ChatGPT for physics, but it introduced them to how to optimize the particle effects system (P2D physics system) to optimize the underlying Assembler and Renderer. 98k easily achieves a full-screen batch of thousands of particle animation effects and improves performance! This lets the mini-game platforms reach thousands of special effects on the same screen!

 

GIF

 

Of course, this program will be free and open source.

 

High-performance collision detection with ChatGPT

 

This time, with the support of ChatGPT AI, we bring you a high-performance 2D/3D, lightweight dynamic collision detection system for the majority of mult-enemy, pop-up attacks, and group gathering ..., all provided with a lightweight, high-performance, out-of-the-box system, for the majority of developers to do more quality games with powerful performance.

 

The collision system is divided into dynamic and static. These two different situations will use different strategies.

 

1. Dynamic collision detection, mainly for moving objects, such as characters, bullets, skills ... based on intelligent split-axis sweeping optimization scheme, using the (optimized version of SAP + OBB, etc. ...) for a variety of 2D / 3D, moving object detection, such as characters, pop-ups, bullets, attacks ... (The performance is 2 to 3 times the performance of the general "quadtree" method)

 

You can experience it at: http://learncocos.com/rougelike/

 

2. Static collision detection, mainly for static objects, such as scenes , objects, and obstacles ... based on a static spatial segmentation optimization scheme, using (octree + kdtree + gjk + epa) technology stack. Suitable for 3D world walking, 3D surface walking and 3D boundary detection (fps , mmo, rpg)

 

You can experience it at https://store.cocos.com/app/detail/4035

 

Dynamic collision detection system components

 

 

1. AABB bounding box update
2. Rough collision algorithm screening
3. Final shape precise collision

 

Next, around these three important ideas, I will ask ChatGPT, which has become popular recently, to see if I can make friends and let the GPT teacher give me some pointers!

 

Let’s popularize science first. Why did you choose SAP this time as the core screening algorithm for dynamic collision detection? Because all major physics engines can see it. The full name of SAP (sweep and prune) is an axial sorting and sweeping algorithm (with a taste of SAT pruning).

 

X-axis projection can quickly separate and exclude intersections

 

ChatGPT summarizes these advantages.

 

1. Fast object update (objects update their position in real-time)

Because you want to sort the sub-axis, you may think of using fast sort nlogn, but SAP has the honest answer. Since the sorting is relatively stable when the moving object is adjacent to the frame, it is actually faster to use insertion sort, which is very useful. The performance of o(n) may be much faster than nlogn as a whole. For other tree shapes such as quad, octo, and BVH, updating the object needs to adjust the tree structure, which consumes a lot of costs.

 

2. Fast mutual detection between objects (often referred to as trigger) due to the use of sequential access structure, read and write performance than other shapes tree structure can be high, while easily mutual trigger detection and this sequential structure is born with de-duplication detection function (for example quadtree, octree, lattice, this type of multiple partitioning, but also de-duplication mark detection, invisible increase in performance consumption)

 

3. support 2D/3D without scope restrictions (compatibility) because of the axial sorting segmentation detection, so you don’t have store space size restrictions, but also not subject to dimensional restrictions, excellent support for the full range of 2D/3D space detection. (Like quadranomials, octonomials, and lattices, which the spatial range size may limit)

 

In the spirit of life's first credo, "Let whoever asks the question solve the problem!" I decided to let AI solve the problems it threw up.

 

 

Seeing this, I started typing the code!

 

It is easy to see that the teacher's primary content is solid, but only one or two sets of moves still need to be included! Let's move step by step and improve the techniques of GPT teacher like a sensei to his student!

 

 

After another round of study with the GPT teacher, the collection is almost identical.

Carry out its precise transformation on the framework written by AI. At the same time, it combines its optimization method to carry out an in-depth modification of the SAP algorithm to provide the most significant effect for subsequent collision detection.

 

The rough introduction is complete, with more than half of the success. There are still two links (AABB, Shape). How to finish this? In line with my third creed, if I can do it, I will never do it again. I wake up the AI again and have it write code.

 

AABB bounding box update? (AABB is mainly provided for rough screening)

 

 

After several rounds of communication, the GPT teacher gave me a more streamlined plan under my relentless pressure.

 

 

ChatGPT is like a tireless programming machine that kept helping me! All I had to do was give more requirements, and he would work non-stop overtime! Tirelessly outputting higher quality code solutions!

 

 

Shape accurate collision

 

Box and Sphere detection is temporarily supported (other commons will be supported later), and collision of 3D rotation operation is supported. So we want to achieve OBB vs. OBB and OBB vs. Sphere detection. So I woke up the AI.

 

1.Sphere vs Sphere collision detection. Done in the background

 

2.OBB vs Sphere (rotating square and sphere precise collision)

 

 

3.OBB vs OBB (precise collision between rotating cubes)

 

 

With the assistance of GPT teacher, this solution was solved pretty well!

 

Finally, with its effort, the collision system is finally running. AI is able to write code, but it is not necessarily perfect and requires practitioners to have a certain knowledge base to better its effectiveness.

 

In time, with the addition of everyone's training and global knowledge, it will eventually become that giant! Stepping on our shoulders! Helping mankind to explore the unknown sea of stars!

 

Dynamic collision in action!

 

Xiaomi 10 browser: 2000 ,2D AABB collision test

 

 

Xiaomi 10 browser: 2000 ,3D AABB crash test

 

 

Xiaomi 10 Browser: Simple to use survivor demo test

 

 

Test device: Xiaomi 10 with Snapdragon 865

Browser: Self-browser

 

Test data:

 

2000 motion 2D AABB : average 5ms or so

2000 motion 3D AABB: average 8ms or so

 

This performance is the general "quadtree" way 2 to 3 times the performance, more test experience, you can run the following experience connection.

 

Experience it here: 

 

http://learncocos.com/rougelike/

 

This is not my limit, not to mention ChatGPT's limit.