Patryk27 / shorelark

Simulation of life & evolution
https://pwy.io/en/posts/learning-to-fly-pt1/
MIT License
207 stars 21 forks source link

Angles problems? #11

Closed yovanoc closed 1 year ago

yovanoc commented 1 year ago

I convert the web pack app part into a vite/react-ts project then try to play a bit with the code and found an interesting thing.

image

PS: I changed to fov angle to PI/2 but that's not the point.

I try to render the fov because your 'arcs' seems a bit off and here's what I saw.

It comes from this part

image

I don't know why you added Math.PI / 2.0 here ? probably because otherwise we see the fov on the side and not in front of them, but IT REALLY is on the side, you can look with your drawn arcs and this is why birds are constantly turning.

yovanoc commented 1 year ago

And I would like to add that if I follow your rules for the -sin +cos thing we have a nice render but we can clearly see that the food perception is not based on these calculations:

image

my repo: https://github.com/yovanoc/mysim

Patryk27 commented 1 year ago

Oh, that's a nice find - I'll take a look later and try to remind what's going on; I swear angles are a hill I'm going to die on 😅

yovanoc commented 1 year ago

Yeah adding this fov representation helped me a lot to catch this. I first saw that your arc shapes are not faced to food then I dig with these things. You can try on my repo, I added a mouse hover on birds to debug too. But yeah if we remove these Math.PI / 2 the representation of the vision appears to be good but then I asked myself if these birds are not flying from the side like crabs 😂 , I'm with you with these angles!

yovanoc commented 1 year ago

I look back your tuto again.

image

pi / 2 is not faced to the right if I understand well so this the eye implementation and all tests that are not correct ?

Patryk27 commented 1 year ago

Note that fov_angle corresponds to how wide an eye sees - and so fov_angle of 2PI = the bird sees everything around it, while fov_angle of PI / 2 = the bird sees 90° "cone" in front of it; it's not related to the direction / rotation 👀

yovanoc commented 1 year ago

Yes sorry I didn't express myself correctly. The thing I wanna said is that for this test rot: 0 and for a fov angle of PI/2, this is not this result because you said rot 0 is faced to the upward direction no ?

    /// ------------
    /// |        /.|
    /// |      /...|
    /// |    @>...%|
    /// |      \...|
    /// |        \.|
    /// ------------
yovanoc commented 1 year ago

Oh no mb you said it was counter clockwise ! I really don't know then.. but there is an issue for sure between the eye.rs file and the things we tried to render

Patryk27 commented 1 year ago

Okie, so eyes themselves seem to be correct - it's rest of the code that uses funky angles, and applying this:

diff --git a/libs/simulation/src/animal.rs b/libs/simulation/src/animal.rs
index a7e65dd..436949e 100644
--- a/libs/simulation/src/animal.rs
+++ b/libs/simulation/src/animal.rs
@@ -56,7 +56,7 @@ impl Animal {
     }

     pub(crate) fn process_movement(&mut self) {
-        self.position += self.rotation * na::Vector2::new(0.0, self.speed);
+        self.position += self.rotation * na::Vector2::new(self.speed, 0.0);
         self.position.x = na::wrap(self.position.x, 0.0, 1.0);
         self.position.y = na::wrap(self.position.y, 0.0, 1.0);
     }
diff --git a/www/index.js b/www/index.js
index 9b8c02b..354fca6 100644
--- a/www/index.js
+++ b/www/index.js
@@ -261,7 +261,7 @@ function redraw() {
             animal.x,
             animal.y,
             config.food_size,
-            animal.rotation,
+            animal.rotation - Math.PI / 2.0,
             'rgb(255, 255, 255)',
         );

@@ -271,8 +271,7 @@ function redraw() {
             const angleFrom =
                   animal.rotation
                   - config.eye_fov_angle / 2.0
-                  + cellId * anglePerCell
-                  + Math.PI / 2.0;
+                  + cellId * anglePerCell;

             const angleTo = angleFrom + anglePerCell;
             const energy = animal.vision[cellId];

... fixes all of the issues.

Still, I have to investigate what I don't understand about my calculations, then 😄

Patryk27 commented 1 year ago

Okie, got the offender - it seems that eye.rs should have:

let angle = na::Rotation2::rotation_between(&na::Vector2::y(), &vec).angle();

... instead of na::Vector2::x(); this is the only fix needed, rest of the calculations are correct then (it seems!).

It's probably a remnant of me fixing https://github.com/Patryk27/website/issues/11 😅

yovanoc commented 1 year ago

image

OH YES perfect ! But all eye.rs tests are failing now x)

Patryk27 commented 1 year ago

Yeah, I'll try to prepare the updated version today :smile:

yovanoc commented 1 year ago

oh nice man ! but look

image

image

if -90 degrees is on the right, then yeah rot 0 is to the top ?

Patryk27 commented 1 year ago

Oh, that's interesting - I guess there's no food in front of that birdie that it could see, right? (i.e. it accidentally sees the food on its left here)

yovanoc commented 1 year ago

No sorry it's normal for the food but I speak about the -90 degrees rotation here, is pointing to the right good ? so 0 is top not right

Patryk27 commented 1 year ago

I see - yes, rotation seems alright here: rotation of zero = fly downward, and then it goes clockwise, so:

yovanoc commented 1 year ago

Ok perfect !