# 2D Scene Collision

**URL:** <https://forum.castle-engine.io/t/2d-scene-collision/322>\
**Category:** Uncategorized\
**Created:** [April 17, 2021, 9:26pm UTC](https://forum.castle-engine.io/t/2d-scene-collision/322 "2021-04-17T21:26:06Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![eugeneloza](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/eugeneloza/32/12_2.png) [@eugeneloza](https://forum.castle-engine.io/u/eugeneloza)\
**Post date:** [May 2, 2021, 8:49am UTC](https://forum.castle-engine.io/t/2d-scene-collision/322/21 "2021-05-02T08:49:28Z")

</div>

> [@Carring](#):
>
> It gives no error indication though.

Then most likely it fires when encountering the first “update”.

Try commenting out `Mouse.RigidBody := TRigidBody.Create(Self)` (i.e. everything related to physics) to make sure that during the experiments you didn’t introduce a random error elsewhere (it happens often). And then uncomment the whole `MakeMousePhysics` code to see that error appears. Then try breakpoing through this procedure - if this is something inside `MakeMousePhysics` or some parameters given to `RigidBody` are invalid and fail when it tries to `Update`.

---

<div class="post-metadata">

**Author:** ![Carring](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/carring/32/1950_2.png) [@Carring](https://forum.castle-engine.io/u/Carring)\
**Post date:** [May 2, 2021, 9:16am UTC](https://forum.castle-engine.io/t/2d-scene-collision/322/22 "2021-05-02T09:16:04Z")

</div>

The error appears when I run the program up until Mouse.RigidBody := TRigidBody.Create(Self);

 ![image](https://forum.castle-engine.io/uploads/default/original/1X/fce6514270b76e116bd36d85b2bb186f5502d80e.png)

---

<div class="post-metadata">

**Author:** ![eugeneloza](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/eugeneloza/32/12_2.png) [@eugeneloza](https://forum.castle-engine.io/u/eugeneloza)\
**Post date:** [May 2, 2021, 3:00pm UTC](https://forum.castle-engine.io/t/2d-scene-collision/322/23 "2021-05-02T15:00:34Z")

</div>

I don’t think I can come up with anything more here remotely, please try to make a minimal example that can be downloaded and ran (including some placeholder data), so that I or someone else will be able to test it and debug it. You can send it in private message if you don’t want to publish it anywhere online.

---

<div class="post-metadata">

**Author:** ![Carring](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/carring/32/1950_2.png) [@Carring](https://forum.castle-engine.io/u/Carring)\
**Post date:** [May 3, 2021, 8:01am UTC](https://forum.castle-engine.io/t/2d-scene-collision/322/24 "2021-05-03T08:01:04Z")

</div>

That’s okay. I just made an archive of the minimal code and data. How can I send it to you private?

---

<div class="post-metadata">

**Author:** ![eugeneloza](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/eugeneloza/32/12_2.png) [@eugeneloza](https://forum.castle-engine.io/u/eugeneloza)\
**Post date:** [May 3, 2021, 8:10am UTC](https://forum.castle-engine.io/t/2d-scene-collision/322/25 "2021-05-03T08:10:15Z")

</div>

Try this way (click my name and select “Message”):

 ![2021-05-03 11_09_52-Greenshot](https://forum.castle-engine.io/uploads/default/original/1X/d76205708489c4055eb81bd43bd8e2f1c89e9ac4.png)

---

<div class="post-metadata">

**Author:** ![eugeneloza](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/eugeneloza/32/12_2.png) [@eugeneloza](https://forum.castle-engine.io/u/eugeneloza)\
**Post date:** [May 3, 2021, 9:36am UTC](https://forum.castle-engine.io/t/2d-scene-collision/322/27 "2021-05-03T09:36:34Z")

</div>

Ugh. Somehow it got sent to the topic instead of private messages. I’ve downloaded the project, so you can delete the link. I’ll look at it later.

---

<div class="post-metadata">

**Author:** ![eugeneloza](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/eugeneloza/32/12_2.png) [@eugeneloza](https://forum.castle-engine.io/u/eugeneloza)\
**Post date:** [May 3, 2021, 1:41pm UTC](https://forum.castle-engine.io/t/2d-scene-collision/322/28 "2021-05-03T13:41:14Z")

</div>

I’ve managed to fix the SIGSEGV:

You should not add it as `Mouse.RigidBody := TRigidBody.Create(Mouse)` because it tries immediately to calculate collider parameters - and it doesn’t yet exist. Thus you should:

```auto
RBody: TRigidBody
...
RBody := TRigidBody.Create(Mouse);
...
(after everything initialized)
Mouse.RigidBody := RBody;

```

That said, I still can’t make collisions work. Either they collide always, or never. Disregarding mouse position. I’ll try to see what can be done here.

---

<div class="post-metadata">

**Author:** ![Carring](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/carring/32/1950_2.png) [@Carring](https://forum.castle-engine.io/u/Carring)\
**Post date:** [May 3, 2021, 5:05pm UTC](https://forum.castle-engine.io/t/2d-scene-collision/322/29 "2021-05-03T17:05:40Z")

</div>

Yes, the SIGSEGV is gone but still no collisions. And a strange add-on: the (Player)Scene is now slowly moving down the screen by calling the "PlayerScenePhysics procedure. Is this a bug in CGE?

```auto
 var
  RBody: TRigidBody;
  Size: TVector3;
  Collider: TBoxCollider;
  begin
    RBody := TRigidBody.Create(Mouse);
    RBody.Dynamic:= true;
    RBody.Setup2D;
    RBody.OnCollisionEnter:= @MouseCollisionEnter;

    Collider := TBoxCollider.Create(RBody);

    Size := Mouse.LocalBoundingBox.Size;
    Size.Z := Max(0.1, Size.Z);
    Collider.Size := Size;
    Mouse.RigidBody := RBody;
end;

Procedure TStatePlay.PlayerScenePhysics;
 var
  RBody: TRigidBody;
  Size: TVector3;
  Collider: TBoxCollider;
begin
  RBody := TRigidBody.Create(PlayerScene);
  RBody.Dynamic:= true;
  RBody.Setup2D;

  Collider := TBoxCollider.Create(RBody);

  Size := PlayerScene.LocalBoundingBox.Size;
  Size.Z := Max(0.1, Size.Z);
  Collider.Size := Size;
  PlayerScene.RigidBody := RBody;
end;
```

---

<div class="post-metadata">

**Author:** ![eugeneloza](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/eugeneloza/32/12_2.png) [@eugeneloza](https://forum.castle-engine.io/u/eugeneloza)\
**Post date:** [May 3, 2021, 6:02pm UTC](https://forum.castle-engine.io/t/2d-scene-collision/322/30 "2021-05-03T18:02:23Z")

</div>

> [@Carring](#):
>
> is now slowly moving down the screen by calling the "PlayerScenePhysics procedure.

No, it’s following the gravity 🙂 I guess now it needs some collider to stand upon.

> [@Carring](#):
>
> but still no collisions.

Yes, note that if you set `Size.Z := Max(1000.1, Size.Z);` for both, then you either always or never have the collision (depending on some unknown to me factors). I’ll try to study this issue later. We seem to do exactly the same as `physics_2d_collisions` example does, but are getting wrong result. Colliders seem to have correct sizes. Could those be some scene settings in Castle Editor?

---

<div class="post-metadata">

**Author:** ![Carring](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/carring/32/1950_2.png) [@Carring](https://forum.castle-engine.io/u/Carring)\
**Post date:** [May 3, 2021, 6:38pm UTC](https://forum.castle-engine.io/t/2d-scene-collision/322/31 "2021-05-03T18:38:48Z")

</div>

I have not changed anything in the editor except the translation z.  
See “westbeach-gamestateplay”.

The Size.Z was a tip of Michalis, because Z must not be 0. But I don’t understand it with the collision.  
Yes, I studied the 'physics\_2d\_collisions` example over and over and don’t see what is wrong.

---

<div class="post-metadata">

**Author:** ![eugeneloza](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/eugeneloza/32/12_2.png) [@eugeneloza](https://forum.castle-engine.io/u/eugeneloza)\
**Post date:** [May 3, 2021, 6:41pm UTC](https://forum.castle-engine.io/t/2d-scene-collision/322/32 "2021-05-03T18:41:44Z")

</div>

> [@Carring](#):
>
> I have not changed anything in the editor

But the example uses manually constructed Scenes and it works. The game uses Scenes derived from Design, and it doesn’t work. Some default values might have gone unnoticed. Or some global setting not set or set in a wrong way.

---

<div class="post-metadata">

**Author:** ![Carring](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/carring/32/1950_2.png) [@Carring](https://forum.castle-engine.io/u/Carring)\
**Post date:** [May 3, 2021, 6:47pm UTC](https://forum.castle-engine.io/t/2d-scene-collision/322/33 "2021-05-03T18:47:26Z")

</div>

So, conclusion is Michalis should take a look at the scene properties in the editor because designed scenes do not work with collision or there should be some properties set extra in the editor?

---

<div class="post-metadata">

**Author:** ![eugeneloza](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/eugeneloza/32/12_2.png) [@eugeneloza](https://forum.castle-engine.io/u/eugeneloza)\
**Post date:** [May 3, 2021, 6:52pm UTC](https://forum.castle-engine.io/t/2d-scene-collision/322/34 "2021-05-03T18:52:02Z")

</div>

No, of course he might help here out (and I’ve already played with settings in Castle Editor for some time - nothing obvious is missing), but we have to figure out what the code game does differently than the example.

---

<div class="post-metadata">

**Author:** ![Carring](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/carring/32/1950_2.png) [@Carring](https://forum.castle-engine.io/u/Carring)\
**Post date:** [May 3, 2021, 7:16pm UTC](https://forum.castle-engine.io/t/2d-scene-collision/322/35 "2021-05-03T19:16:02Z")

</div>

Maybe it is because I have placed the Scenes under a Transform.  
The ‘PlayerScenePhysics’ procedure causes strange behaviour with the Translation coordinates.  
When I change direction the ‘direction’ scene jumps to another position, so the different Scene translations don’t follow the parent Transform Translation. When I switch off the procedure all scene translations are the same as the Transform again.

![image](https://forum.castle-engine.io/uploads/default/original/1X/96d13102e0f2ae4a08975ee94e0c80357b392f4f.png)

---

<div class="post-metadata">

**Author:** ![eugeneloza](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/eugeneloza/32/12_2.png) [@eugeneloza](https://forum.castle-engine.io/u/eugeneloza)\
**Post date:** [May 4, 2021, 6:01am UTC](https://forum.castle-engine.io/t/2d-scene-collision/322/36 "2021-05-04T06:01:17Z")

</div>

> [@Carring](#):
>
> Maybe it is because I have placed the Scenes under a Transform.

I tried to reference those directly yesterday, and it didn’t work 🙂 And overall, physics should work on Transform without any issues. I’ll give it another round hopefully today evening 🙂

---

<div class="post-metadata">

**Author:** ![Carring](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/carring/32/1950_2.png) [@Carring](https://forum.castle-engine.io/u/Carring)\
**Post date:** [May 4, 2021, 5:07pm UTC](https://forum.castle-engine.io/t/2d-scene-collision/322/37 "2021-05-04T17:07:05Z")

</div>

Thanks for your help.  
The “jumping” scenes are strange though. Maybe this is another problem with the physics as I don’t understand why these only occur with the physics procedure.  
But one problem at a time. The collision mission first.  
😉

---

<div class="post-metadata">

**Author:** ![michalis](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/michalis/32/3_2.png) [@michalis](https://forum.castle-engine.io/u/michalis)\
**Post date:** [May 6, 2021, 4:16pm UTC](https://forum.castle-engine.io/t/2d-scene-collision/322/38 "2021-05-06T16:16:45Z")

</div>

@Carring Please put the complete code+data somewhere public, so that I may help best. Apologies if you already put it publicly (from what I see you only send it to @eugeneloza ), direct me to it.

Best to submit it here on the forum (everyone should have a link to attach files in the forum posts since some time). Or place it on GitHub. As a last resort, upload zip anywhere and post a link here.

I understand you ask for my help, but admittedly this thread got long, complicated and I just got lost 🙂 I have a feeling you may use physics for something that doesn’t even need physics (and may be solvable by simple `TFloatRectangle.Collides` call), but I may be wrong.

Please point me to the complete code+data I can try on my own, and say where’s the problem.

---

<div class="post-metadata">

**Author:** ![Carring](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/carring/32/1950_2.png) [@Carring](https://forum.castle-engine.io/u/Carring)\
**Post date:** [May 6, 2021, 4:36pm UTC](https://forum.castle-engine.io/t/2d-scene-collision/322/39 "2021-05-06T16:36:20Z")

</div>

> [@michalis](#):
>
> I have a feeling you may use physics for something that doesn’t even need physics (and may be solvable by simple `TFloatRectangle.Collides` call), but I may be wrong.

I know FloatRectangle.Collides works but remember my topic "starling vs. castle-sprites 🙂  
It is too rough as it takes the largest sprite-scene in the atlas (for instance character walking with wide legs) and does not shrink/bound with a smaller scene (standing character).

I will try to find out how to upload the complete code this evening; data is a bit too large to put in the forum. 🙂

---

<div class="post-metadata">

**Author:** ![Carring](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/carring/32/1950_2.png) [@Carring](https://forum.castle-engine.io/u/Carring)\
**Post date:** [May 6, 2021, 6:02pm UTC](https://forum.castle-engine.io/t/2d-scene-collision/322/40 "2021-05-06T18:02:32Z")

</div>

I just send it to you by mail as it is 70mb and I can only upload 4 mb zip in the forum.

Mail subject is: 2D scene collision problem

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![eugeneloza](https://forum.castle-engine.io/user_avatar/forum.castle-engine.io/eugeneloza/32/12_2.png) [@eugeneloza](https://forum.castle-engine.io/u/eugeneloza)\
**Post date:** [May 6, 2021, 6:30pm UTC](https://forum.castle-engine.io/t/2d-scene-collision/322/41 "2021-05-06T18:30:58Z")

</div>

And sorry, I still didn’t manage to get a deeper research of your issue… It’s lying right before my eyes in the center of Desktop - but this week is kinda ~~busy~~ disorganized for me…

[Previous page](https://forum.castle-engine.io/t/2d-scene-collision/322.md?page=1)

[Next page](https://forum.castle-engine.io/t/2d-scene-collision/322.md?page=3)
