Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / 2D Physics producing unexpected results!

Author
Message
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 21st Dec 2017 11:42 Edited at: 21st Dec 2017 11:51
I'm dabbling with 2D physics and getting some funky (unexpected) results.

The problem:
I have a series of sprites that should all be travelling at a constant velocity but some choose to follow a curved path and randomly accelerate for no obvious reason!

The sprites are all set up in the following way:
They are all initialised with a random velocity and a random amount of angular velocity.
There is no gravity.
Each sprite has no friction.
They all have the same mass and restitution.
They are all set as Dynamic so that they can collide with each other and therefore change direction.
When they reach the edge of the screen their velocity and angular velocity is stored, the sprite is wrapped to the other side of the screen and then the velocity is restored.
Each sprite is made from the same looping animated spritesheet.
They are all set as polygon collision shapes which is recalculated every frame to account for the change in shape due to the animations.
Their mass is also reset each frame because recalculating the polygon shape also affects the mass.

Here's the sprite set up code:


The array rock[] is prepopulated with blank sprites with no physics attributes.
The array sourcerocks[] holds the sprite images and the frame details for animation.

Here is the update code


The CheckCollision() function is not relevant because I can comment that out and the funky effect still persists.

There are no external forces and the change in velocity doesn't happen to all of the sprites, so I'm convinced that its something that I'm doing wrong and not a problem with the physics but I can see no reason why some of the sprites should randomly change velocity. So I'm open to suggestions.
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 21st Dec 2017 14:31
Hi Scraggle. Would you mind posting a small ready made project for us to test? I'm far too lazy to create the types, arrays, animated media, screen setup etc. to test your functions.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 21st Dec 2017 16:59 Edited at: 21st Dec 2017 17:00
I would guess it's because you are manually setting the mass of the sprites. In my experience it's safer to let the mass be automatic and only edit the physics scale to get the behaviour you want. Also you seem to be setting the shape and mass of each rock during every update which seems a bit excessive anyway.
Using AppGameKit V2 Tier 1
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 21st Dec 2017 17:08
Quote: "Also you seem to be setting the shape and mass of each rock during every update which seems a bit excessive anyway."

It does seem excessive but it doesn't appear to hit the frame rate too much.
But it is necessary, as I said in the original post:
Quote: "They are all set as polygon collision shapes which is recalculated every frame to account for the change in shape due to the animations.
Their mass is also reset each frame because recalculating the polygon shape also affects the mass."


Quote: "Would you mind posting a small ready made project for us to test?"

I've just knocked together a stripped down test but it worked perfectly. I suppose that's a good thing though. At least now I know where the fault isn't.

Login to post a reply

Server time is: 2024-11-24 05:58:09
Your offset time is: 2024-11-24 05:58:09