Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Learning exercises requested

edited July 2014 in Questions Posts: 15

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 :-) )
- 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 :)

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

Comments

  • dave1707dave1707 Mod
    Posts: 8,064

    @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.

  • Posts: 749

    @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 by step projects

  • IgnatzIgnatz Mod
    Posts: 5,396

    @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.

  • edited July 2014 Posts: 15

    Thanks for the input (and a special thanks to Ignatz for the ebooks :) 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 :)
    You have no idea how hard it was for me to find the X,Y coordinates of my touch while moving, start was easy :p

    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 :)

  • IgnatzIgnatz Mod
    Posts: 5,396

    @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. :)

  • Posts: 15

    @Ignatz - I'll have to write them down first :) I don't have the code for each of them anymore though :( 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 :) I hope I can manage it, if not I'll have to continue learning first.

  • IgnatzIgnatz Mod
    Posts: 5,396

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

    The best way to learn is to try something difficult!

  • Posts: 15

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

  • @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 :)

  • IgnatzIgnatz Mod
    Posts: 5,396

    @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.

  • Posts: 15

    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) :p

    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 :)

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

  • IgnatzIgnatz Mod
    Posts: 5,396

    @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?

  • Posts: 15

    Sorry :) 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 :)

  • Posts: 15

    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 :)

  • IgnatzIgnatz Mod
    Posts: 5,396

    @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)

  • Posts: 15

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

  • IgnatzIgnatz Mod
    Posts: 5,396

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

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

  • Posts: 15

    @Ignatz - I bet we do :D 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.

  • edited August 2014 Posts: 1,976

    @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

  • Posts: 15

    @Ignatz - been fighting with it for a while. At a small angle it looks ok, but as the angle gets wider it seems to move off center. Trying to find out what i'm missing :-) if I can't fix it tomorrow i'll share the code allready. It must be one of my inputs because the deviation seems to have an identical ratio, not a random deviation

    @SkyTheCoder - Thanks, Will try tomorrow as well :-)

    Ps: sorry for getting off topic here :p

  • Posts: 1,976

    Okay, I got a working example:

    -- Path
    
    function setup()
        a = 1
        b = vec2(0, 0)
        t = 0
        time = 0
        startPosition = vec2(50, 100)
        endPosition = vec2(200, 300)
        update()
    
        print("Tap in two different spots and see what happens")
    end
    
    function update()
        distance = startPosition:dist(endPosition)
        normal = (endPosition - startPosition):normalize()
    end
    
    function draw()
        background(255)
        stroke(0)
        strokeWidth(5)
        t = t + DeltaTime
        time = t % 1
        local p2 = startPosition + normal * distance * time
        line(WIDTH / 2, HEIGHT / 2, p2.x, p2.y)
    end
    
    function touched(touch)
        a = a % 2
        if touch.state == BEGAN then
            if a == 1 then
                b = vec2(touch.x, touch.y)
            else
                startPosition = vec2(b.x, b.y)
                endPosition = vec2(touch.x, touch.y)
                update()
                t = 0
            end
            a = a + 1
        end
    end
    
    

    Although, after some testing in my efficiency lab (an updated version, I'll upload it soon I guess), it appears that normalizing a vec2 is nearly twice as expensive as using trigonometry. Huh. I wonder why @Andrew_Stacey told me to use it, then.

  • Posts: 473

    @SkyTheCoder In your code, there is no need to use trig or renormalisation. You compute

    distance = startPosition:dist(endPosition)
    normal = (endPosition - startPosition):normalize()
    

    and use these as

    local p2 = startPosition + normal * distance * time
    

    (Minor point: the name "normal" is a bad choice here. Unfortunate conflation of mathematical terminology, but the normal vector is at right angles to the given vector or other object.)

    But normal * distance = endPosition - startPosition so you could simplify your computation to

    deltaPosition = endPosition - startPosition
    

    and

    local p2 = startPosition + time * deltaPosition
    

    Put that in your efficiency lab and see if it improves things.

  • IgnatzIgnatz Mod
    edited August 2014 Posts: 5,396

    @SkyTheCoder, @LoopSpace - I'm not sure the code you're discussing is what the poster wanted. He is not touching two points and interpolating between them.

    He is translating to some x,y, then rotating by some angle a, then moving d pixels forward in that direction. His question is what the new x,y position is now. That's why I suggested a trig solution.

  • IgnatzIgnatz Mod
    Posts: 5,396

    @SkyTheCoder, @LoopSpace - no, you're ok, I'm wrong, ignore my comment

  • Posts: 15

    @SkyTheCoder, @LoopSpace, @Ignatz - thank you for the assistance! I've tried that code this morning and it seems interesting. I'll try and incorporate it into my first project tonight.
    When I look at that I know my code is far from efficient but I'm still learning :) I really should start the project from scratch because my code is beginning to be a mess ... I messed up once very early and didn't rename the variable again, so now i need to refer to "endx" and "endy" as my startingpoint and "startx" and "starty" as my ending point :) just one example of a rookie mistake in my code.

    Maybe I should park the code for now and learn Lua some more ... but not before I get these coordinates right! I will not be defeated by my own code! \m/

  • Posts: 15

    Well, after a few hours of looking at it I'm confident that I made a mistake somewhere at the very base of my script :) using 3 different calculations to find the center of a sprite under the above conditions, I end up with the exact same deviation on all 3 attempts. the bigger to angle, the higher the deviation ...

    Rather than looking at where I made a mistake, i've decided to recreate the app using the new Lua tricks i've learned along the way :) Many thanks for your insightful answers. I've learned a great deal!

Sign In or Register to comment.