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

Username:   Password: 
 RegisterRegister   
 Moving when something touches it
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
J-MoNeY




PostPosted: Mon Sep 18, 2006 1:10 pm   Post subject: Moving when something touches it

I'm doing an Air Hockey game. So far I have the paddle moving using input keydown and I need help making it so that the puck will move when the paddle collides with the puck.
Sponsor
Sponsor
Sponsor
sponsor
J-MoNeY




PostPosted: Mon Sep 18, 2006 1:12 pm   Post subject: (No subject)

Sorry for double posting but forgot the code

Quote:
%********************************
%name block ICS ICS ICS ICS ICS
%name - James Nguyen
%date - September 18, 2006
%program description - game using input keydown
%*********************************
%
%
%*********************************
%Algorithm
%A set of instructions in order, used to pre-plan a program
%*********************************
%
%
%*********************************
%Variables Dictionary
%declare variable by name and type
var chars : array char of boolean
var x : int := 325 %..................x location of gamepiece
var y : int := 150 %..................y location of gamepiece
var a : int := 0 %....................comp score
var b : int := 0 %....................playa score
var ballx : int := 325 %..............x location of ball
var bally : int := 250 %..............y location of ball
%*********************************
%
%
%*********************************
%Procedures/Subprograms/Subroutines
%do each seperatly in a block

%<><><><><><><><><><><><><><><><><><>THE SCREEN BACKROUND
proc settings
drawfillbox (0, 0, 700, 700, brightblue) %......backround
drawfillbox (270, 0, 400, 20, black) %..........net
drawfillbox (270, 450, 400, 430, black) %.......net
drawfillbox (0, 450, 639, 479, white) %.........top
locate (1, 1) %.................................locates the put
put "Computer score: ", a %.....................shows the score
locate (1, 30) %................................locates the put
put "Player score: ", b %.......................shows the score
end settings

%<><><><><><><><><><><><><><><><><><>THE GAMEPIECE
proc gamepiece
drawfilloval (x, y, 25, 25, red) %.............draws the main gamepiece
drawfilloval (x, y, 10, 10, grey) %............part of the gamepiece
end gamepiece

%<><><><><><><><><><><><><><><><><><>THE BALL
proc ball
drawfilloval (ballx, bally, 15, 15, black) %...draws the ball
end ball

%*********************************
%
%
%*********************************
%Mainline
%step by step enter the program
settings %...................................draws backround

loop %.......................................repeats forever
setscreen ("offscreenonly") %............puts things into memory
settings %...............................puts backround up again
gamepiece %..............................puts gamepiece on screen
ball %...................................puts ball on screen

Input.KeyDown (chars) %..................checks for what characters are touched
if chars (KEY_UP_ARROW) then %...........checks if the player touches up key
delay (2) %..........................slows the game
View.Update %........................puts things into memory
y := y + 1
end if
if chars (KEY_RIGHT_ARROW) then %........checks if player touches right arrow
delay (2) %..........................slows the game
View.Update %........................puts things into memory
x := x + 1
end if
if chars (KEY_LEFT_ARROW) then %.........checks if player touches left key
delay (2) %..........................slows the game
View.Update %........................puts things into memory
x := x - 1
end if
if chars (KEY_DOWN_ARROW) then %.........checks if player touches down key
delay (2) %..........................slows the game
View.Update %........................puts things into memory
y := y - 1
end if

%.......................................prevents game piece from goin left off screen
if x < 30 then %........................checks if gamepiece goes too far left
x := x + 1
end if
%.......................................prevents game piece from going off screen
if y > 420 then %.......................checks if game piece goes too far off screen
y := y - 1
end if
%.......................................prevents game piece from going off screen
if x > 610 then %.......................checks if game piece goes too far off screen
x := x - 1
end if
%.......................................prevents game piece from going off the screen
if y < 30 then %........................checks if game piece goes too far off screen
y := y + 1
end if

end loop
%*********************************
Clayton




PostPosted: Mon Sep 18, 2006 3:04 pm   Post subject: (No subject)

What you are looking for is Collision Detection

also, your commenting could use a little work. you dont need to comment every single line of code you produce, variables should rarely need to be commented as their names should be all you need to tell you what they do. var a,b : int is not going to tell you in their names that what they do is hold the scores for the computer and player respectively. naming for variables also applies to procedures as well, the procedure "settings" sounds to me like it should control settings of the game, instead it draws the background Confused

also:

Turing:

if chars (KEY_UP_ARROW) then %...........checks if the player touches up key
        delay (2) %..........................slows the game
        View.Update %........................puts things into memory
        y := y + 1
    end if
    if chars (KEY_RIGHT_ARROW) then %........checks if player touches right arrow
        delay (2) %..........................slows the game
        View.Update %........................puts things into memory
        x := x + 1
    end if
    if chars (KEY_LEFT_ARROW) then %.........checks if player touches left key
        delay (2) %..........................slows the game
        View.Update %........................puts things into memory
        x := x - 1
    end if
    if chars (KEY_DOWN_ARROW) then %.........checks if player touches down key
        delay (2) %..........................slows the game
        View.Update %........................puts things into memory
        y := y - 1


would be better as a single if construct using the elsif keyword instead of separate ifs. the reason for this is, if you hold down both the up and down arrow keys at the same time, you wont move! because "chars" is an [/b]array char of boolean[/b] (meaning it is an array with the number of elements being equal to the number of accessible characters with the type being boolean) you can have more than one char being pressed and Input.KeyDown will register all of them. Check out this tutorial if you need help on anything, as well, check out the entire Turing Walkthrough if you want to find out anything more.

One last thing, remember to post code within [code ][/code ] or [syntax=][/syntax ] tags (without the spaces) as it allows your code to keep its indentation on the site Wink
J-MoNeY




PostPosted: Mon Sep 18, 2006 4:42 pm   Post subject: (No subject)

Thanks Freakman. I have another question. When I run my program and I click off the screen everything but the backround and score disapears until I touch any of the moving keys.
Clayton




PostPosted: Mon Sep 18, 2006 4:48 pm   Post subject: (No subject)

its just because you catch it whenever the screen buffer is cleared (due to the effects of Draw.Cls) which you should consequentially read about here as your commenting about it suggests that you dont really understand whats going on.
J-MoNeY




PostPosted: Mon Sep 18, 2006 5:48 pm   Post subject: (No subject)

Ok...heres what my code looks like now. Its really messed up.

code:

%********************************
%name block    ICS    ICS     ICS     ICS     ICS
%name - James Nguyen
%date - September 18, 2006
%program description - game using input keydown
%*********************************
%
%
%*********************************
%Algorithm
%A set of instructions in order, used to pre-plan a program
%*********************************
%
%
%*********************************
%Variables Dictionary
%declare variable by name and type
var chars : array char of boolean
var x : int := 325 %..................x location of gamepiece
var y : int := 150 %..................y location of gamepiece
var compscore : int := 0 %............comp score
var playerscore : int := 0 %..........playa score
var ballx : int := 325 %..............x location of ball
var bally : int := 250 %..............y location of ball
var ballr : int := 15 %...............radius of ball
var radius : int := 25 %..............radius of the paddle
var nx, ny, nx2, ny2 : int := 1 %.....vars for the chage in qroadents
%*********************************
%
%
%*********************************
%Procedures/Subprograms/Subroutines
%do each seperatly in a block

%<><><><><><><><><><><><><><><><><><>THE SCREEN BACKROUND
proc backround
    drawfillbox (0, 0, 700, 700, brightblue) %......backround
    drawfillbox (270, 0, 400, 20, black) %..........net
    drawfillbox (270, 450, 400, 430, black) %.......net
    drawfillbox (0, 450, 639, 479, white) %.........top
    locate (1, 1) %.................................locates the put
    put "Computer score: ", compscore .. %..........shows the score
    locate (1, 30) %................................locates the put
    put "Player score: ", playerscore .. %..........shows the score
end backround

%<><><><><><><><><><><><><><><><><><>THE GAMEPIECE
proc gamepiece
    drawfilloval (x, y, 25, radius, red) %..........draws the main gamepiece
    drawfilloval (x, y, 10, 10, grey) %.............part of the gamepiece
end gamepiece

%<><><><><><><><><><><><><><><><><><>THE BALL
proc ball
    drawfilloval (ballx, bally, ballr, ballr, black) %...draws the ball
end ball

%*********************************
%
%
%*********************************
%Mainline
%step by step enter the program
backround %..................................draws backround
gamepiece %..................................draws gamepiece
ball %.......................................draws ball

loop %.......................................repeats forever
    setscreen ("offscreenonly") %............puts things into memory
    backround %..............................puts backround up again
    gamepiece %..............................puts gamepiece on screen
    ball %...................................puts ball on screen

    Input.KeyDown (chars) %..................checks for what characters are touched
    if chars (KEY_UP_ARROW) then %...........checks if the player touches up key
        delay (2) %..........................slows the game
        View.Update %........................puts things into memory
        y := y + 1

    elsif chars (KEY_RIGHT_ARROW) then %.....checks if player touches right arrow
        delay (2) %..........................slows the game
        View.Update %........................puts things into memory
        x := x + 1

    elsif chars (KEY_LEFT_ARROW) then %......checks if player touches left key
        delay (2) %..........................slows the game
        View.Update %........................puts things into memory
        x := x - 1

    elsif chars (KEY_DOWN_ARROW) then %......checks if player touches down key
        delay (2) %..........................slows the game
        View.Update %........................puts things into memory
        y := y - 1
    end if

    if ballx + ballr > x - radius and ballx - ballr < x + radius and bally + ballr > y - radius and bally - ballr < y + radius then
        nx := nx * -1
        ny := ny * -1
        nx2 := nx2 * -1
        ny2 := ny2 * -1
    end if

    x += nx
    y += ny
    ballx += nx2
    bally += ny2
    delay (15)

    %........................................prevents game piece from goin left off screen
    if x < 30 then %.........................checks if gamepiece goes too far left
        x := x + 1
    end if
    %........................................prevents game piece from going off screen
    if y > 420 then %........................checks if game piece goes too far off screen
        y := y - 1
    end if
    %........................................prevents game piece from going off screen
    if x > 610 then %........................checks if game piece goes too far off screen
        x := x - 1
    end if
    %........................................prevents game piece from going off the screen
    if y < 30 then %.........................checks if game piece goes too far off screen
        y := y + 1
    end if

end loop
%*********************************
Clayton




PostPosted: Mon Sep 18, 2006 6:54 pm   Post subject: (No subject)

this is gonna require some good knowledge of trig to pull off and make it look half decent, not to say give up on it completely, but maybe start with something a bit less complex for now, unless you want to figure out all of the trig etc, i dont think you are quite up to this task right now

for your collision detection though, as both of your objects that are colliding are circles, use Math.Distance (x1, y1, x2, y2 : int), which uses the distance formula to calculate the distance between two points (x1,y1) (x2,y2).
J-MoNeY




PostPosted: Mon Sep 18, 2006 8:28 pm   Post subject: (No subject)

We haven't learned Math.Distance yet in our class and our teacher isn't very smart, so could you explain how I would use Math.Distance for my program?
Sponsor
Sponsor
Sponsor
sponsor
NikG




PostPosted: Mon Sep 18, 2006 11:17 pm   Post subject: (No subject)

All you need to do is search this site!
But I did it for you: Math.Distance tutorial

For future reference, check out the Turing Walkthrough page before asking for help on something. I'm sure it'll cover most of the questions you'll have.
J-MoNeY




PostPosted: Tue Sep 19, 2006 11:33 am   Post subject: (No subject)

I've read that tutorial but I'm still not sure how to use it in my program.
TheOneTrueGod




PostPosted: Tue Sep 19, 2006 12:29 pm   Post subject: (No subject)

If the distance between two objects is less than the combined radius, then they have collided.

code:

     ob2Rad
        V
ob1     |   |     ob2
            ^
          ob1Rad


omg they have collided

Then you just apply trig ratios. You can get the angle between the two by using Math.Angle (Though I prefer my function, as it proves that I actually know what i'm doing, as opposed to proving that the people who created Turing knew what they were doing Razz).

Use this angle, and the paddle's speed (if you are using the mouse) to calculate how much force you should apply to the puck. I suggest you use the mouse, only because it allows you to hit the puck based on how hard you move the mouse... Though you would have to do some extra checks to see if you actually hit the puck or not.
[Gandalf]




PostPosted: Tue Sep 19, 2006 3:04 pm   Post subject: (No subject)

TheOneTrueGod wrote:
You can get the angle between the two by using Math.Angle (Though I prefer my function, as it proves that I actually know what i'm doing, as opposed to proving that the people who created Turing knew what they were doing Razz).

Incidentally, Math.Angle is your function. Razz

Math.Angle is not a function that comes with any official version of Turing, so either you created it and added it in, or someone else created it and you added it in. Smile
TheOneTrueGod




PostPosted: Tue Sep 19, 2006 3:33 pm   Post subject: (No subject)

Quote:
Incidentally, Math.Angle is your function.


Incidentally, yes it is, however, I wrote it before coming on this site, and before seeing any other implementations of it, so i'm proud of my work Razz

Anyways, since math.Angle apparantly doesn't come with Turing, i'll give you a l'il tutorial, but this is meant to be followed up by classwork...

[syntax="ASCII Art Involved"]
Note: FOR RIGHT TRIANGLES ONLY.
Hypoteneuse
|| /|
|V/ |Opposite
|/o |
-------------
|Adjacent
|
|

o = "theta" (Its an angle. The others are the sides of the triangle)

O = Opposite (Called thus because it is OPPOSITE theta)
A = Adjacent (Called thus because it is ADJACENT to theta, and it isn't the hypoteneuse)
H = Hypoteneuse (Called thus because some guy thought it would be cool to give the longest side the longest name Razz)

SOH CAH TOA
Sin(theta) = O/H
Cos(theta) = A/H
tan(theta) = O/A

Now, you can apply these concepts something like this

[calculator notation]
tan^-1(O/A) = theta

[turing notation]
arctand(O/A)

Where "arctan" is what tan^-1 is called, and "d" tells it you are working in degrees (And "arctan(O/A)" would be in

radians)

Remember, when A = 0, this is a special case.
[/syntax]

Hopefully that helps... If there isn't a tutorial on this stuff allready, I could try turning this into one if necessary...
J-MoNeY




PostPosted: Tue Sep 19, 2006 3:51 pm   Post subject: (No subject)

I think I've got the collision detection working, but I can only get the puck moving in 1 direction (the left) and when it moves its moving too fast for me to see so its like disapering to the left everytime my paddle collides with it and when i hit the puck to the far left my ifs don't stop the puck from going off the screen and bouncing off somewhere else. I'm using input keydown because this is our first assignment and our teacher wants it to be a game using keydown only.

code:

%********************************
%name block    ICS    ICS     ICS     ICS     ICS
%name - James Nguyen
%date - September 18, 2006
%program description - air hockey game using input keydown
%*********************************
%
%
%*********************************
%Algorithm
%A set of instructions in order, used to pre-plan a program
%*********************************
%
%
%*********************************
%Variables Dictionary
%declare variable by name and type
var chars : array char of boolean
var x1 : int := 325 %..................x location of gamepiece
var y1 : int := 150 %..................y location of gamepiece
var compscore : int := 0 %.............comp score
var playerscore : int := 0 %...........playa score
var x2 : int := 325 %..................x location of ball
var y2 : int := 250 %..................y location of ball
var r2 : int := 15 %...................radius of ball
var r : int := 25 %....................radius of the paddle
var xchange, ychange : int := 1
var xchange2, ychange2 : int := 200
%*********************************
%
%
%*********************************
%Procedures/Subprograms/Subroutines
%do each seperatly in a block

%<><><><><><><><><><><><><><><><><><>THE SCREEN BACKROUND
proc backround
    drawfillbox (0, 0, 700, 700, brightblue) %......backround
    drawfillbox (270, 0, 400, 20, black) %..........net
    drawfillbox (270, 450, 400, 430, black) %.......net
    drawfillbox (0, 450, 639, 479, white) %.........top
    locate (1, 1) %.................................locates the put
    put "Computer score: ", compscore .. %..........shows the score
    locate (1, 30) %................................locates the put
    put "Player score: ", playerscore .. %..........shows the score
    drawfilloval (600, 250, 25, 25, grey) %.........trap1
    drawfilloval (35, 250, 25, 25, grey) %..........trap2
end backround

%<><><><><><><><><><><><><><><><><><>GAMEPIECE OF THE PADDLE
proc gamepiece
    drawfilloval (x1, y1, r, r, red) %..............draws the main part of game piece
    drawfilloval (x1, y1, 20, 20, yellow) %.........draws the handle of the paddle'
    drawfilloval (x1, y1, 18, 18, brightblue) %.....draws the handle of the paddle'
    drawfilloval (x1, y1, 15, 15, brightred)    %...draws the handle of the paddle'
    drawfilloval (x1, y1, 12, 12, grey)        %....draws the handle of the paddle'
    drawfilloval (x1, y1, 10, 10, 6)        %.......draws the handle of the paddle'
    drawfilloval (x1, y1, 8, 8, 10)            %....draws the handle of the paddle'
    drawfilloval (x1, y1, 6, 6, 13)             %...draws the handle of the paddle'
end gamepiece

%<><><><><><><><><><><><><><><><><><>THE BALL
proc puck
    drawfilloval (x2, y2, r, r, black) %..........draws the ball
end puck

%*********************************
%
%
%*********************************
%Mainline
%step by step enter the program
backround %..................................draws backround
gamepiece %..................................draws gamepiece
puck %.......................................draws ball

loop %.......................................repeats forever
    setscreen ("offscreenonly") %............puts things into memory
    backround %..............................puts backround up again
    gamepiece %..............................puts gamepiece on screen
    puck %...................................puts ball on screen

    Input.KeyDown (chars) %..................checks for what characters are touched
    if chars (KEY_UP_ARROW) then %...........checks if the player touches up key
        delay (2) %..........................slows the game
        View.Update %........................puts things into memory
        y1 := y1 + 1

    elsif chars (KEY_RIGHT_ARROW) then %.....checks if player touches right arrow
        delay (2) %..........................slows the game
        View.Update %........................puts things into memory
        x1 := x1 + 1

    elsif chars (KEY_LEFT_ARROW) then %......checks if player touches left key
        delay (2) %..........................slows the game
        View.Update %........................puts things into memory
        x1 := x1 - 1

    elsif chars (KEY_DOWN_ARROW) then %......checks if player touches down key
        delay (2) %..........................slows the game
        View.Update %........................puts things into memory
        y1 := y1 - 1
    end if

    if Math.Distance (x1, y1, x2, y2) < r * 2 then
        x1 += xchange
        x2 -= xchange2
    end if

    %........................................prevents game piece from goin left off screen
    if x1 < 30 then %........................checks if gamepiece goes too far left
        x1 := x1 + 1
    end if
    %........................................prevents game piece from going off screen
    if y1 > 420 then %.......................checks if game piece goes too far off screen
        y1 := y1 - 1
    end if
    %........................................prevents game piece from going off screen
    if x1 > 610 then %.......................checks if game piece goes too far off screen
        x1 := x1 - 1
    end if
    %........................................prevents game piece from going off the screen
    if y1 < 30 then %........................checks if game piece goes too far off screen
        y1 := y1 + 1
    end if

    %........................................prevents game piece from goin left off screen
    if x2 < 30 then %.........................checks if gamepiece goes too far left
        x2 := x2 + 1
    end if
    %........................................prevents game piece from going off screen
    if y2 > 420 then %.......................checks if game piece goes too far off screen
        y2 := y2 - 1
    end if
    %........................................prevents game piece from going off screen
    if x2 > 610 then %.......................checks if game piece goes too far off screen
        x2 := x2 - 1
    end if
    %........................................prevents game piece from going off the screen
    if y2 < 30 then %........................checks if game piece goes too far off screen
        y2 := y2 + 1
    end if

end loop
%*********************************
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: