Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How to move an object properly
Index -> Programming, Turing -> Turing Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Drakain Zeil




PostPosted: Wed Jan 26, 2005 2:50 pm   Post subject: How to move an object properly

All I have to do is walk around my classroom to see people moving things around in games, like pong for example.

However, the way they do it, the objects bounce around, and give room for errors/bugs, and by bug, I mean bug me for help.

One important thing to remember is equal and opposites. If you are pressing down on a desk with let's say... 3 pounds, that desk is also pressing against it withe the exact same force. Thus, the object stays in that place until a force moves it in another direction (or it's force surpasses what the desk can hold, thus breaking the desk... or by some weird fluke, the desk's force increases and your hand pressing down on the desk launches back at you [however, this breaks the law]).

Why does this matter? Well, lets say you're walking at a speed of 2.

You begin to walk towards a wall, and in turing you can write this as:
code:
position +=2

Now, what happens when the wall hits you (or you hit the wall, both happen in physics [equal and opposites])? You stop (and perhaps decide not to do it again). The wall has NOT moved you back, it has stopped you from moving, you can continue to move at speed 2 (because you decided that nothing is going to stand in your way), but you will never get past the wall.

This is not moving you back, as such...
code:
position -=2


This is preventing you from moving forward when you are at the wall.
code:
position +=0

or even
code:
%Nothing


How do you put this into a program? Well okay I'll take you through it.

Let's say you have a character called cross, and cross cannot ever get inside of a black box! (DUN Dun DUN!) Yes I'm not much for plots, get over it.

Now lets say you stuck together a sloppy program that just moves cross around (I'm not explaining it, but I did try to make it simple to understand).

code:
View.Set ("graphics:350;400,nobuttonbar,offscreenonly")

var rx1, ry1, rx2, ry2 : int
var horizontal, vertical : int := 25
var keyinput : string (1):="x"

rx1 := Rand.Int (50, 300)
ry1 := Rand.Int (50, 300)
rx2 := Rand.Int (50, 300)
ry2 := Rand.Int (50, 300)

Draw.FillBox (rx1, ry1, rx2, ry2, black)

loop
    Draw.FillBox (rx1, ry1, rx2, ry2, black)
    Draw.Line (horizontal + 15, vertical, horizontal - 15, vertical, red)
    Draw.Line (horizontal, vertical + 15, horizontal, vertical - 15, red)

    locate (1, 1)
    if hasch then
        getch (keyinput)
    end if
    if keyinput = chr (200) then %up arrow
        vertical += 5
        keyinput :="x"
    elsif keyinput = chr (205) then %right arrow
        horizontal += 5
        keyinput :="x"
    elsif keyinput = chr (203) then    %left arrow
        horizontal -= 5
        keyinput :="x"
    elsif keyinput = chr (208) then %down arrow
        vertical -= 5
        keyinput :="x"
    end if

    View.Update
    cls
    exit when keyinput = chr (10)
end loop


That's great, it goes up and down when you press the keys, it doesn't do it well, but it sure does do it, oh boy! (If you're making a game, you will proabably want to use Input.KeyDown, but I didn't feel like it Smile
And yes, this does tend to bog your computer down a tad),

Now what you ask?

Make checks inside of your if statements, check to see that if the key down has been pressed and anything in front becomes part of the box.

Well, we have two options, one... check to see if the movement will take it between the bounds of the box... or we can use whatdotcolor. I'm going to use whatdotcolor.

Option 1) (what I'm not going to do, because I just wrote my math exam, no thanks)
So, if the key is pressed, and is NOT going to be (position + increment) at the border of the box (between rx1 OR rx2), then don't let it move in that direction.

Option 2) (what I'm going to do)
whatdotcolor, it returns the colour of a specific pixel. How wonderful.
So we check to see if the pixel 5 up from where we are is black (I used up for this example), and if it is, we just let it sit there and do nothing.
A boolean statement as such should do the trick

code:
whatdotcolour (horizontal, vertical + 5) not= black


Now we apply that logic to all of movement blocks.

Now you have this... And it works! No inter-mediate calculating done, the object either moves forward when you move it forward, or not.

My friend did his game in the way I said not to, and he ran into a forest of problems, and "fixed" then with many, many IFs (about 5 lines of code per bound, and he couldn't have blocks above the character or it was 'teleported' above them because of a flaw).

Anyway, here's the code for the whatdotcolour version.

code:
View.Set ("graphics:350;400,nobuttonbar,offscreenonly")

var rx1, ry1, rx2, ry2 : int
var horizontal, vertical : int := 25
var keyinput : string (1) := "x"

rx1 := 40
ry1 := 30
rx2 := 200
ry2 := 100



loop
    Draw.Line (horizontal + 15, vertical, horizontal - 15, vertical, red)
    Draw.Line (horizontal, vertical + 15, horizontal, vertical - 15, red)
    Draw.FillBox (rx1, ry1, rx2, ry2, black)
    locate (1, 1)
    if hasch then
        getch (keyinput)
        if keyinput = chr (200) then %up arrow
            if whatdotcolour (horizontal, vertical + 5) not= black then
                vertical += 5
            end if
        elsif keyinput = chr (205) then %right arrow
            if whatdotcolour (horizontal +5, vertical) not= black then
            horizontal += 5
            end if
           
         elsif keyinput = chr (203) then %left arrow
            if whatdotcolour (horizontal-5, vertical) not= black then
            horizontal -= 5
            end if
           
        elsif keyinput = chr (208) then %down arrow
            if whatdotcolour (horizontal, vertical -5) not= black then
            vertical -= 5
            end if
        end if
    end if
    View.Update
    cls
    exit when keyinput = chr (10)
end loop


Thanks for reading.
Sponsor
Sponsor
Sponsor
sponsor
cool dude




PostPosted: Wed Jan 26, 2005 3:08 pm   Post subject: (No subject)

good tutorial for beginners but not very efficient. if your making a game i recommend not using this code because its very buggy, slow and insufficient.
Cervantes




PostPosted: Thu Jan 27, 2005 8:48 pm   Post subject: (No subject)

Don't forget to use Input.KeyDown. Also, your movement if statements should be if .. end if instead of if .. elsif .. end if. If you use elsifs, holding down the right and left arrow keys will move you one of those directions, when you really don't want to move in either direction (that is, you'll move left then right [or right then left] and movement will cancle each other out, so you won't move).
FakeSound




PostPosted: Sat May 06, 2006 9:31 am   Post subject: (No subject)

sorry to revive a dead topic .. I've been looking for some help with making a game for a little while; I haven't looked through all of the pages in the tutorial (I don't have that kind of time) but I was hoping maybe somebody could help me out, or maybe redirect me to a place where I can get some help.

My friend and I are trying to make a simple game of pong... We're stuck on making the paddles. I can make one on the left side of the screen that moves up and down using w and s, and I can make one on the right side of the screen that moves up and down using the up and down arrows, but I can't make both.

This is the code I have so far ... If you take out the first paddle, the second one appears, but if you leave them both there, the second one doesn't come up.

code:
setscreen ("graphics")
var x, y, x1, y1, a, b, a1, b1 : int
x := 10
y := 10
x1 := 20
y1 := 100
var chars : array char of boolean
loop
    Input.KeyDown (chars)

    if chars ('w') then
        y := y + 5
        y1 := y1 + 5
    end if
    if chars ('s') then
        y := y - 5
        y1 := y1 - 5
    end if

    drawbox (x, y, x1, y1, blue)
    delay (10)
    cls
end loop

a := 10
b := 10
a1 := 20
b1 := 100

loop
    Input.KeyDown (chars)

    if chars (KEY_UP_ARROW) then
        b := b + 5
        b1 := b1 + 5
    end if
    if chars (KEY_DOWN_ARROW) then
        b := b - 5
        b1 := b1 - 5
    end if

    drawbox (a + 600, b, a1 + 600, b1, red)
    delay (10)
    cls


end loop


can someone please help me, or at least point me in the right direction?
thanks in advance
FakeSound




PostPosted: Sat May 06, 2006 9:40 am   Post subject: (No subject)

scratch that, i figured it out Very Happy
code:
setscreen ("graphics")
var x, y, x1, y1, a, b, a1, b1 : int
x := 10
y := 10
x1 := 20
y1 := 100
a := 10
b := 10
a1 := 20
b1 := 100
var chars : array char of boolean
loop
    Input.KeyDown (chars)

    if chars ('w') then
        y := y + 5
        y1 := y1 + 5
    end if
    if chars ('s') then
        y := y - 5
        y1 := y1 - 5
    end if

    drawbox (x, y, x1, y1, blue)
    drawbox (a + 600, b, a1 + 600, b1, red)
    delay (10)
    cls


    Input.KeyDown (chars)

    if chars (KEY_UP_ARROW) then
        b := b + 5
        b1 := b1 + 5
    end if
    if chars (KEY_DOWN_ARROW) then
        b := b - 5
        b1 := b1 - 5
    end if

   
    delay (10)
    cls


end loop
BigBear




PostPosted: Sun Mar 02, 2008 10:13 am   Post subject: Re: How to move an object properly

I am reviving this because my question is relevant and I didn't want to make another topic. Anyways I was wondering how to move something on angles. I remember those SNES games that had there characters moving diagonally and fluently and others that were OK and moved up, down, left, and right.
OneOffDriveByPoster




PostPosted: Sun Mar 02, 2008 12:29 pm   Post subject: Re: How to move an object properly

BigBear @ Sun Mar 02, 2008 10:13 am wrote:
I am reviving this because my question is relevant and I didn't want to make another topic. Anyways I was wondering how to move something on angles. I remember those SNES games that had there characters moving diagonally and fluently and others that were OK and moved up, down, left, and right.
If you mean diagonally in a straight line, then just take endpoint - startpoint (this gets you a vector) and divide each component (x and y for 2-D) by how many frames you want in between.
BigBear




PostPosted: Sun Mar 02, 2008 3:56 pm   Post subject: Re: How to move an object properly

I am confused.
code:
var ans1, ans2 : string (1)
var x, y :int:=0
loop
cls
drawfillbox(100+x, 100+y, 200+x, 200+y, 65)
getch(ans1)
getch(ans2)
if ans1 = chr(200) and ans2 = chr(205) or ans1= chr(205) and ans2 = chr(200)then
x+=5
y+=5
drawfillbox(100+x, 100+y, 200+x, 200+y, 65)
end if
end loop
If you do it like that how do u know when they want to go diagonally.
Sponsor
Sponsor
Sponsor
sponsor
richcash




PostPosted: Sun Mar 02, 2008 4:17 pm   Post subject: Re: How to move an object properly

You should use Input.KeyDown to detect multiple keys simultaneously.
Clayton




PostPosted: Sun Mar 02, 2008 4:18 pm   Post subject: RE:How to move an object properly

alternatively, you can also use trig, not necessarily easier or harder, just a different way of doing things, and one you are most likely more comfortable with.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: