2D Scene Collision

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.

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

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.

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

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

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.

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:

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.

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?

 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;

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

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?

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.

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.

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?

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.

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

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

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.
:wink:

@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 :slight_smile: 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.

I know FloatRectangle.Collides works but remember my topic "starling vs. castle-sprites :slight_smile:
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. :slight_smile:

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!

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…