{"id":36,"date":"2023-03-10T01:47:20","date_gmt":"2023-03-10T01:47:20","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/brandicook\/?p=36"},"modified":"2023-03-10T01:47:20","modified_gmt":"2023-03-10T01:47:20","slug":"blog-post-3-2","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/brandicook\/2023\/03\/10\/blog-post-3-2\/","title":{"rendered":"Blog Post #3"},"content":{"rendered":"\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"blog 3 demo\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/JYiIINFLHyI?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><figcaption class=\"wp-element-caption\">please ignore the parts where I say\/show the beta demonstration!! I accidentally thought I was recording that but this is where I&#8217;m at for Blog #3 LOL!<\/figcaption><\/figure>\n\n\n\n<p>Hello!<\/p>\n\n\n\n<p>For this blog update, I mostly want to talk about spawning in objects. So previously, my natural forest objects would randomly spawn on my scene within set bounds&#8230; however, they would randomly spawn wherever at will, which means that they could potentially (and DID) spawn on TOP OF EACH OTHER! <\/p>\n\n\n\n<p>Well&#8230; For a simulated environment&#8230; that&#8217;s much less than ideal. If there&#8217;s a tree in a tree that sounds pretty neat, but isn&#8217;t too realistic (especially since there&#8217;s no merging). So to fix this, I had to add some sort of system that checked if an object already spawned in a spot before spawning another. This sounds super simple&#8230; but for some reason I had the hardest time implementing this (mostly due to the whole &#8220;world.spawn&#8221; thing confusing me, and I had to be careful with mutable\/immutable variables and when I could use them). <\/p>\n\n\n\n<p>So&#8230;. In order to prevent overlap this, I decided to add a mini circle &#8220;collider&#8221; to each object, which I could then use to check for overlap. Well, since each object is a different size, and a mushroom wouldn&#8217;t need as big a circle as say, a tree, I added different radius sizes to each NaturalObject. This is within my forest.rs:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npub const ASSETS: &#x5B;NaturalObject; 13] = &#x5B;\n    NaturalObject {\n        name: &quot;coniferousa&quot;,\n        scale: 1.0,\n        radius: 15.0,\n    },\n    NaturalObject {\n        name: &quot;mushroom&quot;,\n        scale: 1.5, \n        radius: 5.0,\n    },\n<\/pre><\/div>\n\n\n<p>Here&#8217;s an example of some of my objects. I can give a mushroom, a small object, a small radius of 5, while a big tree could have a much larger radius. Then, when creating an object within the scene (nature.rs), I can add this radius to an actual circle: <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#&#x5B;derive(Default)]\npub struct Circle {\n    center: Vec2,\n    radius: f32,\n}\n<\/pre><\/div>\n\n\n<p>Then, in order to check for overlap, I can use only spawn an object if overlap != true. So when spawning, I check only spawn if the following function is true: <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n fn check_overlap(\n        new_position: Vec3,\n        new_radius: f32,\n        existing_positions: &amp;amp;&#x5B;Vec3],\n        existing_radii: &amp;amp;&#x5B;f32],\n    ) -&amp;gt; bool {\n        for (i, position) in existing_positions.iter().enumerate() {\n            let distance = position.distance(new_position);\n            if distance &amp;lt; new_radius + existing_radii&#x5B;i] {\n                return true;\n            }\n        }\n        false\n    }\n<\/pre><\/div>\n\n\n<p>Please excuse the formatting. But basically, I just check if the potential spawn point is overlapping with an already created circle. This is created and called within my create_object function: <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n fn create_object(\n        scene_handle: Handle&amp;lt;Scene&amp;gt;,\n        object: &amp;amp;NaturalObject,\n        existing_positions: &amp;amp;&#x5B;Vec3],\n        existing_radii: &amp;amp;&#x5B;f32],\n    ) -&amp;gt; Option&amp;lt;SceneBundle&amp;gt; {\n        let mut circle = AddNature::create_circle(object.radius);\n        let mut attempts = 0;\n        loop {\n            \/\/ randomize position until non-overlapping position is found\n            let position = Vec3::new(\n                fastrand::f32() * 450.0 - 225.0,\n                fastrand::f32() * 450.0 - 225.0,\n                0.0,\n            );\n            circle.center = position.truncate();\n            if !AddNature::check_overlap(position, object.radius, existing_positions, existing_radii) {\n                return Some(SceneBundle {\n                    scene: scene_handle,\n                    transform: Transform::from_translation(position)\n                        .with_scale(Vec3::splat(object.scale))\n                        .with_rotation(Quat::from_rotation_x(std::f32::consts::FRAC_PI_2)),\n                    ..default()\n                });\n            }\n            attempts += 1;\n            if attempts &amp;gt; 10 {\n                return None;\n            }\n        }\n    }\n<\/pre><\/div>\n\n\n<p>This basically says to only create an object if its circle does not overlap with an already existing circle. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"646\" src=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6226\/files\/2023\/03\/image-1024x646.png\" alt=\"\" class=\"wp-image-37\" srcset=\"https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6226\/files\/2023\/03\/image-1024x646.png 1024w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6226\/files\/2023\/03\/image-300x189.png 300w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6226\/files\/2023\/03\/image-768x485.png 768w, https:\/\/osu-wams-blogs-uploads.s3.amazonaws.com\/blogs.dir\/6226\/files\/2023\/03\/image.png 1158w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Well, look at that, an almost believable forest scene!<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Hello! For this blog update, I mostly want to talk about spawning in objects. So previously, my natural forest objects would randomly spawn on my scene within set bounds&#8230; however, they would randomly spawn wherever at will, which means that they could potentially (and DID) spawn on TOP OF EACH OTHER! Well&#8230; For a simulated [&hellip;]<\/p>\n","protected":false},"author":12984,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-36","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/brandicook\/wp-json\/wp\/v2\/posts\/36","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/brandicook\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/brandicook\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/brandicook\/wp-json\/wp\/v2\/users\/12984"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/brandicook\/wp-json\/wp\/v2\/comments?post=36"}],"version-history":[{"count":1,"href":"https:\/\/blogs.oregonstate.edu\/brandicook\/wp-json\/wp\/v2\/posts\/36\/revisions"}],"predecessor-version":[{"id":38,"href":"https:\/\/blogs.oregonstate.edu\/brandicook\/wp-json\/wp\/v2\/posts\/36\/revisions\/38"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/brandicook\/wp-json\/wp\/v2\/media?parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/brandicook\/wp-json\/wp\/v2\/categories?post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/brandicook\/wp-json\/wp\/v2\/tags?post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}