# Help understanding physics code.

• Akira__tyler
In summary, understanding complex physics code requires a strong foundation in mathematical concepts and principles, as well as a thorough understanding of the specific theories and laws being used. There are various resources available such as textbooks, online tutorials, and collaboration with other scientists and mentors. To improve coding skills for physics, it is important to practice, seek feedback, and stay updated on advancements in the field. Common mistakes to avoid include not understanding the underlying math and concepts, not properly testing and debugging, and not following good coding practices. To make physics code more efficient, careful analysis and optimization of algorithms and data structures, as well as avoiding unnecessary calculations and using parallel computing techniques, can be helpful. Continuous review and refinement of the code can also improve efficiency over time
Akira__tyler
Hello. I am writing a 3D physics engine for use in a game engine I'm creating with some friends. This is the first time I've written a 3D physics engine and I am having some difficulty understanding some of the math behind it all.

The section I am currently struggling with is collision resolution. At this point two bodies have collided and their velocities and penetration are being resolved. I am trying to do this with impulses, as guided by a book (Game Physics Engine Development). There are a few sections I do not understand fully.

In one section I try to find the amount of rotational velocity change for every unit of impulse, implemented something like this:

Code:
Vec3 impulsiveTorque = CrossProduct(relativeContact, contactNormal);
Vec3 angularVelocity = inverseInertiaTensor * impulsiveTorque;
Vec3 rotChangePerUnitImp = CrossProduct(angularVelocity, relativeContact);

The relativeContact is a vector from the CoM to the point of collision.
The contactNormal is the normal towards the other object from relativeContact.
The tensor is the inverse inertia tensor of the object in world space.

What I THINK the code is doing is finding how much of the relativeContact vector is not in the direction of the contact normal, changing that to angular velocity, and then ending up with the right result. I am missing a lot here and I'm having a lot of difficulty understanding why this does what it does.

Although it would be easiest for me to understand if someone just walked through this code in their own words, any help would be greatly appreciated. Thank you.

Hello, thank you for reaching out for help with your physics engine. I understand that collision resolution can be a complex topic, especially when it comes to implementing it in a 3D environment. Let me try to break down the code you provided and explain it in simpler terms.

The first line of code, Vec3 impulsiveTorque = CrossProduct(relativeContact, contactNormal), is calculating the torque or rotational force that is applied to the object at the point of contact. The CrossProduct function is a mathematical operation that results in a vector that is perpendicular to both the relativeContact and contactNormal vectors. This torque is necessary for the object to rotate in response to the collision.

Next, the code is calculating the angular velocity of the object by multiplying the inverse inertia tensor with the impulsiveTorque vector. The inverse inertia tensor is a mathematical representation of an object's resistance to rotation, and by multiplying it with the torque, we can determine how much the object will rotate in response to the collision.

Finally, the last line of code, Vec3 rotChangePerUnitImp = CrossProduct(angularVelocity, relativeContact), is calculating the amount of rotational change that will occur for every unit of impulse applied. The CrossProduct function is used again to determine the direction of the rotational change, and by multiplying it with the angular velocity, we can get the final result.

In summary, this code is calculating the necessary rotational changes that need to occur in response to a collision. I hope this explanation helps you understand the code better. If you have any further questions, please don't hesitate to ask. Good luck with your physics engine!

## 1. How can I understand complex physics code?

Understanding complex physics code requires a strong foundation in mathematical concepts and principles. It is also important to have a thorough understanding of the specific physics theories and laws being used in the code. Practice and patience are key in developing the skills needed to comprehend and work with physics code.

## 2. What resources can I use to help me understand physics code?

There are many resources available to help with understanding physics code, including textbooks, online tutorials, and forums where you can ask questions and seek guidance from experienced programmers. Additionally, collaborating with other scientists and seeking guidance from mentors can also be helpful in gaining a better understanding of physics code.

## 3. How can I improve my coding skills for physics?

Improving coding skills for physics requires practice and continuous learning. It is important to regularly work on coding projects, seek feedback from others, and stay updated on advancements and new techniques in the field. Additionally, taking courses or attending workshops focused on coding for physics can also be beneficial.

## 4. What are some common mistakes to avoid when working with physics code?

One common mistake when working with physics code is not thoroughly understanding the underlying math and concepts. Another mistake is not properly testing and debugging the code, which can lead to errors and incorrect results. It is also important to properly document the code and follow good coding practices for readability and maintainability.

## 5. How can I make my physics code more efficient?

To make physics code more efficient, it is important to carefully analyze and optimize the algorithms and data structures used in the code. Additionally, avoiding unnecessary calculations and using parallel computing techniques can also improve efficiency. Continuously reviewing and refining the code can also help to make it more efficient over time.

• Other Physics Topics
Replies
4
Views
2K
• Mechanics
Replies
14
Views
2K
• Classical Physics
Replies
9
Views
419
• Other Physics Topics
Replies
2
Views
1K
• Mechanics
Replies
9
Views
1K
Replies
11
Views
2K
• Mechanics
Replies
17
Views
3K
• Set Theory, Logic, Probability, Statistics
Replies
5
Views
1K
• Introductory Physics Homework Help
Replies
9
Views
2K
• Programming and Computer Science
Replies
13
Views
2K