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

Username:   Password: 
 RegisterRegister   
 ball control using mouse help
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
neufelni




PostPosted: Tue May 03, 2005 11:07 am   Post subject: ball control using mouse help

I am making a mini golf game and I need some help. How would you be able to use your mouse to control the ball. I want to be able to click on the ball and pull the mouse back and sideways to change the velocity and the angle that the ball will travel at.
Sponsor
Sponsor
Sponsor
sponsor
Viper




PostPosted: Tue May 03, 2005 1:28 pm   Post subject: (No subject)

i think you could do something like this

if mb>=1 and whatdotcolour (mx,my)=white(ball colour) then
if ballx-mx>=10 then(this determines how far you pulled the mouse from the ball to the left only youll have to put something to determine which way they pulled the mouse or have all courses go one way)
ballxincrease=5(ballxincrease is how many x picks it will move a one time)
....
....
i cant help with the direction though i know you need to us math n angles
not much help but a basic idea i guess
Cervantes




PostPosted: Tue May 03, 2005 7:37 pm   Post subject: (No subject)

This is more for a pool type game, but here:
Turing:

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

View.Set ("offscreenonly")
Mouse.ButtonChoose ("multibutton")
var x1, y1, x2, y2 : real
var mx, my, btn : int
var firsthit := false
var angle, distance, ballX, ballY, ballVX, ballVY, speedAdjust : real
ballX := maxx / 2
ballY := maxy / 2
ballVX := 0
ballVY := 0
speedAdjust := 5


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
        drawline (round (x1), round (y1), mx, my, black)
        if btn = 0 then
            firsthit := false
            x2 := mx
            y2 := my
            angle := find_angle (x1, y1, x2, y2)
            distance := Math.Distance (x1, y1, x2, y2)
            ballVX := cosd (angle) * distance / speedAdjust %speedAdjust just makes it go less insanely fast
            ballVY := sind (angle) * distance / speedAdjust
        end if
    end if

    ballX += ballVX
    ballY += ballVY

    drawfilloval (round (ballX), round (ballY), 10, 10, red)
    View.Update
    delay (10)
end loop
neufelni




PostPosted: Wed May 04, 2005 10:58 am   Post subject: (No subject)

Cervantes had the line of code :
code:

distance := Math.Distance (x1, y1, x2, y2)



The Math.Distance does not work with the version of Turing that I am using. It tells me that Distance is nt an export command of Math. Is there any other way that I could do this without using Math.Distance?
fehrge




PostPosted: Wed May 04, 2005 11:06 am   Post subject: (No subject)

Here is a simple way of doing it that worked for me. I had to put the check process in or else the ball would never stop. It has some minor glitches but it works.

code:

%ball move
setscreen ("graphics:800;600,position:50;100,nocursor")

%variables and costants
const divby := 15
var x : int := 400
var y : int := 300
var vx : real := 0
var vy : real := 0
var exitB : boolean := false
var color1 : int := 7
var color2 : int := 0

var mb, mx, my : int := 0

%move ball
process moveBall
    loop
        %draw and erase the ball
        Draw.FillOval (x, y, 10, 10, color1)
        delay (100)
        Draw.FillOval (x, y, 10, 10, color2)
        %move the x and y coordinates
        x += round (vx)
        y += round (vy)

        %check for collision
        if x - 10 < 0 or x + 10 > maxx then
            vx *= - 1
        end if

        if y - 10 < 0 or y + 10 > maxy then
            vy *= - 1
        end if

        %slow down the ball
        if vx > 0 then
            vx := vx - .1
        elsif vx < 0 then
            vx := vx + .1
        end if
        if vy > 0 then
            vy := vy - .1
        elsif vy < 0 then
            vy := vy + .1
        end if
        exit when exitB = true
    end loop
end moveBall

process check1
    loop
        %stop ball of it is going to slow
        if vy < .01 and vy > - .01 then
            vy := 0
        end if
        if vx < .01 and vx > - .01 then
            vx := 0
        end if
        if vy = 0 and vx = 0 then
            exitB := true
        end if
        exit when exitB = true
    end loop
end check1

%shoot ball
process interFace
    loop
        mb := 0
        Mouse.Where (mx, my, mb)
        if mx < x + 20 and mx > x - 20 and my < y + 20 and my > y - 20 and
                mb = 1 then
            loop
                Mouse.Where (mx, my, mb)
                Draw.Line (x, y, mx, my, color1)
                delay (100)
                Draw.Line (x, y, mx, my, color2)
                exit when mb = 0
            end loop
            vx := (x - mx) / divby
            vy := (y - my) / divby
            exit when vx not= 0
        end if
    end loop

    fork check1

end interFace

fork interFace
fork moveBall
Cervantes




PostPosted: Wed May 04, 2005 7:43 pm   Post subject: (No subject)

fehrge: processes = evil!

Nick: Use the distance formula, to write your own distance function:
code:

function MathDistance (x1, y1, x2, y2 : real) : real
  result ((x2 - x1)**2 + (y2 - y1)**2)**0.5
end MathDistance
neufelni




PostPosted: Mon May 09, 2005 11:05 am   Post subject: (No subject)

I do not like that fehrge has proccesses in his code but his ball slows down, unlike Cervantes' and I need it to slow down.
I would uses fehrge's code but I do not like using proccesses and since I am new to Turing I do not know how I can change either fehrge's or Cervantes' code to be like I want it.

fehrge : Get rid of the proccesses and keep everything else the same,

OR

Cervantes : Could you make it so the ball slows down and you are only able to shoot the ball once? And also slow the ball down a little. It goes to fast.
Cervantes




PostPosted: Mon May 09, 2005 4:47 pm   Post subject: (No subject)

Nick wrote:

Cervantes : Could you make it so the ball slows down and you are only able to shoot the ball once? And also slow the ball down a little. It goes to fast.

Yes, I can do all of that. The question is, can you? This is your project, not mine. Wink I posted the above code as an example, not to be directly used in your project. Fortunately, it seems, that code is not completely what you want, so you'll have to understand that code in order to edit it to suit your needs.

That said, let's go through how we will accomplish these things:
To slow the ball down, we want to decrease it's velocity by a certain ratio each time through the loop. If we multiply the velocity of the object by, say, 0.99, we will effectively slow the object down over time.
To prevent the ball from being shot more than once, we simply need a boolean variable to keep track of whether the ball has been shot or not. As soon as we shoot, we want to set this boolean to true. We want to prevent the user from shooting if this boolean is false.
To slow the ball down a little, you just play with the speedAdjust variable.

Okay, so I've basically told you how to do these things. I think it should be pretty easy for you, provided you can understand the original code I posted. Let us know if you need any clarification with the code I posted above or anything I've said in this post.

-Cervantes
Sponsor
Sponsor
Sponsor
sponsor
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  [ 8 Posts ]
Jump to:   


Style:  
Search: