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

Username:   Password: 
 RegisterRegister   
 Lines with negative slope won't reflect laser beams!!!
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
amz_best




PostPosted: Tue May 12, 2015 6:51 pm   Post subject: Lines with negative slope won't reflect laser beams!!!

What is it you are trying to achieve?
Reflect laser beams off lines

What is the problem you are having?
ANYTHING WITH NEGATIVE SLOPE DOESNT WORK!!!


Describe what you have tried to solve this problem
I TRIED PLAYING WITH THE VARIABLES, SWITCHING EVERYTHING AROUND, REPACING STUFF, REFRESHING VARIABLES, BUT NOTHING WORKED!!!

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

Turing:


setscreen ("graphics:400;500, offscreenonly")
%This type will get the x, y, and directions of x and y
type point :
    record
        x, y : real
        dx, dy : real
    end record

%This type will get 2 points of a strait line, the slope and the y intercept
type line :
    record
        s, e : point
        m, b : real
    end record

   
%This type will create a circular ball that will intercept the line, points of center, 8 points outside and the slope (direction its moving in in slope)
type laserP :
    record
        p : point
        u, d, l, r, ul, ur, dl, dr : point
        intC : int
        m : real
    end record

%Arrays for laser and lines, as well as points for the laser shooter
var a_laser : flexible array 1 .. 0 of laserP
var a_lines : flexible array 1 .. 0 of line
var shooter : point
%Keybaord and mouse
var chars : array char of boolean
var mX, mY, mB : int

%Give variables values
shooter.x := maxx / 2
shooter.y := 40
new a_lines, 2
a_lines (1).s.x := 1
a_lines (1).s.y := 1
a_lines (1).e.x := maxx
a_lines (1).e.y := maxy
a_lines (1).m := (a_lines (1).e.y - a_lines (1).s.y) / (a_lines (1).e.x - a_lines (1).s.x)

a_lines (2).s.x := maxx - 1
a_lines (2).s.y := 1
a_lines (2).e.x := maxx
a_lines (2).e.y := maxy - 10
a_lines (2).m := (a_lines (2).e.y - a_lines (2).s.y) / (a_lines (2).e.x - a_lines (2).s.x)


%Move shooter left and right
procedure movement
    Input.KeyDown (chars)
    if chars (KEY_LEFT_ARROW) and shooter.x > 10 then
        shooter.x -= 2
    end if

    if chars (KEY_RIGHT_ARROW) and shooter.x < maxx - 10 then
        shooter.x += 2
    end if

    if chars (KEY_UP_ARROW) then
        %Tilt shooter
    end if

    if chars (KEY_DOWN_ARROW) then
        %Tilt shooter
    end if
end movement



%This procedure will draw the lines, check for collision between line and ball, calculate new slope and display new slope at location 1, 1
procedure drawlines (i : int)
    for s : 1 .. upper (a_lines)
        Draw.ThickLine (round (a_lines (s).s.x), round (a_lines (s).s.y), round (a_lines (s).e.x), round (a_lines (s).e.y), 3, black)
        if i <= upper (a_laser) and i > 1 and
                (round (a_lines (s).m * a_laser (i).u.x + a_lines (s).b) - round (a_laser (i).u.y) = 0 or
                round (a_lines (s).m * a_laser (i).u.x + a_lines (s).b) - round (a_laser (i).u.y + 1) = 0 or
                round (a_lines (s).m * a_laser (i).u.x + a_lines (s).b) - round (a_laser (i).u.y - 1) = 0 or
                round (a_lines (s).m * a_laser (i).u.x + a_lines (s).b - 1) - round (a_laser (i).u.y + 1) = 0 or
                round (a_lines (s).m * a_laser (i).u.x + a_lines (s).b + 1) - round (a_laser (i).u.y - 1) = 0) then
            if (a_laser (i).p.x > a_lines (s).e.x and a_laser (i).p.y > a_lines (s).e.y and
                    a_laser (i).p.x < a_lines (s).s.x and a_laser (i).p.y < a_lines (s).s.y) or
                    (a_laser (i).p.x > a_lines (s).s.x and a_laser (i).p.y > a_lines (s).s.y and
                    a_laser (i).p.x < a_lines (s).e.x and a_laser (i).p.y < a_lines (s).e.y)
                    then
                a_laser (i).p.dx := arctand (-a_lines (s).m) * 2 - a_laser (i).p.dx - 180
                a_laser (i).p.dy := arctand (-a_lines (s).m) * 2 - a_laser (i).p.dy - 180
                locate (1, 1)
                put a_laser (i).p.dx
            end if
        end if
    end for
end drawlines



procedure drawray
    %Constantly add new slope,
    new a_laser, upper (a_laser) + 1
    a_laser (upper (a_laser)).p.x := shooter.x
    a_laser (upper (a_laser)).p.y := shooter.y
    a_laser (upper (a_laser)).p.dx := 360
    a_laser (upper (a_laser)).p.dy := 360
    %u, d, l, r, ul, ur, dl, dr : point
   
    %Draw all lasers, do calculations and move lasers AND draw lines >>> *that will check for collision*
    for i : 1 .. upper (a_laser)
        Draw.FillOval (round (a_laser (i).p.x), round (a_laser (i).p.y), 1, 1, brightred)
        a_laser (i).p.y += cosd (a_laser (i).p.dy) * 3
        a_laser (i).p.x += sind (a_laser (i).p.dx) * 3
        a_laser (i).u.y := a_laser (i).p.y + 1
        a_laser (i).u.x := a_laser (i).p.x
        a_laser (i).d.y := a_laser (i).p.y - 1
        a_laser (i).l.x := a_laser (i).p.y - 1
        a_laser (i).r.x := a_laser (i).p.y + 1
        if i > 1 then
            a_laser (i).m := a_laser (i - 1).m
        else
            a_laser (i).m := tan (90)
        end if
        drawlines (i)
    end for
   
    %Remove lasers out of the field of view
    if a_laser (lower (a_laser)).p.y > maxy - 5 or a_laser (lower (a_laser)).p.x > maxx - 5 or a_laser (lower (a_laser)).p.y < 5 or a_laser (lower (a_laser)).p.x < 5 then
        for i : 1 .. upper (a_laser) - 1
            var tempX : real := a_laser (i + 1).p.x
            var tempY : real := a_laser (i + 1).p.y
            var tempDX : real := a_laser (i + 1).p.dx
            var tempDY : real := a_laser (i + 1).p.dy
            a_laser (i + 1).p.x := a_laser (i).p.x
            a_laser (i + 1).p.y := a_laser (i).p.y
            a_laser (i + 1).p.dx := a_laser (i).p.dx
            a_laser (i + 1).p.dy := a_laser (i).p.dy
            a_laser (i).p.x := tempX
            a_laser (i).p.y := tempY
            a_laser (i).p.dx := tempDX
            a_laser (i).p.dy := tempDY
        end for
        new a_laser, upper (a_laser) - 1
    end if
end drawray

%Main Loop
loop
    %Check Mouse
    Mouse.Where (mX, mY, mB)
    %Move shooter
    movement
    cls
    %Give lines values
    put upper (a_laser)
    a_lines (1).e.x := mX
    a_lines (1).e.y := mY
    a_lines (2).e.x := mX
    a_lines (2).e.y := mY
    a_lines (1).m := (a_lines (1).e.y - a_lines (1).s.y) / (a_lines (1).e.x - a_lines (1).s.x)
    a_lines (2).m := (a_lines (2).e.y - a_lines (2).s.y) / (a_lines (2).e.x - a_lines (2).s.x)
    a_lines (1).b := a_lines (1).e.y - (a_lines (1).m * a_lines (1).e.x)
    a_lines (2).b := a_lines (2).e.y - (a_lines (2).m * a_lines (2).e.x)
    put a_lines (1).m
    put a_lines (2).m
    %Draw shooter
    drawbox (round (shooter.x - 8), 0, round (shooter.x + 8), round (shooter.y), black)
    %Draw first line
    drawlines (1)
    %Draw all rays
    drawray
    %Update screen
    View.Update ()
end loop



Please specify what version of Turing you are using
Latest
Sponsor
Sponsor
Sponsor
sponsor
amz_best




PostPosted: Tue May 12, 2015 9:04 pm   Post subject: Re: Lines with negative slope won't reflect laser beams!!!

nvm: got it working.. this part of the code was ruining it:
Turing:


if (a_laser (i).p.x > a_lines (s).e.x and a_laser (i).p.y > a_lines (s).e.y and
                    a_laser (i).p.x < a_lines (s).s.x and a_laser (i).p.y < a_lines (s).s.y) or
                    (a_laser (i).p.x > a_lines (s).s.x and a_laser (i).p.y > a_lines (s).s.y and
                    a_laser (i).p.x < a_lines (s).e.x and a_laser (i).p.y < a_lines (s).e.y)
                    then





All you have to do is name variables like startPointX, endPointX and same fr y, find the greater x and y points of the start and end points of the line, than just simply make an if statement to make sure the laser is within those points. Thank you for all your help!

Turing:


var sPX : real
            var ePX : real
            var sPY : real
            var ePY : real
            if a_lines (s).s.x < a_lines (s).e.x then
                sPX := a_lines (s).s.x
                ePX := a_lines (s).e.x
            else
                sPX := a_lines (s).e.x
                ePX := a_lines (s).s.x
            end if
           
            if a_lines (s).s.y < a_lines (s).e.y then
                sPY := a_lines (s).s.y
                ePY := a_lines (s).e.y
            else
                sPY := a_lines (s).e.y
                ePY := a_lines (s).s.y
            end if
            if a_laser (i).p.x > sPX and a_laser (i).p.x < ePX and a_laser (i).p.y > sPY and a_laser (i).p.y < ePY then
                a_laser (i).p.dx := arctand (-a_lines (s).m) * 2 - a_laser (i).p.dx - 180
                a_laser (i).p.dy := arctand (-a_lines (s).m) * 2 - a_laser (i).p.dy - 180
                locate (1, 1)
                put a_laser (i).p.dx
            end if

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  [ 2 Posts ]
Jump to:   


Style:  
Search: