Computer Science Canada Simple Mouse.Where Program Help |
Author: | V1RU5 [ 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)
Please specify what version of Turing you are using 4.1.1 |
Author: | DemonWasp [ 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:
This gives the illusion of animation and makes it obvious where you should put the logic controlling the position of the ball. |
Author: | Cezna [ 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:
I recommend setting gravity at something like .7 or .8, but anything above 1 will be pretty substantial |
Author: | V1RU5 [ 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:
This gives the illusion of animation and makes it obvious where you should put the logic controlling the position of the ball.
I don't get what I am doing wrong |
Author: | TheGuardian001 [ Mon Jun 07, 2010 5:54 pm ] | ||||||
Post subject: | Re: Simple Mouse.Where Program Help | ||||||
V1RU5 wrote:
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:
The above code is exactly the same as:
It's just a short form. |
Author: | V1RU5 [ 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:
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:
The above code is exactly the same as:
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
|
Author: | USEC_OFFICER [ 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. |
Author: | TheGuardian001 [ Mon Jun 07, 2010 7:14 pm ] | ||||||
Post subject: | Re: Simple Mouse.Where Program Help | ||||||
V1RU5 @ Mon Jun 07, 2010 7:03 pm wrote:
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:
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:
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. |
Author: | V1RU5 [ Mon Jun 07, 2010 7:35 pm ] | ||
Post subject: | RE:Simple Mouse.Where Program Help | ||
K now it goes up first and then goes right, how do I make it go diagonally. |
Author: | TheGuardian001 [ 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. |
Author: | V1RU5 [ 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? |
Author: | TheGuardian001 [ 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? |
Author: | V1RU5 [ 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 |
Author: | V1RU5 [ Tue Jun 08, 2010 8:17 am ] |
Post subject: | RE:Simple Mouse.Where Program Help |
anyone? I need help with gravity |
Author: | Cezna [ 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. |
Author: | V1RU5 [ Tue Jun 08, 2010 11:27 pm ] |
Post subject: | Re: RE:Simple Mouse.Where Program Help |
Cezna @ Tue Jun 08, 2010 10:54 am wrote: 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? |
Author: | Zren [ Wed Jun 09, 2010 7:40 am ] | ||
Post subject: | 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.)
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. |
Author: | V1RU5 [ Wed Jun 09, 2010 8:23 am ] | ||
Post subject: | RE:Simple Mouse.Where Program Help | ||
So here is what I did
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. |
Author: | Zren [ Wed Jun 09, 2010 9:40 am ] |
Post subject: | 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? |
Author: | V1RU5 [ Wed Jun 09, 2010 5:58 pm ] |
Post subject: | Re: RE:Simple Mouse.Where Program Help |
Zren @ Wed Jun 09, 2010 9:40 am wrote: 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 |
Author: | Cezna [ Wed Jun 09, 2010 6:24 pm ] | ||||
Post subject: | 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:
you should have
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). |