Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Trigonometry Help, Hockey Game, Shooting
Author Message
@DRI@N

Posted: Wed May 26, 2004 9:08 pm   Post subject: Trigonometry Help, Hockey Game, Shooting

Can anyone spot what's wrong here because I can't see it. YOu press shift to shoot. Works alright from pretty close but if u shoot from far only the xchange increases. What I did was find the angle using Tan(opp/adj) and then multiplied my angle by the movement(2) and divided it by 90 to get x velocity. For Y i did the same but did (90-angle)...please help...
 code: setscreen("graphics:800;800") View.Set("offscreenonly")      colorback(black) cls     var crowd_colour:int         var x1_crowd: int     var y1_crowd: int         var counter:int:=0     var arena_x : array 1..8 of int := init (200, 200, 250, 550,                                    600, 600, 550, 250)     var arena_y : array 1..8 of int := init (150, 650, 700, 700,                                    650, 150, 100, 100)     var Score_font:int:=Font.New ("Garamond:30")         var score:int:=0     var player_score:string:=""     var puckx,pucky:int:=400     var puckychange,puckxchange:int:=1     var RD_X:int:=500     var RD_Y:int:=200     var chars : array char of boolean           var cur_x,cur_y:int     const movement:=2     procedure Arena loop counter:=counter+1 randint(crowd_colour,1,255) randint(x1_crowd,1,800) randint(y1_crowd,1,800) drawfilloval(x1_crowd,y1_crowd,2,2,crowd_colour) exit when counter=30000 end loop end Arena function X_Velocity_Calculate(puckx:int,pucky:int):real var xvelocity, yvelocity : real var angle : real var Opposite : int var Adjacent : int Adjacent := 400-puckx Opposite := 620-pucky if Opposite = 0 then        angle := 0 else     angle := arctand (Opposite/Adjacent) end if xvelocity := angle * movement / 90 result xvelocity end X_Velocity_Calculate function Y_Velocity_Calculate(puckx:int,pucky:int):real var xvelocity, yvelocity : real var angle : real var Opposite : int var Adjacent : int Adjacent := 400-puckx Opposite := 620-pucky if Opposite = 0 then        angle := 0 else     angle := arctand (Opposite/Adjacent) end if yvelocity := (90-angle)* movement / 90 result yvelocity end Y_Velocity_Calculate function Collision(x:real,y:real,xwall1:int,xwall2:int,ywall1:int,ywall2:int):boolean if x < xwall2 and x >xwall1 and y > ywall2 and y =puckx then puckxchange:=puckxchange*-1 puckychange:=puckychange*-1 end if if puckx>550 and pucky>650 and (1250-pucky)<=puckx then puckxchange:=puckxchange*-1 puckychange:=puckychange*-1 end if if puckx<250 and pucky<150 and (350-pucky)>=puckx then puckxchange:=puckxchange*-1 puckychange:=puckychange*-1 end if if puckx>550 and pucky<150 and (pucky+450)<=puckx then puckxchange:=puckxchange*-1 puckychange:=puckychange*-1 end if %%%%Player Corners if (RD_Y+2-450)>=RD_X-30 then RD_Y:=RD_Y-10 RD_X:=RD_X+10 end if if RD_X+52>550 and RD_Y+32>650 and (1250-RD_Y+32)<=RD_X+52 then RD_Y:=RD_Y-10 RD_X:=RD_X-10 end if if RD_X-40<250 and RD_Y-16<150 and (350-RD_Y-16)>=RD_X-40 then RD_Y:=RD_Y+10 RD_X:=RD_X+10 end if if RD_X+6>550 and RD_Y-11<150 and (RD_Y-11+450)<=RD_X+6 then RD_Y:=RD_Y+10 RD_X:=RD_X-10 end if %%%Collison if Collision(puckx,pucky,RD_X-34,RD_X+18,RD_Y+16,RD_Y-15)=true then puckx:=RD_X-25 pucky:=RD_Y-2 end if if chars(KEY_ENTER)and puckx < RD_X+18 and puckx >RD_X-34 and pucky > RD_Y-15 and pucky 380 and cur_x <420 and cur_y >600 and cur_y <640 and puckx not=RD_X-25 and pucky not=RD_Y-2  then score:=score+1 puckx:=400 pucky:=400 puckxchange:=1 RD_X:=500 RD_Y:=200 player_score:=intstr(score) end if %%% View.Update end loop end loop

@DRI@N

Posted: Wed May 26, 2004 9:16 pm   Post subject: (No subject)

I just noticed too....it shoots faster from the right side of the screen and.. sometimes doesn't bounce off the walls..can anyone tell me what the fuck is going on????
Tony

Posted: Wed May 26, 2004 10:01 pm   Post subject: (No subject)

no no no

lets go back to our grade 10 trig class.

you got the angle... good

 code: velocityX := totalVelocity * cosd(angle) velocityY := totalVelocity * sind(angle)

where totalVelocity is the absolute speed of the puck. It could be a static value or depend on player's stength or w/e variable for more depth
Tony's programming blog. DWITE - a programming contest.
guruguru

Posted: Wed May 26, 2004 10:17 pm   Post subject: (No subject)

I want to argue with you Tony... I believe his method is right... but you're the expert so I'm gonna drop it .
Tony

Posted: Wed May 26, 2004 10:22 pm   Post subject: (No subject)

no, plz argue with me, I'd like to see what a 15 year old has to teach me about trig functions
Tony's programming blog. DWITE - a programming contest.
guruguru

Posted: Thu May 27, 2004 4:04 pm   Post subject: (No subject)

EDIT EDIT EDIT: I am wrong!!! I was thinking of totalVelocity as the sum of the x and y velocities!!! Tony brought that to my attention in the other (identical :p) post. Tony wins again . You can read below for interest but dun think about implementing it because its not right lol...

Gladly! I like arguing math (even if I (am) have a chance being wrong).

Ok. He has the slope. And he gets the angle. We gots that . But your way does not work.

Lets say that the angle is 45, and totalVelocity 10. Hence, both the x and y velocities should be equal and have a sum of totalVelocity. Well, lets see if the methods work..

 code: Your method: velocityX := totalVelocity * cosd(angle) velocityX := 10 * cosd(45) velocityX := 7.07 velocityY is the same obviously. But, 7.07 * 2 does not equal 10!!! Sure it gives the right ration, but they dont add up to 10! My method: velocityX := (90 - angle) * totalVelocity / 90 velocityX := 45 * 10 / 90 velcotiyX := 5 velocityY is the same. And 5 + 5 = 10 :D. Mine both makes the numbers equal, and scales them down. Another example. Angle = 30. totalVelocity is 10. Tony's: velocityX := 8.66 velocityY := 5 They dont add up to 10... and they're not in the right ratio. Mine: velocityX := 6.66 velocityY := 3.33 They add to 10, and are in the right ratio.

Please counter-argue!!! This is getting fun fun fun !!!

Note: I switched the x and y velcotiy formulas in @dri@ans (and mine indirectly), because they were messed up and I wasn't thinking fully right when I made them.
@DRI@N

Posted: Thu May 27, 2004 4:22 pm   Post subject: (No subject)

So the "movement" varialbe I had shood be 10? and then all i need to change is the (90-x) part? sorry I'm kinda confused because I never used "Total Velocity" in my program
guruguru

Posted: Thu May 27, 2004 4:25 pm   Post subject: (No subject)

No no no... you must use Tony's version.

Quote:

velocityX := totalVelocity * cosd(angle)

velocityY := totalVelocity * sind(angle)

totalVelocity is movement in your program.

BTW Tony: We take trig in grade 9.

@DRI@N

Posted: Thu May 27, 2004 4:30 pm   Post subject: (No subject)

Something is still wrong I just tried it...its slower from some places and always shoots 45 degrees, NOT at the net
@DRI@N

Posted: Thu May 27, 2004 4:37 pm   Post subject: (No subject)

Here's what I did...for the functions. It works from some places..but from the right side of the screen the shots are backwards. When it hits the boards it should slow down again to xchange=1 ychange=1 but instead it flies off at the same speed as the shot.
 code: function X_Velocity_Calculate(puckx:int,pucky:int):real var xvelocity, yvelocity : real var angle : real var Opposite : int var Adjacent : int Adjacent := 400-puckx Opposite := 620-pucky if Opposite = 0 then        angle := 0 else     angle := arctand (Opposite/Adjacent) end if xvelocity := movement*cosd(angle) result xvelocity end X_Velocity_Calculate function Y_Velocity_Calculate(puckx:int,pucky:int):real var xvelocity, yvelocity : real var angle : real var Opposite : int var Adjacent : int Adjacent := 400-puckx Opposite := 620-pucky if Opposite = 0 then        angle := 0 else     angle := arctand (Opposite/Adjacent) end if yvelocity := movement*sind(angle) result yvelocity end Y_Velocity_Calculate

As well....I have my if statements for Collision to know when he gets the puck, and..he cant shoot because of that because the puck is within the collision zone when im trying to shoot. How do I overcome these 3 problems...
 code: function Collision(x:real,y:real,xwall1:int,xwall2:int,ywall1:int,ywall2:int):boolean if x < xwall2 and x >xwall1 and y > ywall2 and y
guruguru

Posted: Thu May 27, 2004 4:47 pm   Post subject: (No subject)

Functions look ok... you should have the variables declared globally, not in the functions. And you should have one function for both values. Simple make a test for the direction and then make negative/positive the x/y velcoityes based on that test.

It probally shoots at 45 degrees because you have local and global puck values. Make them just global.

What are the x and y values? The player? or puck? And what is the collission zone? I'm confused about what you are asking.
@DRI@N

Posted: Thu May 27, 2004 4:56 pm   Post subject: (No subject)

Forget the shooting I think I fixed it...Ok collisions...I have if statements so when the puck goes withing this "invisible" rectangle(basically right around the player) to move to his stick. Otherwise it will just go by him and I want him to be able to have the puck right? Well when I want to take a shot...the puck is on his stick....and it cannot exit the "invisible" box because it is always being told to return it to his stick
guruguru

Posted: Thu May 27, 2004 5:11 pm   Post subject: (No subject)

Mkae a boolean variable onStick. When onStick is false, the ball goes to the stick. And then onStick := true when it reaches the stick.

When it is true, then the 'go on stick' procedure is not called and just the shoot procedure is called. Then onStick := false after shot is made.
@DRI@N

Posted: Thu May 27, 2004 5:16 pm   Post subject: (No subject)

K ill try that in a minute...the damn shooting from the right side just goes down and through the walls too
guruguru

Posted: Thu May 27, 2004 5:36 pm   Post subject: (No subject)

Make sure the collision detection has no little faults. Make sure you < and > are not switched, and that you are testing the right coordinates. As well, like I said before, check the directin of the current velocity and adjust it so that it goes up/dow/left/right.
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 2  [ 22 Posts ]
Goto page 1, 2  Next
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: