
-----------------------------------
V1RU5
Mon Jun 07, 2010 8:25 am

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)








Please specify what version of Turing you are using
4.1.1

-----------------------------------
DemonWasp
Mon Jun 07, 2010 9:00 am

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:


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
Mon Jun 07, 2010 11:03 am

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:


I recommend setting gravity at something like .7 or .8, but anything above 1 will be pretty substantial


-----------------------------------
V1RU5
Mon Jun 07, 2010 5:43 pm

Re: 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:


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.



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  :|

-----------------------------------
TheGuardian001
Mon Jun 07, 2010 5:54 pm

Re: Simple Mouse.Where Program Help
-----------------------------------


    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
[/code]
The above code is exactly the same as:
[code]
x += 5
y += 5
[/code]

It's just a short form.

-----------------------------------
V1RU5
Mon Jun 07, 2010 7:03 pm

Re: Simple Mouse.Where Program Help
-----------------------------------


    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:


Okay I did that but now the oval sticks to the mouse, I want it to shoot when the mouse is clicked


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
Mon Jun 07, 2010 7:05 pm

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
Mon Jun 07, 2010 7:14 pm

Re: Simple Mouse.Where Program Help
-----------------------------------


    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:


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:

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.

-----------------------------------
V1RU5
Mon Jun 07, 2010 7:35 pm

RE:Simple Mouse.Where Program Help
-----------------------------------

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
Mon Jun 07, 2010 7:43 pm

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
Mon Jun 07, 2010 7:49 pm

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.

K thanks so much its working now, one last question, how do I add gravity?

-----------------------------------
TheGuardian001
Mon Jun 07, 2010 8:08 pm

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
Mon Jun 07, 2010 8:58 pm

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?

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
Tue Jun 08, 2010 8:17 am

RE:Simple Mouse.Where Program Help
-----------------------------------
anyone? I need help with gravity

-----------------------------------
Cezna
Tue Jun 08, 2010 10:54 am

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.

-----------------------------------
V1RU5
Tue Jun 08, 2010 11:27 pm

Re: 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.

SO basically I make gravity a constant variable and make it equal to 0.7 which means it is a real number? If that is correct, then I am not able to subtract gravity form y because y is an int and gravity is a real number so it does not let me do that. What am I to do in order for this to work?

-----------------------------------
Zren
Wed Jun 09, 2010 7:40 am

RE:Simple Mouse.Where Program Help
-----------------------------------
Well you could just make y a real number. Or just subtract by 1 every frame instead. Keep in mind that gravity will combat your up movement. If you do this though, your guy's just gonna fly up in so long as you hold the mouse down instead of jump.

If you want it to be more "jumpy". When you jump, it's more of an arc, the speed of which you jump from the ground is really fast, but as you get higher, gravity takes hold until you hit a point where your floating, before gravity slowly makes you speed faster and faster towards the ground. So the speed that which you travel changes from positive, to 0, to negative as you fall. We can represent this as another variable. (Velocity is just a physic-y word for speed with direction.)

[code]x, y
xVelocity, yVelocity = 0

for every frame:
	if jump then yVelocity = jumpStartingSpeed
	yVelocity -= Gravity

	x += xVelocity
	y += yVelocity
[/code]

For this purpose, you don't really need xVelocity, but it's better to stay consistent with variables of the same ... genre (in this case, coordinates for a blob). Also, your gonna want a variable to single when you can jump and can't, otherwise you can jump in the air or hold down the jump button and fly up.

-----------------------------------
V1RU5
Wed Jun 09, 2010 8:23 am

RE:Simple Mouse.Where Program Help
-----------------------------------
So here is what I did


var xPos : int
var yPos : int
var button : int
var x : int := 50
var y : int := 50
var gravity : int := 1
var xVelocity, yVelocity : int := 0
const tennisBall := Pic.FileNew ("tennis ball.bmp")

View.Set ("offscreenonly")     % part of the common anti-flicker fix
loop
    % update ball position here
    Mouse.Where (xPos, yPos, button)

    if button = 1 then
        if y > yPos then
            y -= 2
        elsif y < yPos then
            y += 2
        end if
        if x > xPos then
            x -= 2
        elsif x < xPos then
            x += 2
        end if
        
        yVelocity := 2
        yVelocity -= gravity
        x += xVelocity
        y += yVelocity
    end if

    if y > 0 then
    y -= 0
    end if
    % draw ball here
    %Pic.Draw (tennisBall, x, y, picMerge)
    Draw.FillOval (x, y, 10, 10, blue)

    % update screen, then clear it for the next frame
    View.Update ()
    Draw.Cls ()
end loop


Okay I did not notice but I am having the problem again that it goes up on the y-axis and then right or left on the x-axis but it does not go diagonally. I do not know how to fix this. Does "if jump" refer to "if button = 1"? Also, I guess the jumpStartingSpeed is 2.

-----------------------------------
Zren
Wed Jun 09, 2010 9:40 am

RE:Simple Mouse.Where Program Help
-----------------------------------
Your code is going diagonally. It's just going diagonally really fast after the last mods you made, then moving the rest of the way horizontally. Your only moving in 8-degrees of movement (similar to zelda on the gameboy movement).

Can I ask you a question, how do you control jumping? Do you run horizontally then jump when you click above the character? Do you plan to have you press a button on the keyboard?

-----------------------------------
V1RU5
Wed Jun 09, 2010 5:58 pm

Re: RE:Simple Mouse.Where Program Help
-----------------------------------
Your code is going diagonally. It's just going diagonally really fast after the last mods you made, then moving the rest of the way horizontally. Your only moving in 8-degrees of movement (similar to zelda on the gameboy movement).

Can I ask you a question, how do you control jumping? Do you run horizontally then jump when you click above the character? Do you plan to have you press a button on the keyboard?

NVM i fixed this now it works, thanks to everyone who helped. I might have more questions later then I will ask but thanks for now :)

-----------------------------------
Cezna
Wed Jun 09, 2010 6:24 pm

RE:Simple Mouse.Where Program Help
-----------------------------------
You need to have the x and y co-ordinates of the object as real numbers, and instead of putting:

drawfilloval (x, y, radius, radius, colour)


you should have


drawfilloval (round (x), round (y), radius, radius, colour)


Having real co-ordiantes, even though they are being rounded before being used for the drawing, will allow for smoother movement.

With real co-ordinates:
Let's say that y velocity is -2.4, and y is 10.
First time around, y would become 7.6, which would be drawn at 8.
Second time around, y would still be 7.6, so y now becomes 5.2, which is drawn at 5.

With integer co-ordinates:
Let's say that y velocity is -2, and y is 10.
First time around, y would become 8, which would be drawn at 8.
Second time around, y be 8, so y now becomes 6, which is drawn at 6.

You can see how this would limit the possible speeds you can move at.
Using real co-ordinates for moving objects, and rounding them to draw is thus far superior to using integer co-ordinates, since normally the animations would be going to fast that you won't notice the difference between rounding real co-ordinates and actually drawing at real co-ordinates (which is impossible in Turing, I just mean to say it creates the same effect as drawing at real co-ordinates would when run fast enough).

