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

Username:   Password: 
 RegisterRegister   
 Simple Mouse.Where Program Help
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
V1RU5




PostPosted: Mon Jun 07, 2010 8:25 am   Post subject: Simple Mouse.Where Program Help

What is it you are trying to achieve?
Basically, when the mouse is clicked, the ball should move in the direction of the mouse and fall down with gravity without holding the mouse button


What is the problem you are having?
I do not know what to do :S


Describe what you have tried to solve this problem
I tried making a program but it didnt work so any help would be appreciated


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


Turing:


<Add your code here>



Please specify what version of Turing you are using
4.1.1
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Mon Jun 07, 2010 9:00 am   Post subject: RE:Simple Mouse.Where Program Help

Start with the idea of "iterative simulation". What this means is that you're going to break time up into "ticks" or "frames", each of which is perhaps a 30th of a second long - short enough that ticks start to "blend together" to the human eye.

On each tick, the ball will move slightly dependent on conditions in the program. The screen will be cleared and the ball will be drawn at its new position. This looks like the following:

Turing:

View.Set ( "offscreenonly" )   % part of the common anti-flicker fix
loop
    % update ball position here
    %   this is where you'd put your Mouse.Where and logic...which I will not be writing for you

    % draw ball here
    %   you'll probably want either Draw.FillOval or Pic.Draw

    % update screen, then clear it for the next frame
    View.Update()
    Draw.Cls()
end loop


This gives the illusion of animation and makes it obvious where you should put the logic controlling the position of the ball.
Cezna




PostPosted: Mon Jun 07, 2010 11:03 am   Post subject: Re: Simple Mouse.Where Program Help

Here is a piece of code for a particle engine that should give you an idea of what you need:

code:

function getAngle (x, y : real) : real
    if x = 0 and y = 0 then
        result 0
    elsif x = 0 and y > 0 then
        result 90
    elsif x < 0 and y = 0 then
        result 180
    elsif x = 0 and y < 0 then
        result 270
    elsif x > 0 and y > 0 then
        result arctand (y / x)
    elsif x < 0 and y > 0 then
        result 180 + arctand (y / x)
    elsif x < 0 and y < 0 then
        result 180 + arctand (y / x)
    elsif x > 0 and y < 0 then
        result 360 + arctand (y / x)
    else
        result 0
    end if
end getAngle

proc Moving
    angle := getAngle (mx - x, my - y)
    if button = 1 then % this attracts the object
        vx += cosd (angle) * strength
        vy += sind (angle) * strength
    elsif button = 10 then % this repels the object
        vx -= cosd (angle) * strength
        vy -= sind (angle) * strength
    end if
    x += vx
    y += vy
end Moving

proc Gravity
    if y > 0 then
        vy -= gravity
    end if
end Gravity

loop
    cls
    Mouse.Where (mx, my, button)
    if button = 1 then
        Moving
    end if
    Gravity
    drawdot (round (x), round (y), clr)
    delay (40)
    View.Update
end loop



I recommend setting gravity at something like .7 or .8, but anything above 1 will be pretty substantial
V1RU5




PostPosted: Mon Jun 07, 2010 5:43 pm   Post subject: Re: RE:Simple Mouse.Where Program Help

DemonWasp @ Mon Jun 07, 2010 9:00 am wrote:
Start with the idea of "iterative simulation". What this means is that you're going to break time up into "ticks" or "frames", each of which is perhaps a 30th of a second long - short enough that ticks start to "blend together" to the human eye.

On each tick, the ball will move slightly dependent on conditions in the program. The screen will be cleared and the ball will be drawn at its new position. This looks like the following:

Turing:

View.Set ( "offscreenonly" )   % part of the common anti-flicker fix
loop
    % update ball position here
    %   this is where you'd put your Mouse.Where and logic...which I will not be writing for you

    % draw ball here
    %   you'll probably want either Draw.FillOval or Pic.Draw

    % update screen, then clear it for the next frame
    View.Update()
    Draw.Cls()
end loop


This gives the illusion of animation and makes it obvious where you should put the logic controlling the position of the ball.


Turing:


var xPos : int
var yPos : int
var button : int
var x, y : int

View.Set ( "offscreenonly" )   % part of the common anti-flicker fix
loop
    % update ball position here
    %   this is where you'd put your Mouse.Where and logic...which I will not be writing for you
    Mouse.Where (xPos, yPos, button)
   
    x := xPos + 5
    y := yPos + 5

    % draw ball here
    Draw.FillOval (x, y, 10, 10, blue)
   
    % update screen, then clear it for the next frame
    View.Update()
    Draw.Cls()
end loop


I don't get what I am doing wrong Neutral
TheGuardian001




PostPosted: Mon Jun 07, 2010 5:54 pm   Post subject: Re: Simple Mouse.Where Program Help

V1RU5 wrote:

Turing:

    x := xPos + 5
    y := yPos + 5



You aren't moving towards xPos and yPos, you're setting it equal to xPos and yPos.

You need to check whether the ball is currently above or below, and whether it is left or right, then move it slightly (using +=/-=, not :=) towards wherever the mouse is.

Edit: in case you don't know, the += and -= operators:
code:

x := x + 5
y := y + 5

The above code is exactly the same as:
code:

x += 5
y += 5


It's just a short form.
V1RU5




PostPosted: Mon Jun 07, 2010 7:03 pm   Post subject: Re: Simple Mouse.Where Program Help

TheGuardian001 @ Mon Jun 07, 2010 5:54 pm wrote:
V1RU5 wrote:

Turing:

    x := xPos + 5
    y := yPos + 5



You aren't moving towards xPos and yPos, you're setting it equal to xPos and yPos.

You need to check whether the ball is currently above or below, and whether it is left or right, then move it slightly (using +=/-=, not :=) towards wherever the mouse is.

Edit: in case you don't know, the += and -= operators:
code:

x := x + 5
y := y + 5

The above code is exactly the same as:
code:

x += 5
y += 5


It's just a short form.


Okay I did that but now the oval sticks to the mouse, I want it to shoot when the mouse is clicked

Turing:

var xPos : int
var yPos : int
var button : int

View.Set ( "offscreenonly" )   % part of the common anti-flicker fix
loop
    % update ball position here
    %   this is where you'd put your Mouse.Where and logic...which I will not be writing for you
    Mouse.Where (xPos, yPos, button)
   
    xPos += 5
    yPos += 5

    % draw ball here
    Draw.FillOval (xPos, yPos, 10, 10, blue)
   
    % update screen, then clear it for the next frame
    View.Update()
    Draw.Cls()
end loop
USEC_OFFICER




PostPosted: Mon Jun 07, 2010 7:05 pm   Post subject: RE:Simple Mouse.Where Program Help

You need two more variables to replace the xpos and ypos. When you click, the two variables become the current xpos and ypos, then you can affect those two variables all you want.
TheGuardian001




PostPosted: Mon Jun 07, 2010 7:14 pm   Post subject: Re: Simple Mouse.Where Program Help

V1RU5 @ Mon Jun 07, 2010 7:03 pm wrote:

Turing:

    xPos += 5
    yPos += 5

xPos and yPos are the position of the mouse. You don't want to change that. You had it right before with two variables. The problem is what you're doing to them.

Let's go back to what you originally had:

Turing:

x := xPos + 5
y := yPos + 5


The problem here is the := does not work in the same way as, say, accelerating a car. It doesn't slowly change the value of x and y until they match xPos and yPos. It simply changes them instantly to that value.

Instead, you should first check if the ball needs to move up, or down (y > yPos means it has to move down, otherwise, it should move up.)

Now that you know which way it needs to go, you can change its value. Lets say it needs to move down ( if y > yPos.) You should do something like:
Turing:

y -= 5 %subtract 5 from the balls y value. Next time we draw it, it will be 5 pixels down from where it was before.

You can change that 5 to whatever you want (nothing too big, or it'll fly right past the mouse)

Then you just do the same for the x value.
Sponsor
Sponsor
Sponsor
sponsor
V1RU5




PostPosted: Mon Jun 07, 2010 7:35 pm   Post subject: RE:Simple Mouse.Where Program Help

Turing:

var xPos : int
var yPos : int
var button : int
var x : int := 0
var y : int := 0

View.Set ( "offscreenonly" )   % part of the common anti-flicker fix
loop
    % update ball position here
    %   this is where you'd put your Mouse.Where and logic...which I will not be writing for you
    Mouse.Where (xPos, yPos, button)
   
    if button = 1 then
    if y > yPos then
    y -= 2
    elsif y < yPos then
    y += 2
    elsif x > xPos then
    x -= 2
    elsif x < xPos then
    x += 2
    end if
    end if

    % draw ball here
    Draw.FillOval (x, y, 10, 10, blue)
   
    % update screen, then clear it for the next frame
    View.Update()
    Draw.Cls()
end loop


K now it goes up first and then goes right, how do I make it go diagonally.
TheGuardian001




PostPosted: Mon Jun 07, 2010 7:43 pm   Post subject: Re: Simple Mouse.Where Program Help

X and Y are independant of each other. X should be in its own if, and Y should be in its own if. With elsifs, only the first true condition is ever executed.
V1RU5




PostPosted: Mon Jun 07, 2010 7:49 pm   Post subject: Re: Simple Mouse.Where Program Help

TheGuardian001 @ Mon Jun 07, 2010 7:43 pm wrote:
X and Y are independant of each other. X should be in its own if, and Y should be in its own if. With elsifs, only the first true condition is ever executed.


K thanks so much its working now, one last question, how do I add gravity?
TheGuardian001




PostPosted: Mon Jun 07, 2010 8:08 pm   Post subject: Re: Simple Mouse.Where Program Help

Well, gravity is just always moving stuff down. Based on what you've done so far, how do you think this could be accomplished?
V1RU5




PostPosted: Mon Jun 07, 2010 8:58 pm   Post subject: Re: Simple Mouse.Where Program Help

TheGuardian001 @ Mon Jun 07, 2010 8:08 pm wrote:
Well, gravity is just always moving stuff down. Based on what you've done so far, how do you think this could be accomplished?


Okay so basically I have to make the ball y = 0 for it to touch the ground and it should move and bit on the x axis also depending on the way it is getting shot at. Do you have msn or something cause msn is faster. If you have msn, pm me yours. Thanks
V1RU5




PostPosted: Tue Jun 08, 2010 8:17 am   Post subject: RE:Simple Mouse.Where Program Help

anyone? I need help with gravity
Cezna




PostPosted: Tue Jun 08, 2010 10:54 am   Post subject: RE:Simple Mouse.Where Program Help

You need to subtract a constant gravity value (probably something like .7 or .8) from the y velocity every cycle of the main loop.
Then you can tell it not to enact gravity if the ball is on top of something such as the ground or an object.
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 2  [ 21 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: