Aidan’s Cooler in Contraption Maker – Implementation Details

By Kevin Ryan

Posted on April 22nd, 2016

Preliminary Note 1: Part one  of this subject can be found here: Aidan’s Cooler in Contraption Maker – The Backstory

Preliminary Note 2: So after trying, I find that embedding the code within this post just doesn’t work well. So you can find it all in one spot here. I’ll hook up the links in all the sections below as I write about each element.


aidansCooler

In this second post about Aidan’s Cooler I am going to briefly discuss and list the specifics about how I implemented it.

First you can find the code for the Aidan’s Cooler class here:

CMPartAidansCooler.h
CMPartAidansCooler.cpp

For Contraption Maker I wanted everything to be as data-driven as possible. So I created a CMPartManager class that would be responsible for making all the different parts in the game. My idea was that all the parts would be defined by JSON files which describe all the physical attributes of the part and then I’d have a base CMPart class that took care of all shared/general part methods. And then part specific code needed would be in part specific classes. In the end it almost worked that way.

This line is still in my CMPartManager.cpp file right before all the different sets of arrays that define all of the parts in the game:

// Hard coding here for now just to get 1st pass structure/data in place to work with
// -- will move into loadable JSON files

So unfortunately because of time constraints, something that sometimes happens during development, things never got moved into JSON files. It was a lower priority task. All the part definitions are data-driven, but from hard coded arrays instead of loadable JSON files. It may eventually happen, especially if I end up creating a part editor so that end users can create their own parts, but for now Aidan’s Cooler is defined by a few arrays. It’s not a hard thing to do, just takes time.

Here is the initial artwork that I received in order to create the cooler.

aidansCooler

Aidan’s Cooler Art

The Arrays

The group of arrays that completely define the cooler are here:

Code Excerpt #1

In each short section below I’ll briefly explain the details of each.

Preliminaries

Let me list out a couple of the macros before going over each separate element. These are used because I modified the physics engine we are using to used fixed point instead of floating point math and so I made a few macros to easily convert values between regular floating point and our integer system. You can read about the reason why used fixed point in this post: The Butterfly Effect

Code Excerpt #2

Polygon Vectors

First thing listed in Code Excerpt #1 above are the cpVect arrays. Each array contains the points of a polygon collision shape for the cooler. In the Chipmunk physics engine the polygons must be convex so for many parts multiple polygons are needed because their shape is irregular. The values in these arrays look ugly with lots of numbers and subtractions which were half the width/height of the artwork because I transferred each points value by hand from the artwork in Photoshop. I should have automated the process. Here is a sample of how I went about it from the Contraption Maker bear. Not a pretty process:

cmBearPolyVerts

Dynamic Bodies

Next up are the dynamic bodies which are just point masses. There is one body for the cooler body, another body for the cooler’s handle, and a third body for the wheel. Each of these bodies can move and rotate independently of each other. They are attached to each other by joint constraints that are described further down in this post. Here is the constructor for the dynamic body class:

Code Excerpt #3

Most of the variables are self explanatory.

  • id – Unique id for the body. Used by contraints, shapes, attachments, and artwork.
  • pos – Is just (surprise) the position of the body. It is usually just (0,0).
  • Mass – Mass of body
  • Density – Density of body
  • noGravity – Whether body is affected by gravity. Billiard balls aren’t effected by gravity. This is a carry over from what I did in The Incredible Machine where I was planning on making a billiard ball computer.
  • dampsRope – Whether this body will apply damping to attached ropes
  • shouldSave – Whether this body info should be saved in level files
  • next – Pointer to next body. All the different properties the describe a part are in null terminated linked lists.

The cooler has three bodies: the main container of the cooler, the handle, and the wheel. The main thing you see here is the offsets to the positions of the handle and wheel to the main cooler body and also the relative masses of the the three bodies.

Constraints

After the dynamic bodies are the constraints. There are joints that attached one body to another. You can check out this YouTube video which shows all the different Chipmunk constraints and joints in action. Here are the constructors for the rotary limit constraint:

Code Excerpt #4 – Rotary Limit Constraint

  • id – Unique id for the constraint
  • body1Id – First body that this joint connects
  • body2Id – Second body that this joint connects
  • minAngle – Minimum angle between these two bodies
  • maxAngle – Maximum angle between these two bodies
  • shouldSave – Whether this specific constraint info should be saved in the level file
  • next – Pointer to next constraint for this part

This rotary limit constraint is used to control how far the handle can rotate relative to the cooler body. The other two constraints are pivot joint constraints and they are used to connect the handle to the body and also the wheel to the body. Here is the constructor for the pivot joint constraint.

Code Excerpt #5 – Pivot Joint Constraint

  • id – Unique id
  • body1Id – First body that this joint connects
  • body2Id – Second body that this joint connects
  • pivot – Offset to location of where the two bodies are connected
  • shouldSave – Whether this specific constraint info should be saved in the level file
  • next – Pointer to next constraint for this part (null terminated linked list)\

If you look at the pivot joint arrays you’ll see that the body ids are set to hook the main body to the wheel and the main body to the handle.

Collision Shapes

The collision shapes for the cooler are made up of one circle and quite a few polygon shapes. As mentioned above polygons must be convex so many parts will need multiple polygons to correctly represent their irregular shapes. All of the collision shapes have Chipmunk group, type, and layers associated with them. Here is the code that has the defines for them:

Code Excerpt #6 – Defines for Collision Group, Type, and Layer

There a quite a few different shape properties that are defined. This code excerpt has the struct which has the three types of values define for each type of property and also a list of all the different types that Contraption Maker is currently using:

Code Excerpt #7 – Shapes’ Physics’ Properties

And there are also defines for different shape properties. This is so shapes can automatically play the correct sound effect when they hit different surfaces.

Code Excerpt #8 – Shapes’ Sound Properties

And here the constructor for the circle shape which is used by the cooler’s wheel:

Code Excerpt #9 – Circle Collision Shape

  • id – Unique id
  • pos – Offset to the position of the center of the circle
  • bodyId – The body that this collision shape is attached to
  • shapeProperty – Description of the friction and elasticity of the shape
  • shapeSfxProperty – Sound that this shape makes when hitting something
  • radius – The radius of the circle
  • group – The Chipmunk collision group this shape is in
  • collisionType – What type of collision callbacks this shape uses
  • layer – Which Chipmunk collision layers that this shape is in
  • shouldSave – Whether this specific collision shape should be saved in the level file
  • next – Pointer to next collision shape in a linked list

And this is the constructor for polygon collision shapes:

Code Excerpt #10 – Polygon Collision Shape

  • id – Unique id
  • pos – Offset to the position of the polygon
  • bodyId – The body that this collision shape is attached to
  • shapeProperty – Description of the friction and elasticity of the shape
  • shapeSfxProperty – Sound that this shape makes when colliding with something
  • verts – Pointer to an array of the vertices of the polygon
  • numVerts – Number of verts in the polygon
  • group – The Chipmunk collision group this shape is in
  • collisionType – What type of collision callbacks this shape uses
  • layer – Which Chipmunk collision layers this shape is in
  • shouldSave – Whether this specific collision shape should be saved in the level file
  • next – Pointer to next collision shape in a linked list

There are quite a few polygons for the cooler because there need to be separate shapes for the sides and the bottom on the cooler.

Rope Attachments

The cooler is set up so that a rope can be attached to the handle. This is handled in Contraption Maker by adding a rope attachment to a part. Any part can have any number of rope attachments. They just need to be part of the linked list of attachments in the part definition array. This is the rope attachment constructor:

Code Excerpt #11

  • id – Unique id
  • pos – Offset of the position of the rope attachment to the body it is attached to
  • bodyLinkId – The id of the body that this rope attachment is connected to
  • shouldSave – Whether this attachment should be saved in the level file
  • next – Point to the next attachment in a linked list

Nothing fancy with the rope attachment. Its location is just at the end of the handle and it is on the handle body.

Artwork

The final element describing the cooler is the only element that is actually seen on screen – the artwork. Everything else goes on behind the scenes. Each sprite is connected to a body and then tracks its position and/or rotation. There are few special cases where the artwork is connected to an attachment (like a rope attachment) instead of a body. Here is the rope attachment constructor:

Code Excerpt #12

  • id – Unique id
  • layer – Which layer it is draw on. Controls which sprites are drawn in front or behind each other
  • posLinkId – The id of the body whose position this sprite will track. -1 means don’t track any body’s position.
  • rotLinkId – The id of the body whose rotation this sprite will track. -1 means don’t track any body’s rotation.
  • spriteName – The sprite’s file name
  • width – Sprite’s display width
  • height – Sprite’s display height
  • partAttachmentId – Only used for special cases where we want this sprite to track an attachments location (like a rope attachment where the sprite would be the knot)
  • pos – Offset to the sprite’s location relative to the body
  • useFrameCache – Set to true if the sprite is inside of a sprite sheet
  • usedForBb – Set to true if this sprite should adjust size of part’s bounding box – use by the gui
  • shouldSave – Whether the info about this sprite should be saved in a level file
  • shouldPostProcess – Set to true if this sprite needs to be processed after physics have run. Used for cpu efficiency purposes so we can ignore lots of sprites each frame and only spend cpu time on those that need it.
  • opacity – Ranges from 0 to 255.  Used to control sprite’s transparency.
  • next – Pointer to next sprite in a linked list.]

If you take a look at the CMPartAidansCooler.cpp code, you should be able to figure out why the artwork for the word “Aidan” is reversed on one of the sprites.

Chipmunk Calls

Each of these constructors then use the definitions from the arrays to make calls to various Chipmunk routines to build up all the elements that make up the part. In the code excerpt below I’ve just put all those various calls in one place.

Code Excerpt #13

Final Wrap up

That is pretty much it. Here is a quick diagram of the various elements that make up Aidan’s Cooler.

coolerOverview

 

This post ended up being quite a bit longer that I thought it would be. Hope it is at least halfway helpful to someone.


Short Rambling Thoughts on the Discus, Tennis, Warren Zevon, and Flatlands

By Kevin Ryan

Posted on April 13th, 2016

I took a hike through the forest this morning (like almost every morning). Right near the end of today’s walk there is a log home which I’ve passed by many times. But today I noticed that they have a basketball hoop up in their driveway. “Oh, that’d be nice to have.” We can’t have one at our home because we don’t have any flat land. Actually, I guess we could put up a hoop and it would make for an interesting game since our driveway has a pretty good slope to it.

We didn’t think about how nice it would be to have some flat areas when we bought our land and built our home up here in the mountains back in the mid 1990s. Our log home is made of hand crafted logs that were shaped and fitted up in Canada before being taken apart and then traveling down to Shaver Lake, California. Amusing to think of the shell of our home on a few big trucks making their way down I-5 through Portland, Salem, Eugene (our former home), Mt. Shasta, and eventually making its way here.

logHomeInside

Our home under construction.

Discus

I was a discus thrower on our track team in high school and I was competing against people that usually outweighed me by at least 50 lbs and also had quite a few inches on me. Most of the discus and shot putters on all the different squads were lineman from the football team. I’m not built like a lineman. Try as I might, and I really tried, I couldn’t get my weight above 150 lbs. Trust me though, that “can’t get your weight up problem” does goes away when you get older.

I was good enough to get invited to some invitationals with some of the best in the state/nation where my relative size compared to them was even more apparent. My best throw was 151′ 11″ which wasn’t too shabby for my weight. The discus doesn’t rely on brute strength as much as the shot put so I was able to use technique to get on a more even keel with my competitors. Plus my math skills let me know that I wanted to release the discus at at 37 degree angle with a -10 degree angle of incidence.

discus1

Preparing for launch.

discus2

Away it goes

As a quick aside, those two photos were taken at our meet at McLane High School in Fresno. I recently found out that Warren Zevon went to that high school.

Tennis

Track and Field is a spring sport just like tennis, so you choose one or the other. Tennis fits my body type and skills much better than the discus did. Not sure why I choose discus over tennis. When I was younger I wasn’t too bad at tennis used to play quite a bit at the tennis club that my family belonged to. My older brother lettered in tennis at Fresno State and I think I would have had a good chance to do the same (at Oregon) if I had taken up the sport seriously.

Discus vs Tennis

When I was a senior in high school back in 1979, I remember sitting in my 6th period math class when Jeff Anderson started talking about tennis and the discus. He was on the tennis team. He thought tennis was a much better sport than just tossing the discus. He said, “When we get old I can call up a friend and say ‘hey want to play a round of tennis?’ What are you going to do? Call up someone and say ‘hey want to toss the discus around?” I said, “Uhhh. Yeah, I guess.” I think he had a point.

My Dad is 86 and he played tennis until recently when his knees gave out. He was Fresno State’s starting QB back when he was in college and I suspect his knees took a lot of punishment. Growing up I watched lots of tennis matches on tv with my Dad and brothers. Wimbledon and the US Open were the big matches, but the French Open was neat because it was played on clay which gives a whole different pace to the game. I remember watching an epic match between Borg and McEnroe at Wimbledon back in 1980. It was beyond belief wonderful. The sort of thing that the word ‘epic’ was made for.

borgMcEnroeWinbledon

Borg vs McEnroe Wimbledon 1980. Wow!

I wonder if I still have my old Wilson wood tennis racket somewhere. I should call up my little brother (50 year old little brother) and play a couple of sets sometime. Seriously. It used to be a lot of fun and its been a long time since I’ve played. Basketball needs too many people and the discus needs too few. To quote Goldielocks, tennis feels just right.

But right now I really feel like calling up my old high school friend, Jeff, and saying, “Hey wanna toss the discus around!”