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

Username:   Password: 
 RegisterRegister   
 SLOPE PROBLEM!
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
@DRI@N




PostPosted: Tue May 25, 2004 9:31 pm   Post subject: SLOPE PROBLEM!

I made a function to calculate slope and shoot on the net..press shift to shoot, but Smile something isn't working. The friggin puck disappears right away when u press shift....
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:real:=400
    var puckychange,puckxchange:real:=1
    var RD_X:int:=500
    var RD_Y:int:=200
    var chars : array char of boolean
     
    var cur_x,cur_y:real

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 Slope_Calculate(x1:int,y1:int,x2:real,y2:real):real
var slope:real
slope:=(y1-y2/abs(x1-x2))
result slope
end Slope_Calculate



loop
cls
Arena
loop
Input.KeyDown (chars)

if chars (KEY_UP_ARROW) then
            RD_Y := RD_Y + 2
        elsif chars (KEY_DOWN_ARROW) then
            RD_Y := RD_Y - 2
        elsif chars (KEY_RIGHT_ARROW) then
            RD_X := RD_X + 2
        elsif chars (KEY_LEFT_ARROW) then
            RD_X := RD_X - 2
end if

if chars (KEY_SHIFT) then
puckxchange:=Slope_Calculate(400,620,puckx,pucky)
puckychange:=Slope_Calculate(400,620,puckx,pucky)
end if
if RD_X=586 then
RD_X:=RD_X-8
elsif  RD_X=224 then
RD_X:=RD_X+8
end if

if RD_Y=686 then
RD_Y:=RD_Y-8
elsif  RD_Y=112 then
RD_Y:=RD_Y+8
end if

%Arena
Draw.FillPolygon (arena_x, arena_y, 8, 100) 

puckx:=puckx+puckxchange
pucky:=pucky+puckychange

cur_x:=puckx
cur_y:=pucky
%Puck
Draw.FillOval(round(puckx),round(pucky),2,2,black)

if puckx=598 or puckx=202 then
puckxchange:=puckxchange*-1
end if

if pucky=698 or pucky=102 then
puckychange:=puckychange*-1
end if
%Puck Corners
if (pucky-450)>=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 puckx < RD_X+18 and puckx >RD_X-34 and pucky > RD_Y-15 and pucky <RD_Y+16 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 <RD_Y+16 then
puckx:=cur_x
pucky:=cur_y
puckxchange:=0
puckychange:=0
end if
%%%Target(Version1Only
Draw.Box(380,600,420,640,red)
%%%%User Right Defense
Draw.Line(RD_X+14,RD_Y+7,RD_X-25,RD_Y-5,black)
Draw.Line(RD_X-25,RD_Y-5,RD_X-30,RD_Y-2,black)

Draw.FillOval(RD_X,RD_Y,5,10,9)
Draw.FillOval(RD_X,RD_Y+12,4,4,66)

Draw.Line(RD_X+4,RD_Y+8,RD_X+14,RD_Y+8,9)
Draw.Line(RD_X-4,RD_Y+8,RD_X-14,RD_Y,9)

Draw.FillOval(RD_X-14,RD_Y,2,3,9)
Draw.FillOval(RD_X+14,RD_Y+8,2,3,9)

Draw.FillOval(RD_X-6,RD_Y-11,2,5,black)
Draw.FillOval(RD_X+6,RD_Y-11,2,5,black)
%%%%%%%%%%%%%%


%ScoreBox
Draw.FillBox(340,0,460,50,black)
player_score:=intstr(score)
Font.Draw(player_score,360,10,Score_font,40)
Font.Draw(" : 0",380,10,Score_font,40)
%Timebox

if puckxchange=0 and puckychange=0 and cur_x >390 and cur_x <430 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
Sponsor
Sponsor
Sponsor
sponsor
guruguru




PostPosted: Tue May 25, 2004 10:00 pm   Post subject: (No subject)

Your're making both the x and y change equal to the slope?!?! That won't work. The slope is a way of finding the ratio between the x and y change.

Learn a bit more about slopes and then try to tackle this problem... if you can't do simple slopes then you will have major problems later in your game development.
@DRI@N




PostPosted: Tue May 25, 2004 11:26 pm   Post subject: (No subject)

What do you mean though! If you add 1 to x and y change then the slope is 1!! So if I'm adding the slope to x and y change then the slope is (slope)...I don't see any other way to do it.
guruguru




PostPosted: Wed May 26, 2004 3:52 pm   Post subject: (No subject)

Say slope is 1/4... then the x increase would be 4, and the y increase 1.
Say slope is 6/5... then the x increase would be 5, and the y increase 6.
Say slope is 3/1... then the x increase would be 1, and the y increase 3.
Say slope is 0... then the x increase would be anything, and the y increase 0.

You can then take these values and scale them down to the size you want. Say your total distance is 10.

Slope = 3/5
XtempIncrease = 5
YtempIncrease = 3
TotalCurrentIncrease = 8
Total Distance = 10

XIncrease = XtempIncrease * Total Distance / TotalCurrentDistance
XIncrease = 5 * 10 / 8 = 6.25
YIncreaes = 3 * 10 / 8 = 3.75

Ta dah. Hope that helps. If any questions, ask away.
Tony




PostPosted: Wed May 26, 2004 4:29 pm   Post subject: (No subject)

@DRI@N : first of all you got to change your controls to allow for sideway movement. So 4 separate if statements for each button.

as for the puck - have you tried debugging it? guruguru brings up a good point that your slope is always 45 degrees... and your puck change is 600something pixels at a time (so it shots staright out of the screen)

what you have to do is to find the angle and then to find puck change components
code:

changeX := puckSpeed * cosd(angle)
changeY := puckSpeed * sind(angle)
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
@DRI@N




PostPosted: Wed May 26, 2004 7:23 pm   Post subject: (No subject)

Does the slope go where PuckSpeed is?
@DRI@N




PostPosted: Wed May 26, 2004 7:28 pm   Post subject: (No subject)

Oh yeah that might work, but wouldnt I need to use Tangent? because I want the angle of elevation...and I know the opposite and adjacent lengths...
@DRI@N




PostPosted: Wed May 26, 2004 7:31 pm   Post subject: (No subject)

Sorry just one last thing... the Turing index has basically the same description for functions COS and COSD..can you explain the difference?
Sponsor
Sponsor
Sponsor
sponsor
AsianSensation




PostPosted: Wed May 26, 2004 7:35 pm   Post subject: (No subject)

cos is in radians, and cosd is in degrees.

180 degrees = pie

therefore, 1 degree = pie/180
Cervantes




PostPosted: Wed May 26, 2004 8:39 pm   Post subject: (No subject)

did you say angle tony?
tony wrote:
find the angle

you did! Laughing
in that case:
code:

function find_angle (x1, y1, x2, y2 : real) : real
    if x2 = x1 then
        if y2 >= y1 then
            result 90
        elsif y2 < y1 then
            result 270
        end if
    else
        if x2 > x1 and y2 >= y1 then %QUAD 1
            result (arctand ((y2 - y1) / (x2 - x1)))
        elsif x2 > x1 and y2 < y1 then %QUAD 2
            result 360 + (arctand ((y2 - y1) / (x2 - x1)))
        elsif x2 < x1 and y2 < y1 then %QUAD 3
            result 180 + (arctand ((y2 - y1) / (x2 - x1)))
        elsif x2 < x1 and y2 >= y1 then %QUAD 4
            result 180 + (arctand ((y2 - y1) / (x2 - x1)))
        end if
    end if
end find_angle

Mouse.ButtonChoose ("multibutton")
var x1, y1, x2, y2 : real
var mx, my, btn : int
var firsthit := false

loop
    cls
    mousewhere (mx, my, btn)

    if btn = 1 and firsthit = false then
        firsthit := true
        x1 := mx
        y1 := my
    end if
    if btn = 100 then
        firsthit := false
    end if
    if firsthit = true then
        x2 := mx
        y2 := my
        drawline (round (x1), round (y1), round (x2), round (y2), black)
        locate (1, 1)
        put find_angle (x1, y1, x2, y2)
    end if
    View.Update
    delay (10)
end loop


your very own angle finding function.
enjoy
guruguru




PostPosted: Wed May 26, 2004 9:46 pm   Post subject: (No subject)

lol. Don't get caught up in Cervanted mystical world :p. Stick with what your doing with the slope, its easier and you seem to know what you are doing.

And answering your question to Toney... the slope is the slope. THe puckspeed is what I explained as xChange and yChange. ANd don't get into tangents... and trig...

Wait... have you been able to find the slope?!?! If you havent then... I did a quick post on trig in your old topic... check tehre...
Tony




PostPosted: Wed May 26, 2004 10:09 pm   Post subject: (No subject)

why are there two threads on the exact same topic? Thinking

if you keep calculations based on the slope components, then your X/Y change will depend on the slope itself and could ether move too slow or too fast (I think it was an issue before when the puck shot at 600pixels/frame)
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
guruguru




PostPosted: Wed May 26, 2004 10:11 pm   Post subject: (No subject)

Tony: I also posted a way to scale it down to a total distance. Look at my above post Wink .
Tony




PostPosted: Wed May 26, 2004 10:25 pm   Post subject: (No subject)

guruguru : have you checked your results? Laughing
code:

put sqrt(6.25*6.25+3.75*3.75)

result is 7.3. You loosing almost 30% of your original total distance of 10 Laughing
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
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: