shapes icon indicating copy to clipboard operation
shapes copied to clipboard

write a guide/tutorial

Open ublubu opened this issue 8 years ago • 17 comments

this one's for you, @schell

comment with your questions, so i know what to write about

ublubu avatar Jul 13 '16 22:07 ublubu

My goal is to get an mvp working within my game. It's a pretty traditional setup - one main loop with a time tick, physics tick (currently empty), scripting tick and render tick. I'd like to be able to associate game entities with physical bodies and update them in between physics ticks (applying an impulse here or changing the mass there, etc).

From my browsing I think I'm mostly interested in the concrete types and functions, basically the contents of

Physics.Engine Physics.Engine.Main Physics.World etc

I'm using an entity-component setup where entities are Int keys which I can use to associate to different components. In the case of the physics system I'm guessing the component may be a WorldObj. Every physics tick would then consist of gathering the entity's keys and associated WorldObjs, constructing the World WorldObj, constructing the Scene Engine and then running runWorld or some such function, decomposing the result back into updates to the entity's components. From that you can probably guess what I'm trying to do and what I don't understand about the system so far ;) The current problem in my brain is how to feed the time delta to runWorld, as it takes integer steps. My guess is it's somewhere in Physics.Engine.Main - I'm looking at it now ... looks like it's fixed? Anything that would help me get on the right track would be greatly appreciated 👍

One quick question though - it seems there are a lot of big typeclasses and functions that require Proxy - I'm trying to steer clear of them for now as I'm guessing they're only needed internally. Did you make a conscious decision to use typeclasses over good old records? I've found that my code becomes drastically easier to refactor and "see" when using the method described in this article. You can see an example between my project renderable 0.1 and renderable 0.2.

Okay! Bed time for me! Thanks again 💯

schell avatar Jul 15 '16 05:07 schell

BTW - I only mentioned the typeclass/record thing because it looks like a lot of the typeclass functions require you to pass a Proxy anyway - so you might as well be passing the record and get rid of the constraints. I've asked around a bit on IRC how that would affect performance and everyone seems to think it would either improve only very slightly or have no effect - since apparently that's what GHC does under the hood anyway, using a dictionary.

schell avatar Jul 15 '16 06:07 schell

Hey, that's a good tip. I thought about doing it that way, and maybe I should. I still think I need the type families, but I should be able to put the functions in a record.

WRT Physics.Engine.Main, I'd recommend replacing whichever pieces you want to customize. It's not very parameterized since I've only been using it for testing/benchmarking so far. In the long run, there will probably be multiple implementations for some pieces, so library users will mix and match to fit the kind of scenes they're simulating. Just use Physics.Engine.Main as a guide (an example of client code) for now.

ublubu avatar Jul 15 '16 14:07 ublubu

What is mu in

makeWorldObj :: PhysicalObj -> Double -> ConvexHull -> WorldObj
makeWorldObj phys mu shape = woUpdateShape $ WorldObj phys mu shape

from Physics.World.Object?

schell avatar Jul 15 '16 17:07 schell

What is the unit of time for the delta in

updateWorld :: Double -> EngineT s World'
updateWorld delta = ...

from Physics.Engine.Main?

schell avatar Jul 15 '16 17:07 schell

mu is the coefficient of friction. It's usually represented by the Greek letter.

The time delta is in seconds.

ublubu avatar Jul 15 '16 21:07 ublubu

I've got a working demo going! demo

schell avatar Jul 18 '16 23:07 schell

Hey, that's great! I definitely want to get a "restitution" knob for you, so the boxes don't have to look like they're full of sand when they collide.

ublubu avatar Jul 19 '16 01:07 ublubu

How do I create static objects? Should I give them infinite or a really big mass?

schell avatar Jul 19 '16 23:07 schell

Ah - sorry - I figured it out - you set the mass to 0.

schell avatar Jul 19 '16 23:07 schell

How do I overlay static objects? Currently 'crossing' two static objects makes the sim explode (or something similar).

schell avatar Jul 21 '16 19:07 schell

Oh, crap. I shouldn't check for collisions between static objects. I've been a bit sick the past few days, so I haven't spent any time on shapes. I'll have a look at this and the PR stuff this weekend.

ublubu avatar Jul 21 '16 19:07 ublubu

ok, i fixed the static objects exploding everything. @611b2ee8ecd048f5c222918689c9a831601507aa

ublubu avatar Aug 02 '16 19:08 ublubu

b7ubqaIl48xS8

schell avatar Aug 02 '16 21:08 schell

And I'm glad you're feeling better :)

schell avatar Aug 02 '16 21:08 schell

thanks. me, too. i'll look into exposing dt tomorrow - unless there's a higher priority for you. maybe i'll also look into a task tracker, so i don't have to make up a sort order every time i look at the issues list.

ublubu avatar Aug 03 '16 03:08 ublubu

I'm actually off in graphics land for a bit, so there's no rush for anything on my behalf.

schell avatar Aug 03 '16 04:08 schell