Computer Science Canada

Real/Int problems (to do with Math.Distance)

Author:  pttptppt [ Fri Feb 24, 2017 8:04 pm ]
Post subject:  Real/Int problems (to do with Math.Distance)

What is it you are trying to achieve?
I'm making a game similar to Diep.io. Basically as the user moves, depending on the direction of movement, little bullets will shoot. Now my program still requires work but I have a big problem.


What is the problem you are having?
The only methods Ive found for achieving the goal of "shooting" is using "Math.Distance" from this thread: http://compsci.ca/v3/viewtopic.php?t=30017. The problem is that if the values are not real, then Math.Distance doesn't work.


Describe what you have tried to solve this problem
I've tried using the Round command but after rounding and whatnot, it tells me that it has no value. So either the "Assigned values are the wrong type" or "It has no value"


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
The problematic code is highlighted with a lot of percent signs.

Turing:


import GUI
setscreen ("graphics:1500;1500")

var x, y, button : int
var xtemp, ytemp : int := 0

procedure draw (x, y : int)
    View.Set ("offscreenonly")
    %Scope casing
    Draw.FillOval (x, y, 55, 55, black)
    Draw.FillOval (x, y, 50, 50, white)

    %Thick crosshair threads
    Draw.ThickLine (x - 50, y, x - 35, y, 3, black)
    Draw.ThickLine (x + 50, y, x + 35, y, 3, black)
    Draw.ThickLine (x, y - 50, x, y - 35, 3, black)
    Draw.ThickLine (x, y + 50, x, y + 35, 3, black)

    %Thin threads
    Draw.Line (x - 20, y, x + 20, y, red)
    Draw.Line (x, y - 20, x, y + 20, red)

    %Adds slight delay. Optional
    delay (100)

    View.Update
end draw

procedure undraw

    %Scope casing
    Draw.FillOval (x, y, 55, 55, white)
    Draw.FillOval (x, y, 50, 50, white)

    %Thick crosshair threads
    Draw.ThickLine (x - 50, y, x - 35, y, 3, white)
    Draw.ThickLine (x + 50, y, x + 35, y, 3, white)
    Draw.ThickLine (x, y - 50, x, y - 35, 3, white)
    Draw.ThickLine (x, y + 50, x, y + 35, 3, white)

    %Thin threads
    Draw.Line (x - 20, y, x + 20, y, white)
    Draw.Line (x, y - 20, x, y + 20, white)

end undraw

var speed : int := 2
var xfinal, yfinal : int
var xv, yv : int

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
proc shoot
xv := (x - xtemp) / (Math.Distance (xtemp, ytemp, x, y) / speed)
yv := (y - ytemp) / (Math.Distance (xtemp, ytemp, x, y) / speed)
    x := xtemp
    y := ytemp
end shoot
shoot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
loop
    Mouse.Where (x, y, button)  %button is a variable that checks if mouse is clicked
    draw (x, y)
    undraw
    delay (5)
    if button = 1 then
        x += xv                                              % uses velocities to determine the new x and y values
        y -= yv
        if Math.Distance (x, y, xtemp, ytemp) <= 9 then
            delay (100)
            shoot
        end if
        drawfilloval (round (xtemp), round (ytemp), 10, 10, blue)
        drawfilloval (round (x), round (y), 10, 10, black)
        drawfilloval (round (x), round (y), 2, 2, brightred)
        delay (75)
        View.Update

    end if
    xtemp := x
    ytemp := y
end loop


Please specify what version of Turing you are using
Latest
[url][/url]

Author:  Insectoid [ Sat Feb 25, 2017 1:42 pm ]
Post subject:  RE:Real/Int problems (to do with Math.Distance)

Math.Distance works with integers. Any function that takes reals as input will also accept integers.

Even if you couldn't do that, the Math.Distance function is nothing more than a^2 + b^2 = c^2. You could write your own version in one line of code.


: