{"id":5547,"date":"2025-04-11T14:49:27","date_gmt":"2025-04-11T14:49:27","guid":{"rendered":"https:\/\/castle-engine.io\/wp\/?p=5547"},"modified":"2025-04-11T14:49:27","modified_gmt":"2025-04-11T14:49:27","slug":"dice-throwing-demo-recording-physics-simulation","status":"publish","type":"post","link":"https:\/\/castle-engine.io\/wp\/2025\/04\/11\/dice-throwing-demo-recording-physics-simulation\/","title":{"rendered":"Dice Throwing Demo, Recording Physics Simulation"},"content":{"rendered":"<table class=\"thumbnails thumbnails-align-right\"><tr><td>\n          <a href=\"https:\/\/castle-engine.io\/wp\/wp-content\/uploads\/2025\/04\/dice.png\"\n             class=\"screenshot\"\n             title=\"Dice Throwing Demo\"><img loading=\"lazy\" decoding=\"async\"\n            style=\"float: right\"\n            src=\"https:\/\/castle-engine.io\/wp\/wp-content\/uploads\/2025\/04\/dice-200x119.png\"\n             width=\"200\" height=\"119\" \n            alt=\"Dice Throwing Demo\"\n          ><\/a><\/td><\/tr><\/table>\n<p>We&#8217;ve a made a new demo project using <a href=\"https:\/\/castle-engine.io\/\">Castle Game Engine<\/a>:<\/p>\n<p>Throw a 6-sided dice, using physics, and cheat to make the outcome predictable. Before throwing the dice, you select the desired outcome, and we guarantee the dice will land the desired face &#8220;up&#8221;.<\/p>\n<p>Full source code and data are available at <a href=\"https:\/\/github.com\/castle-engine\/castle-dice-throwing\">https:\/\/github.com\/castle-engine\/castle-dice-throwing<\/a>.<\/p>\n<ul>\n<li>You can <a href=\"https:\/\/castle-engine.io\/web-demos\/castle-dice-throwing\/\">run the demo in a web browser<\/a>, done thanks to our <a href=\"https:\/\/castle-engine.io\/web\">web target<\/a> support.<\/p>\n<\/li>\n<li>\n<p>You can also <a href=\"https:\/\/github.com\/castle-engine\/castle-dice-throwing\/releases\/latest\">download a precompiled version for Windows and Linux<\/a>.<\/p>\n<\/li>\n<\/ul>\n<p>The dice trajectory is truly random, following physics and some initial (random) parameters. So, how is it possible we can guarantee the outcome?<\/p>\n<ul>\n<li>First we run the physics simulation without rendering it. It&#8217;s a very fast process, takes about 1\/50 of the second (so you don&#8217;t even notice that we do this whole simulation when you press the button <em>&#8220;Throw Dice&#8221;<\/em>).<\/p>\n<\/li>\n<li>\n<p>Then we apply a local rotation to the dice, to change the <em>simulated<\/em> outcome (face that is up after simulation) into the <em>desired<\/em> outcome. Since a dice has the same shape (at least for our physics approximation, and for our human feelings; in reality of course the shape is a bit different from each side), no matter how you rotate it by 90 or 180 degrees around one the primary axis, the rotated dice will look equally good during the same trajectory.<\/p>\n<\/li>\n<li>\n<p>Only then we actually render the simulation. We simply play the previously-recorded physics simulation.<\/p>\n<\/li>\n<\/ul>\n<p>You can look at the source code, naturally, to see how this is done. The <a href=\"https:\/\/github.com\/castle-engine\/castle-dice-throwing\/blob\/b71a9aa5720e439ae043002feb3e9252bc0dc216\/code\/gameviewmain.pas#L286\">ClickThrow method<\/a> with the nested procedures <code>StartSimulation<\/code> and <code>RecordSimulation<\/code> does the core job. If you build the example yourself, in <em>debug<\/em> mode (toggle it using the editor <em>&#8220;Run -> Debug Mode&#8221;<\/em> menu item, or command-line <code>--mode=debug<\/code> for the <a href=\"https:\/\/castle-engine.io\/build_tool\">build tool<\/a>), then additional &#8220;debug UI&#8221; will be visible, allowing you to tweak the physics parameters and also to perform the simulation without prerecording (so the outcome is unknown).<\/p>\n<p>We hope that you enjoy this demo \ud83d\ude42 Recording the physics simulation (to later replay it) may have a number of use-cases, we hope that our demo makes it clear how to do it.<\/p>\n<p>If you like this, please consider <a href=\"https:\/\/castle-engine.io\/donate\">donating<\/a>, for example by <a href=\"https:\/\/www.patreon.com\/castleengine\">supporting us on Patreon<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;ve a made a new demo project using Castle Game Engine: Throw a 6-sided dice, using physics, and cheat to make the outcome predictable. Before throwing the dice, you select the desired outcome, and we guarantee the dice will land the desired face &#8220;up&#8221;. Full source code and data are available at https:\/\/github.com\/castle-engine\/castle-dice-throwing. You can &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/castle-engine.io\/wp\/2025\/04\/11\/dice-throwing-demo-recording-physics-simulation\/\" class=\"more-link\">Continue reading  \u27a4<span class=\"screen-reader-text\"> &#8220;Dice Throwing Demo, Recording Physics Simulation&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":5548,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"publish_to_discourse":"1","publish_post_category":"9","wpdc_auto_publish_overridden":"","wpdc_topic_tags":"","wpdc_pin_topic":"","wpdc_pin_until":"","discourse_post_id":"6535","discourse_permalink":"https:\/\/forum.castle-engine.io\/t\/dice-throwing-demo-recording-physics-simulation\/1861","wpdc_publishing_response":"","wpdc_publishing_error":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-5547","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/castle-engine.io\/wp\/wp-content\/uploads\/2025\/04\/dice.png","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9IgYW-1rt","jetpack_likes_enabled":false,"_links":{"self":[{"href":"https:\/\/castle-engine.io\/wp\/wp-json\/wp\/v2\/posts\/5547","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/castle-engine.io\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/castle-engine.io\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/castle-engine.io\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/castle-engine.io\/wp\/wp-json\/wp\/v2\/comments?post=5547"}],"version-history":[{"count":8,"href":"https:\/\/castle-engine.io\/wp\/wp-json\/wp\/v2\/posts\/5547\/revisions"}],"predecessor-version":[{"id":5556,"href":"https:\/\/castle-engine.io\/wp\/wp-json\/wp\/v2\/posts\/5547\/revisions\/5556"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/castle-engine.io\/wp\/wp-json\/wp\/v2\/media\/5548"}],"wp:attachment":[{"href":"https:\/\/castle-engine.io\/wp\/wp-json\/wp\/v2\/media?parent=5547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/castle-engine.io\/wp\/wp-json\/wp\/v2\/categories?post=5547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/castle-engine.io\/wp\/wp-json\/wp\/v2\/tags?post=5547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}