Learning exercises requested

Hi guys,

I’m new to Codea and Lua (not to coding in general) and am learning Codea one step at a time. I’m almost done with the “Codea for beginners” pdf by ignatz, which I’ve found to be a real good help!

As with anything, the best way to learn is by doing. So at the end of each chapter I gave myself some tests that I felt should be possible with the knowledge I just earned. Nothing too fancy, but just enough to test my competences.
I was wondering if you guys had some more exercises I could attempt (please add an estimated complexity & if required what must be known before starting)?

As an example, these are a few of the tests I gave myself (all beginner level off course :slight_smile: )

  • Draw a randomly sized rectangle and make it continuously move up and down over 200 pixels in the center of the screen (I didn’t have the skillset yet to use tween, so I coded each pixel movement)
  • Add an image to the project and make it “pulse” (so zoom slightly in and back out continuously)
  • Add a line that starts where the touch started and ends where the touch ended (Yay, I reached touch code!)
  • Place a circle in the center (pretend it’s a planet) and make a smaller circle “orbit” it (calculate the distance from the center of the planet and keep that distance)
  • Add to the above: remove the orbiting planet and replace by a triangle (which represent a satellite) that always has the same side towards to planet surface

I’ve just finished reading the physics and mesh chapters, don’t have any tests for those yet but I will invent some by the time I get home from work :slight_smile:

So, who’s got more little tests for me? As you can see, I’m still real basic, but I’m getting there :wink:

@MrBramme One suggestion is to look thru previous discussions for small programs. There has to be hundreds of them that show examples of specific things. You can load those to see what they do then try to understand what’s happening. Change things in each program so you get a feeling of what the code does.

@MrBramme - two of my step by step tutorials (simple Lunar lander game and simple Snake game) have specific learning exercises (challenges) in them:

https://bitbucket.org/TwoLivesLeft/core/wiki/Step%20by%20step%20projects

@MrBramme - welcome!

See the step by step projects on the wiki link above

Also search the forum for “50 line” as there was a competition some time ago for small 50 line projects (all in one thread) and they would be useful to study

After that, it’s up to you. I would just explore and find something you want to try building. In addition to my Codea ebook, I have one on Lua you should read (if you haven’t), also on 3D, shaders and lighting, but those are more advanced topics and maybe I’d wait a while.

Thanks for the input (and a special thanks to Ignatz for the ebooks :slight_smile: I believe I have a version from 2013 and didn’t find an update if there is one)!

The 50 line code competition sounds exactly like something I’m looking for. I try not to “limit” myself to tinkering with settings of existing code, but to start writing it myself as well at an early stage. When you’re “forced” to do something yourself, there’s a better chance to make mistakes and learn rather than copy/paste and modify a number :slight_smile:
You have no idea how hard it was for me to find the X,Y coordinates of my touch while moving, start was easy :stuck_out_tongue:

EDIT: btw Ignatz, if you’d like more detail on my little excercises to add to your book (with perhaps the answers at the end), feel free :slight_smile:

@MrBramme - your examples would be good, thanks. I started out doing examples at the end of each chapter, but it was a chore so I focussed on writing instead.

Send them to me any way that is convenient. :slight_smile:

@Ignatz - I’ll have to write them down first :slight_smile: I don’t have the code for each of them anymore though :frowning: I worked in the same “test” project all the time and simply removed everything and started over each test.

But right now I’m trying to do this (based on what I learned from your book so far): “Have a planet rotating in the center of the screen” → done
"Have a moon orbitting the planet, while rotating in the other direction of the planet " → done
“have astroids flying around the screen” → todo
“Be able to shoot down the astroids with the evil death lazzzzzer that you’ve built on your planet’s surface” → todo

it’s going to be a tricky one not knowing everything i should know from Codea & Lua :slight_smile: I hope I can manage it, if not I’ll have to continue learning first.

@MrBramme - don’t worry about it then, you keep working on your project

The best way to learn is to try something difficult!

@Ignatz - Okido. I can, relatively easy, send you the exact tests that I gave myself at the end of each chapter, if that helps. :slight_smile:

@MrBramme Since I am pretty much a self learner I’ll tell you how I learn new things.

Most oftenly I get a random idea for either a game or just a cool thing that I would like to create, at this stage I might know how to do the simpler stuff but there might be parts that I have no clue about how I would create them.

I then start out with what I know and if there are some new functions or libraries I need to use I just look up on how they work and how I use them.
Very often it happens that I get ‘stuck’ on some piece of code in a way that I cannot figure out how I will fix it, then if not googling around works I’ll just go to a forum(like this) and ask around. Most of the time there is someone who knows the answer.

And this is mostly how I work with new code or just creating something that requires some new knowledge, it does happen that I cannot solve a problem and just move on to another project but most of them time the problem is that I might not understand something properly or just loose interest, it happens…

But most importatly, if you do not understand something, google it and see if someone else explains it in a way that you can understand it and make use of that knowledge to find new things to learn :slight_smile:

@MrBramme - one thing I would strongly suggest, though, is to add code incrementally, a little at a time, and check it works before going further. I wouldn’t dream of starting with a pile of code like you have there. It just gets way too hard to find errors.

thanks for the tips! My first “Game” is coming along nicely. I’ll share it when I’m done so I can feel ashamed in & year at what level my coding was (just kidding) :stuck_out_tongue:

I’ve started simple indeed, placing the image i wanted, where i wanted it, then making it rotate, then adding another etc etc. pressing play every few steps to make sure it rund correctly and using the print command to verify certain calculations (like coordinates etc).

Right now I’m trying to figure out what the absolute X,Y position is of random generated images so i can run collision testing on other random generated images. Both have been transposed & rotated hence my need for “absolute” (unrotated and starting at the actual 0,0) coordinates.
I can feel I’m close (and see i’m close using the print command), but i’m still a bit off from what i can visually verify. I figured my math was sound, but it doesn’t math the actual on screen result (i’ve added “gridlines” to help me verify). As far as i’ve found on google there’s no real command to say AbsoluteX = (transformbase, rotatedeg, currentx).
Once I’m done, and i’ve shared the code, I can’t wait to see what efficiency gains you guys can teach me.

But I’ve started the Lua for beginners book as well now :slight_smile:

By daughter likes the game allready (Look at me daddy, I touch this device and magically colors appear!).

@MrBramme - if you transpose to (say) 300,200, then draw your image at 0,0, your actual co-ords are of course 300,200, whether rotated or not. But I’m sure this isn’t what you’re talking about.

I can see your problem if you are transposing, rotating, then drawing from that position at a non zero x,y. Is that what you’re talking about? Can you give us a clearer explanation?

Sorry :slight_smile: but you’re correct. basicly I’m firing a laser (line) at rocks (sprite). I’m at work so don’t know the exact numbers but in a nutshell:
i transpose to a random between (100-width/2,100+width/2), then i rotate to the degree from that spot to where i touched. Then my laser moves along the line between the transposed startpoint and the touch point. (so i do have a bunch of absolute numbers to work with). ps: i’m not sure if absolute is the correct term, but in my mind you have absolutes (always based on 0,0) and relatives (based on the transposed coordinates).

The initial spot is easy, like you said, but it’s when it is moving along the line that it’s harder. My current mathematical thinking tells me this (i’ve split one calculation for the X coordinate, the other for the Y but it’s identical reasoning):

Startpoint = my transposed X coordinate (so i’m talking about absolute coordinats here)
Endpoint = my touch X coordinate (so i’m talking about absolute coordinats here)
Distance = the number of times i’ve moved my laser
TotalDistance = the distance of the start to the end
AbsoluteX = startpoint + ((endpoint-startpoint)*(Distance/TotalDistance))

So basically i’m taking a fracton of the lenght of the “to travel” X line. If i haven’t figured it out tonight after some tinkering and your feedback here, i’ll post my exact code :slight_smile:

I’ve just tested this logic in excel real quick (I love excel btw :)), and it seems to be the correct logic. My only explanation is that I’m using wrong inputdata, or to be more precise, that i’m doing something to one of my input variables that i shouldn’t be doing :slight_smile:

@MrBramme - it sounds like a simple sin or cos calculation to determine the new x,y position

If the angle is a (in radians), then if you move d pixels along the line from your translated point X,Y, then the new x,y position is

x = X + d * math.sin(a)
y = Y + d * math.cos(a)

(PS I love Excel too, that is my specialty rather than Codea)

thank you for the formula, i’ll have to give it a try tonight :slight_smile: I have noticed that i SERIOUSLY need to freshen up my (don’t know the word, triginometry?) math with triangles for codea :smiley:

@MrBramme - we (mostly) all struggle a bit with trig. Here is a nice page

http://www.mathsisfun.com/sine-cosine-tangent.html

@Ignatz - I bet we do :smiley: I used to be kick ass at it at the university, sadly a few years later I forgot a lot about it. I use a lot of math for my job, but not this stuff. I found a “class” in khan academy about it as well.

@Ignatz What about normalizing it? It’s less expensive than trigonometry.

time = 0.5
startPosition = vec2(50, 100)
endPosition = vec2(200, 300)
distance = startPosition:dist(endPosition)
normal = (endPosition - startPosition):normalize()

(Pseudo-code, should work, run that code only once unless you update the positions, change the time whenever you want, and time is on a scale of 0-1)

Then the output should be startPosition + normal * time * distance