Bodgers – Coolest Projects UK

As you may remember from before Christmas Kevin, Zack and Barry won a prize in the Raspberry Pi Pioneers competition, part of that prize was an invitation to Coolest Projects UK which was held in London last Saturday.IMG_20180428_130215

The first thing that struck us when we got there was how small the event was, there was about forty projects there, compared to Coolest Projects in the RDS. However this is the first time that Coolest Projects has been run in the UK so it’s a very good start. We also noticed that at least half the projects were hardware based which reflects the fact that there are more Raspberry Jams etc. than CoderDojos in the UK.

The day started with us setting up our “Piggy In The Middle” project, followed by a very nice speech by Philip Colligan. The lads then demoed their project to the public while they were waiting for the Judges. After the judging was finished there were science shows by Greg Foot and Neil Monteiro as well as stands to keep the kids entertained, Zack and Kevin really enjoyed the huge version of Connect 4 that was there. In what has now become Coolest Projects tradition for Kevin and Zack they had their picture taken again with Philip Colligan CEO of the Raspberry Pi Foundation.

Db3elddWAAA30cE

Then it was time for the results, the Mobile category award went to Meriem Ait Ziane for her app of healthy eating recipes and advice as well as support for diabetic people and a personalized diet plan according to your health conditions.

The Games category was won by Hamdan Syed and Amiyan Ezdi. The game they coded is called Calorie Challenge, which challenges you to remember the amount of calories in each meal/snack.

The Website award went to George Hart for his project called “Educelevate”. He designed the website to educate children of all ages on various topics.

The Scratch category was won by to Gabriella Jenkins and Liya John for their game “Toad Ahoy”.

The Hardware category went to Avye Couloute for her project: Voice O’tronik Bot. We were all very impressed by both Avye and her project and were delighted to see her win.

We enjoyed our visit to Coolest Projects UK and we can’t wait for Coolest Projects in the RDS.

Back in Athenry Dave helped the rest of the Bodgers with their projects.

Creators Quiz Time

This week at Creators we took some time out to play some games and do some quizzes – well done to everyone that took part!  We used the fantastic website “Kahoot!” to drive the quizzes.  Some of you had used this site before in school.  We had a little trouble getting it going due to network trouble, etc but got it sorted and interestingly saw how we could debug the issues in the kahoot javascript using the console to see that it was the network causing the problems, just like we did with our own code.

Anyhow the results of the quiz are below – everyone did brilliantly as expected but LORD PJO showed her quickfire dominance by amazingly winning two of the three games:

We then did a rapid-fire challenge to do a circle that changes color when the mouse is over it.  This is one of those ones that is easy in scratch (“touching mouse”) but we hadn’t covered collision detection really in javascript so you had to figure it out.  Most all of you got it, some with really clever solutions!  My pretty simplistic solution is checked up to github as usual.

Next week we have a break and then we plan to spend a week working on our own ideas – anything you like – either bring in a project that you are working on and Kieran and I will try to give advice or just start a new one in the class and we’ll try to get you off on a good footing.

A WebGL 3D Solar System!

solarsystem

This week we looked at WebGL and how it can be used in Javascript to easily create 3D animations and games.  After looking at different shapes for a while, someone suggested building a solar system so that’s what we did!  This project just did the first sample planet.

The code is up on github as usual!

Click here to view the project we did on Saturday!

What is WebGL?

WebGL is a javascript API that allows your programs to use the graphic cards on your computer.  Graphics cards are built for really fast graphics and have specially encoded hardware to quickly do the calculations needed for common graphics.  With 2D, you can often get away without graphics cards, but they really make things a lot faster in 3D!

There are several javascript libraries that let you use webGL – the most famous and probably the best of them is one called three.js, which lets you do really advanced 3d – however our trusty p5.js also has a pretty decent set of functions and for us is much easier to use.

Initializing the p5 canvas for 3D

The great thing we found about p5.js 3d is that is is REALLY similar to the 2D shape functions, so much so that we all intuitively were able to get going with little need to look at documentation.

Sadly, it’s not easy to mix 2D and 3D code in P5 as the “canvas” is created differently.

To get it started, we had to add one word to the createCanvas function call – “WEBGL” – so to create a canvas we use the function:


setup() {

createCanvas(800, 600, WEBGL);

}

P5 3D primitives

There are many better reference guides online so I won’t go int great detail here but the main built-in shapes that you can use to create things with are:

  • Box for all manners of cuboids
  • Plane for a flat plane (a “rect” also works actually!)
  • Sphere for a (duh!) sphere
  • Ellipsoid for a rugby ball shaped thing
  • Cone for a cone-shape
  • Torus for a donut shape
  • Cylinder for a cylinder

Using these basic building blocks, it’s possible to create all manner of shapes.

There is also a “loadModel” function that allows you to read in a model from a 3D package like “Blender”.  These can be models of spaceships, bad-guys, houses, ponies, whatever you like!  Maybe at some stage in the future we’ll try to learn a bit about blender.  One of the ninjas already had played with blended and managed to load in a model of a meteorite he’d been working on!

Positioning shapes

The models and shapes don’t have any coordinates on them – it’s all stuff like size and grid size.

Our first program involved creating a box and moving it around.

To position the shapes you use the “translate” function, just like you can in 2d. With this we moved the shape around


function draw(){
background(50);

push();
translate(0, 0, 0);
box(100, 100, 100);
pop();

// increase X - move right
push();
translate(200, 0, 0);
box(100, 100, 100);
pop();

// increase Y - move down
push();
translate(0, 200, 0);
box(100, 100, 100);
pop();

// upper left and closer to viewer
push();
translate(-200, -200, 200);
box(100, 100, 100);
pop();
}

We quickly noticed that the axis are slightly different:

  1. The box appeared in the center of the screen – i.e. 0,0 is the middle of the screen (like scratch) rather than the top left. (Box 1 below)
  2. Making X bigger moved it to the right (Box 2)
  3. That making Y bigger still moved it “down” unlike scratch! (Box 3)
  4. That making Z bigger moved it towards the viewer (Box 4)
  5. That there is perspective automatically built in

cubeFade

To Rotate shapes, there are three functions that are handy: rotateX, rotateY, rotateZ – these can rotate around the axes.  Just like in 2D the ddefault unit is radians so you need to set angleMode(DEGREES) if you want degrees.  For example – rotating the box by 45 degrees is per below:

angleMode(DEGREES);
rotateX(45);
box(50, 50, 50);

Push() and pop() can be used to stack the transformations on top of each other – just like in the 2d sketches!

Lights!

We learnt that there are three types of light:

  1. AmbientLight is light which comes from everywhere – it casts no shadow.  We just give it a color ambientLight(255) is a white ambient light for example
  2. pointLight is a light which is centered on a point.  It just has a color and a position – e.g. pointLight(r, g, b, x, y, z).  Pointlights are pretty dim, but you can stack lots of them on the same location to make them brighter – this is what we did for our sun!
  3. directionalLight is a light which has a color and a direction – this is very similar to the pointLight except that the light just shines in one direction and can be used for e.g. spotlights.

Material!

Depending on the material, the light reflects different ways – the main material types we tried were:

  • ambientMaterial: This is normal “Matte” material
  • specularMaterial: this is reflective material – it shines back like glass or water
  • normalMaterial: This is the default material type – this is kind of weird looking to me and I’d usually use ambient…

For each of these types, you simply set the material before drawing the shape – kind of like the fill function.

Explaining the Solar System Code

Our “Solar System” code was very basic – what we have here was one planet orbiting around a Sun in a spherical orbit, with a moon orbiting around that planet.  We didn’t have any physics in there – just spheres orbiting each other.

We did the following:

  1. set nostroke
  2. started a rotation around the Y axis
  3. created a bunch of lights at the center where the sun is
  4. drew a big yellow sphere for the sun
  5. translate 300 pixels out to draw a blue sphere
  6. set it’s rotation to 12 times faster than the “earths”
  7. moved 50 pixels out.
  8. drew another small white sphere

This sounds like a lot but the code is really simple and can be seen here.

Challenge!!

We did one planet and moon in the class because a lot of the time was spent playing with our own projects.  How about you take our solar system and change it so that all the planets have accurate relative rotation speeds?  i.e.

Mercury: 87.97 days (0.2 years)
Venus : 224.70 days (0.6 years)
Earth: 365.26 days(1 year)
Mars: 686.98 days(1.9 years)
Jupiter: 4,332.82 days (11.9 years)
Saturn: 10,755.70 days (29.5 years)
Uranus: 30,687.15 days (84 years)
Neptune: 60,190.03 days (164.8 years)

Try putting a ring (torus?) around Saturn!

The distance from the sun could also be put in, but it might be a bit weird looking – in fact you wouldn’t even see most of the planets as they get pretty spaced out as you move out!

Fractal Fun In Javascript

This week in Creators we looked at a brand new concept – functions that actually call themselves – also known as recursion.

recursion1

Though this seems at first glance like a really silly thing to do, we were quickly able to use it to generate some pretty effects.

The main things to remember about the recursion are:

1. Make sure to give it something that will let it exit – a function calling itself forever will not work and the browser will crash or throw an error.  For us, we passed a value as a parameter and made it smaller each time, exiting when it fell below a certain value.

2. Do the math! Recursion leads to big numbers of operations very quickly and can slow down or crash a browser easily.  Make sure to save often and think about the amount of times your function will run.

Recursion is really handy for cases where there are lots of operations that are similar.

Circle Pattern Project

In this project, we wrote a function which draws a shape (we picked a circle) before calling itself three times to draw the shape to the left, right and below.  The code is below:


function drawShape(x, y, size){
if(size > 5){
stroke(random(255), random(255), random(255));
fill(size, 50);
ellipse(x, y, size, size);
drawShape(x+size/2, y, size/2);
drawShape(x-size/2, y, size/2);
drawShape(x, y+size/2, size/2);
}
}

Notice the If statement?  This means that it will not bother drawing circles smaller than 5 pixels in diameter – this allows the code to work.

This simple code generated the following pretty fractal:

fractal_circles

Fractal Tree

For the next project, we used very similar code to create a beautiful fractal tree.  In this case we wrote a “drawBranch” function which can draw a branch at a given angle and length.

We added a little code to allow us to change the angle that the branches fan at – the fanangle – dynamically.  Depending on the variables, we could slow down our computer quite easily – the trick is to think about how many lines your computer will need to draw.

recursive-tree-steps

Each function called itself to draw a pair of smaller branches off of it.  The tree is shown below – just before I checked the code in, I added a little if statement to color the branched brown if the length is > 50.  The result is below:

fractal_tree

This looks quite pretty and is fun to play with if you have a pretty fast computer – play with it here:

https://coderdojoathenry.github.io/creators2017/week16-tree/index.html

As usual, all of our code is on the Creators 2017 github repository.

Week 6 2018 Explorers – Greeting Card

Hi everyone,

Good to see everyone after our unplanned extended break, but it was nice to see some snow too!

This week we did a Greeting Card, I did a birthday card but some of you did a Mothers Day, I’m sure all your  Moms loved them.

I have uploaded the finished card up to the scratch website. You will find the log in details in the notes below.

Here are this weeks notes in PDF CDA-S7-Week 6-GreetingCard.PDF

Enjoy the break!

 

Martha

Advancers: Motorway Madness

This week in advancers, we simulated a true story!  Once Mark was driving past Athlone and encountered a car driving the wrong way down the motorway.  In this case it was a poor old gentleman who had somehow ended up turning down the sliproad, but we wrote a game which simulated the incident from a crazier drivers point of view!

The idea of the game was to drive as far as possible the wrong way down a 4-lane motorway before you crash.

The finished game is here if you want to play with it or look at the code!

Step 1: Draw the car

Out first step was to draw a car – we did this by switching to “Vector” mode and dragging a few boxes together – this is mark’s car below:

motor-01-draw-car

Step 2: Draw the road

The next step was to draw the road – the road was drawn the same way, using vectors.  One key point here was to space out the lanes more or less evenly, and to use the “Shift” button to ensure that the lines on the road were at 90 degree angles.  We drew the gaps in the white line using grey lines the same colour as the road.  A few bushes and buildings completed the scene!

motor-02-road

Step 2: Make the road scroll!

In this game, we wanted the road to scroll from top to bottom.  This meant that the ySpeed of the road was negative.  We also wanted the road to move back to the top whenever it got to the bottom.  We had done side-scrolling with multiple sprites year ago in Explorers, and this was the same idea – however we managed to do it with just one sprite by using clones in the code below:

motor-03-road-scroll

 

Step 4: Make the Car Move!

In this step we started off with basic movement – we were going to get to more realistic physics, but ran out of time!  I’ll see if I can show the code for this at the bottom and you can add it in yourself!

To make it move, we just checked for left and right arrows and added speed accordingly:

motor-04-player-code

Step 5: Enemies!

The game was already starting to feel fun, but it’s a lot better with some unsuspecting traffic on the road!  We cloned the “player” sprite, and created a bunch of costumes to represent different styles of vehicle:

Next we wrote some code VERY similar to the scrolling background code for the enemy – this also reappears at the top when it reaches the edge – the difference being that it appears in a random lane and with a new costume.  This turned out great!

Final Step: Game Over

The final step was a “Game Over” text sprite – this shows whenever the player crashes!

This was a really fun game – I enjoyed making it a lot!

Exercise: Realistic Turning Physics

I said I’d have a go at realistic turning physics for the car – this code works and looks great – try adding it to your game to see what you think – basically it turns the front of the car realistically and the back skids around like a real front-wheel-drive car.  It was a bit tricky so I’m glad I didn’t push it as people would have gotten really tired as I tried and failed a few times!  Just pop this code into your “player” script anywhere near the end!

motor-07-realistic-physics

The game looks quite cool when finished!