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

Username:   Password: 
 RegisterRegister   
 Why does the line equation hate me so?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
RedRogueXIII




PostPosted: Wed Aug 30, 2006 4:29 pm   Post subject: Why does the line equation hate me so?

Here is my test platform code for a 2d sidescroller, handling slopes and such, I've gotten the main char ( a red ball ) to land on the platform then ascend a steep slope without weird bouncing, but on a slightly inverted 30* and less it gives me this weird bouncing. on straight surfaces it works fine.

code:
View.Set ("graphics:max,max, offscreenonly")
var x1, y1, x2, y2, meX, meY, distance : int
var m, b : real
var tX, tY : int
var controls : array char of boolean
% The line works fine on a straight or steep slope but screws up on a small slope
x1 := 200
y1 := 300
x2 := 500
y2 := 400

meX := 300
meY := 400
fcn wallDETECTION (x1_, y1_, x2_, y2_, charX_, charY_ : int, m_, b_ : real) : boolean
    var x, y, ratio : real
    var angle : int
    for i : x1_ .. x2_
        x := i
        y := (m_ * x) + b_
        drawdot (round (x), round (y), black)
        /*if (charX_ - round (x)) not= 0 then
         ratio := (charY_ - round (y)) / (charX_ - round (x))
         else
         ratio := (charY_ - round (y)) / 0.001
         end if
         angle := round (arctand (abs (ratio)))
         if (charX_ - round (x)) < 0 then
         angle := 180 - angle
         end if
         if (charY_ - round (y)) < 0 then
         angle := 360 - angle
         end if
         locate (1, 1)
         put angle ..*/
        if Math.Distance (x, y, charX_, charY_) <= 5 then %and angle >= 0 and angle <= 180 then
            %if angle = 27 or angle = 180 then
            %    meY += 5
            %end if
            %put "HIT:" ..
            result true
        end if
    end for
    result false
end wallDETECTION

proc initialize (x1_, y1_, x2_, y2_ : int)
    x1 := x1_
    x2 := x2_
    y1 := y1_
    y2 := y2_
    if x2 - x1 <= 0 then
        m := (y2 - y1) / 0.0000001
    else
        m := (y2 - y1) / (x2 - x1)
    end if
    b := (m * x1 - y1) * -1
    distance := round (sqrt ((x2 - x1) ** 2 + (y2 - y1) ** 2))
end initialize

proc move
    Input.KeyDown (controls)
% PROBLEM AREA
    % The t variables represents the line and the balls position shifted up by 5
    % The me variables is the ball shifted up 5, cept that it descends down into the t's trajectory.
    if controls (KEY_RIGHT_ARROW) then
        if wallDETECTION (x1, y1, x2, y2, meX, meY, m, b) = true then
            meX += 5
            tX := meX
            meY := round ((m * intreal (meX)) + b) + 5
            tY := round ((m * intreal (meX)) + b)
        else
            meX += 5
        end if
    elsif controls (KEY_LEFT_ARROW) then
        if wallDETECTION (x1, y1, x2, y2, meX, meY, m, b) = true then
            meX -= 5
            tX := meX
            meY := round ((m * intreal (meX)) + b) + 5
            tY := round ((m * intreal (meX)) + b)
        else
            meX -= 5
        end if
    end if
    if controls (KEY_UP_ARROW) then
        meY += 10
    end if
    if wallDETECTION (x1, y1, x2, y2, meX, meY, m, b) = false then
        meY -= 5
        tX := meX
        tY := meY
    end if
end move

initialize (x1, y1, x2, y2)

loop
    move
    View.Update
    cls
    drawline (x1, y1, x2, y2, black)
    drawoval (meX, meY, 5, 5, red)
    drawoval (meX, meY - 5, 5, 5, 1)
    drawoval (tX, tY, 5, 5, green)
    delay (10)
    locate (1, 1)
    put meX, " ", meY ..
    locate (2, 1)
    put meX, " ", meY - 5 ..
end loop
% Need to check for Steep slopes and change so that movement falls better :D


Can anyone help me debug this annoying glitch?
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Wed Aug 30, 2006 9:50 pm   Post subject: (No subject)

I had a similar question a while back. I don't think I ever solved it, but the code there might be of some use. Not sure.
NikG




PostPosted: Fri Sep 01, 2006 11:22 am   Post subject: (No subject)

I think your problem lies in the fact that whenever the left or right arrow is pressed and there is a collision, you are added 5 to meX (and doing whatever to the other variables).

Instead, you should have your move function make your ball move 5 pixels in the direction of the slope. Something along the lines of:
code:
meX += 5 * cosd(angle)
meY += 5 * sind(angle)
where angle is the angle of the line segment the ball is on...

I think that should stop the bouncing effect.
NikG




PostPosted: Fri Sep 01, 2006 11:33 am   Post subject: (No subject)

Sorry for the double post, I just thought I'd test out what I suggested:
code:
View.Set ("offscreenonly")
var chars : array char of boolean
var meX, meY := 100.0
var angle := 30

loop
    Input.KeyDown (chars)
    if chars (KEY_RIGHT_ARROW) then
        meX += 5 * cosd (angle)
        meY += 5 * sind (angle)
    elsif chars (KEY_LEFT_ARROW) then
        meX -= 5 * cosd (angle)
        meY -= 5 * sind (angle)
    end if

    drawoval (round (meX), round (meY), 5, 5, black)
    View.Update
    delay (25)
    cls
end loop
angle represents the angle of the line segment the ball is on. Try changing it and running the program; the ball moves pretty smoothly at every angle I believe.

The only problem I see with the approach is that if you have a vertical angle (say 80-90 degrees), the ball moves up/down when you press right/left, but you can probably limit this easily.
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  [ 4 Posts ]
Jump to:   


Style:  
Search: