• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

HALP! Simultaneous Equations with Chemistry and Physics Simulation

2 posts in this topic

Posted (edited)

As part of one of my projects, there is a /chemistry/ simulator. It's not intended to be exact to real-life chemistry, but is still a fair bit more complex than just "A + B = C" This is being done in an Object-Oriented programming language similar to java and c#.

Some background: 
Before I started working on the project:


The container was an instantiated object. It contained a variable, with an array of instantiated chemicals that *existed in the container*.
The chemicals themselves were instantiated objects, with a reference to their container, and a volume.

Whenever the container was interacted with, (such as a new chemical added), the function would instantiate a new chemical, add linking references, and update the volume of the chemical. Then it would iterate through *EVERY* chemical in the container to check for possible reactions using a lookup table, and if a match was found, it would run the reaction, and restart. This included a potential for infinitely 'reacting' chemicals, but the workaround was just careful implementation of recipes to prevent infinite loops.


Now i saw this, and was absolutely disgusted. And changed it to something more akin to:


In this model, the container has a 2D array of the chemical, and it's volume inside of the container.
The chemical is only ever instantiated ONCE, and if a chemical is accessed, the properties of the master chemical are read.

(Each unique chemical has static properties in both models, and the only dynamic variable was *volume*)

Then when the contents of the container are modified, it is *flagged* to receive an update, iterates through the contents, performs all currently possible reactions unless that reaction was already performed during this update. If any reactions occured, it would flag the container to be updated *next* process. This removed the potential for problems caused be recursive reactions, but prevented instantaneous consecutive reactions.


The problem!

NOW, i want to add some more realism and complexity to the system, as well as make it more efficient if possible. I want to iterate as infrequently as possible.

containers now store the following variables and properties regarding its chemical contents:

Quantity - The quantity of a chemical within. Equivalent to moles.
Thermal Energy - The total thermal energy of chemicals within. Equivalent to joules.
Volume - The volume of the container. This is the *SAME* as the total volume of the container's contents. But the volume of each chemical should also be tracked separately. ie. 50 unit container contains 30 units of AIR and 1 unit of OIL and 19 units of WATER. This should be calculated based on the quantity of each substance in the container, the temperature, and the total volume of the container. As well as some data based on the individual chemical's properties.
Temperature - This is calculated based on the thermal energy, and the chemical properties of the chemicals within. (IE. Temperature = Total thermal energy / Total specific heat capacity of chemicals.)
Pressure - This is calculated based on volume, temperature, quantity, and some chemical properties.


What is the most efficient way for me to *add* or *remove* a chemical from the container?

For example "I add 30 units of water. This displaces the gas in the container, and modifies the specific heat and thermal energy of the container. As a result, modifying the temperature, volume, and pressure of the container contents." 

How can i adjust all relevant container values using *just* the values before the water is added, and the properties of the water? What additional information might i need to store? 

The volume of the contents of the container is *ALWAYS* equal to the volume of the container. This means that if the container is sealed, and contains 30 units of water @ standard temperature, but the volume of the container is *larger* than the volume of liquid water. How do i find the equilibrium point for the volume of *liquid* water and water *vapour* using only temperature, quantity, total volume, and a single extra variable/coefficient for vapour pressure?

How do i quickly and efficiently calculate that, when there are say, n other chemicals inside the container with different properties, that also need to be in equilibrium?


Are there any faster and more efficient methods i can use to look-up all possible reaction matches, and perform the reactions, so that the reactions are in equilibrium?

IE if i have : "1 water + 1 agent A = volatile acid" and "1 water + 1 agent B = volatile explosive" in the recipe list, and i have:
"1 water, 1 agent A, and 1 agent B" in the container,

How can i make sure that i end up with 25% acid, 25% explosive, 25% A, 25% B, instead of 50% acid, 50% agent B, with as few iterations as possible?

In the case that "acid" and "explosive" react together to form 3 parts agent C and 1 part water, How can i make the reaction find equilibrium in only one update, with as few iterations as possible? 

Edited by nullie

Share this post

Link to post
Share on other sites

If the space is fine grained, you could try random action. Throw dice, 1-3 makes acid, 4-6 makes explosive. Just as quantum physics. This should give it some reality. If their masses are not integer but any ratio, then you would need to alter the probability with it so 1-2 acid 3-6 explosive is B is more. Something exponential should do the trick.


Enable finer grained sub 2D-map for the mixed cells. 


Share this post

Link to post
Share on other sites

This all seems a bit vaguely complex. What factors are you supposed to allow for here? e.g. Are you supposed to change reaction rates based on temperature and pressure? Are you supposed to say how much of each chemical is in what state (solid, liquid, gas...) and adapt rates appropriately? Is this supposed to be some kind of simulation of Le Chatelier's principle and take into account endothermic and exothermic reactions?

n the case that "acid" and "explosive" react together to form 3 parts agent C and 1 part water, How can i make the reaction find equilibrium in only one update, with as few iterations as possible?

I don't think we have enough information. Are you supposed to be adapting the reaction rate over time, to react to changes in pressure, temperature, concentration etc... Or are all reaction rates supposed to be (unrealistically) constantly occuring? Reactions at a constant rate would be very easy and could use the simulated dice rolls suggested above. Reactions at a variable rate are much harder. If you wanted to calculate in one step, you could use some calculus to obtain your answer.


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0