Posted: 12th Sep 2011 19:50
Hi Guys, I'm very new to programming and I have been working through Daniel TGC's video tutorial for scribble squash and it has helped me understand a lot about program flow, variables and other basic stuff.

I'm doing a few things myself just to see how I get on without help but because I'm very new to programming I have ran into a few things that I need help understanding, I'm not looking for someone to code things for me, just maybe give me an example or two to study or explain how I would go about doing this. I'm trying to move the ball with a joystick in my program (which I have attached to this post)

All I want it to do is move left, right and jump using the controls in the program.

Please understand that I might not have a clue what your talking about but I will be very greatful with any help you can give me as I'm totally lost and the help files don't really explain it that well for me.
Posted: 12th Sep 2011 20:55
So, you're using physics. You've got a few different ways to move objects around.

1 - Gravity. Probably not a good choice, since gravity is what makes the ball fall down in the first place.

2 - World Force. Again, not a good choice if you just want to move the ball, and not the other objects.

3 - Set the velocity of the ball. Better, but still not the best. The ball will keep whatever velocity you assign it, minus gravity, friction, and damping.

4 - Add Force. This is probably the one you want to use. It gives a one-cycle push to the object. So, move your joystick right and give the ball some positive x force. Keep it to the right, more force is added. Let it return to center, the ball slows down due to gravity, friction, and damping. Move the joystick to the left to give the ball some negative x force. Move the joystick up to add some negative y force.

I'm at work, or I would post some code.

One thing about your main loop, it looks like you're setting many physics parameters every time the pointer is pressed. Most of those should only be set once, so some sort of flag or switch would be good, like

+ Code Snippet
    if GetPointerPressed() = 1 AND startFlag = 0
        startFlag = 1
        etc.
    endif


You have a good start, and I'll be interested to see what you do next!
Posted: 12th Sep 2011 23:15
Thanks Rich I understand what you mean by setting a flag in the main loop but when it comes to the add force way of moving the ball I honestly don't know where to start

I think I'll just have to do a bit of study and see what I can learn.

Thanks very much for the help.
Posted: 13th Sep 2011 16:37
Hi Rich, I have been trying to get the ball to move all day and all I can get it to do is move left and right using the setspriteposition command with the joystick.

If you get the time would you show me an example of how you would move it using the force method you were talking about.

I feel like I'm banging my head against a brick wall here trying to figure it out
Posted: 13th Sep 2011 17:22
Soulstealer - Sure, I'd be glad to help. I can't test code while I'm at work (no AppGameKit here ) but I can whip up something that might work.

I'll post it in a bit...
Posted: 13th Sep 2011 17:28
Thanks Rich I might be offline until tomorrow but I'll check it as soon as I can.

I'm looking forward to seeing how this is done
Posted: 13th Sep 2011 19:40
Here's some code that creates a sort-of "Lunar Lander" type situation. Use the joystick to apply spurts of force to the lander (just a ball for this demo).

This code needs no media.

+ Code Snippet
SetVirtualResolution(640,480)
SetClearColor(155,155,255)
AddVirtualJoystick (1, 50, 430, 100)

groundSpr = CreateSprite(0)
SetSpriteSize(groundSpr, 640, 100)
SetSpritePosition(groundSpr, 0, 380)
SetSpriteShape(groundSpr, 2)
SetSpritePhysicsOn(groundSpr, 1)

hillSpr = CreateSprite(0)
SetSpriteSize(hillSpr, 320, 240)
SetSpritePosition(hillSpr, 320, 300)
SetSpriteAngle(hillSpr, 45.0)
SetSpriteShape(hillSpr, 2)
SetSpritePhysicsOn(hillSpr, 1)

ballImg = LoadImage("JoystickInner.png")
ballSpr = CreateSprite(ballImg)
SetSpritePosition(ballSpr, 320, 0)
SetSpriteOffset(ballSpr, GetSpriteWidth(ballSpr)/2, GetSpriteHeight(ballSpr)/2)
SetSpriteColor(ballSpr, 255, 255, 100, 255)
SetSpriteShape(ballSpr, 1)
SetSpritePhysicsOn(ballSpr, 2)
SetSpritePhysicsRestitution(ballSpr, 0.5)
SetSpritePhysicsFriction(ballSpr, 10.0)

do
    x# = GetVirtualJoystickX(1)
    y# = GetVirtualJoystickY(1)
    Print(str(x#,1)+"/"+str(y#,1))

    if x# > 0.2
        SetSpritePhysicsForce(ballSpr, GetSpriteXByOffset(ballSpr), GetSpriteYByOffset(ballSpr), 20000.0, 0)
    endif

    if x# < -0.2
        SetSpritePhysicsForce(ballSpr, GetSpriteXByOffset(ballSpr), GetSpriteYByOffset(ballSpr), -20000.0, 0)
    endif

    if y# < -0.2
        SetSpritePhysicsForce(ballSpr, GetSpriteXByOffset(ballSpr), GetSpriteYByOffset(ballSpr), 0, -20000)
    endif

    Sync()
loop


EDIT: Soulstealer, I took the basic idea a step further, here.
Posted: 14th Sep 2011 18:30
Wow thanks very much for the code Rich It really helped me understand how to move the ball using PhysicsForce

Now that I understand that I can progress onto setting up a level and trying to use scrolling.

Thanks again and I really like your Lunar Lander code, again it explains a lot of things.