TI Basic Bouncing Ball Animation - ti-basic

I am trying to make a bouncing ball animation in TI Basic and The ball is leaving a trail, and I can't figure out how to clear the graph every second. Is there a solution to this problem, if so please post it.
My Code:
ClrDraw
AxesOff
0->Xmin
0->Ymin
94->Xmax
62->Ymax
Xmax/2->X
Ymax/2->Y
1->A
1->B
Line(0, Ymax, Xmax, Ymax)
Line(0, Ymax, 0, 0)
Line(0, 0, Xmax, 0)
Line(Xmax, Ymax, Xmax, 0)
While 1
If X<1 or X>Xmax-3
Then A*-1->A
End
If Y<1 or Y>Ymax-3
Then
B*-1->B
End
Line(X,Y,X+2,Y)
Line(X,Y+1,X+2,Y+1)
Line(X,Y+2,X+2,Y+2)
X+A->X
Y+B->Y
End

You have two options to clear the ball after every frame:
You can either run ClrDraw every frame, before you draw the ball on that frame, or
you can run the code to draw the ball before you update the ball's coordinates, but instead of drawing with a black pen color, you can draw with white or set the erase flag, to erase the ball. There's an optional 5th argument to Line() that if it's set to 0, will erase your line instead of drawing it.
You can use either to remove the "after-image" the ball keeps, but ClrDraw will erase the whole screen, and keep nothing that you had there before, as opposed to the erasing Line() technique, which will only erase the ball.
The code, however, is up to you to implement.

One option that I have used in the past is to render blank all around the object's sides that are leaving trails.
I believe it's the left and top that leave trails, so you can simply draw a blank nothing to the left and above where the ball is rendered, effectively erasing the trail on the fly.
Another option is to use ClrDraw every iteration, but this is highly inefficient on the processor, and will slow down the animation.

You can use ClrDraw, however, it slows the program tremendously, so it is better to use redraw the ball with the last argument as 0 to clear traces, and only use ClrDraw once before the main loop.

Related

Processing: how to detect collisions when one object is traveling 'too fast'?

I have two objects: one is a ball, whose velocity is given by a vector of components velocity.x and velocity.y, such that, at every frame, the (x, y) position of the ball gets updated to (x + velocity.x, y + velocity.y); the other is a very thin horizontal rectangle that cannot move. Of course, I'd love the ball to change its trajectory whenever it hits the rectangle either from above or from below.
However, I've run into a problem I don't know how to fix: whenever the ball travels fast enough to go from one side of the rectangle to the other side in just one frame, the collision isn't detected because the ball never actually comes in contact with the rectangle. Being the rectangle very thin, the minimum velocity beyond which this behaviour occurs isn't even too high.
I know this is a problem that usually occurs when you want to keep an object inside the screen: in fact, if you just change the sign of its velocity as soon as it exits the screen, this doesn't necessarily make your object rebound off the side of the window (because it could travel so fast that a part of it gets stuck outside the edge), so you actually have to reposition it inside the window first, and then change the sign of its velocity accordingly. But I cannot use this trick here, because the behaviours of the ball on the two sides of the rectangle are supposed to be different: if the ball hits it from above, it will bounce upwards; if from below, it will bounce downwards. But when the ball manages to go past the rectangle in just one frame, how can I tell the program which side the ball should be repositioned? After all, once that frame is drawn, the program isn't able to know whether the ball came 'from the other side' of the rectangle, or if it's always been on that side.
I hope I managed to explain my problem clearly enough. What can I do to solve it?
Take the previous position of the ball and the current position of the ball, and create a line. Use that line to perform collision detection instead of the ball itself. Test for collision detection between the line and the rectangle.
This is going to be a bit more complicated than testing for collision detection between a circle and a rectangle, but googling "line rectangle collision detection" will return a ton of results. Basically you'll want to break the rectangle down into 4 lines and then check whether each line intersects your path line.
This question might help: How do you detect where two line segments intersect?

How to move all pixels down?

I drew multiple white pixels on a black canvas to get a night sky. I gave the stars random positions and now want that all pixels move down in-order to imitate the movement of the earth.
I tried Translate but that doesn't seem to work with pixels.
Is there a way to move all the Pixels in the canvas down?
arrayCopy(pixels, 0, pixels, width, (height - 1) * width);
Should solve the problem you have. For more help about arrayCopy look here: https://processing.org/reference/arrayCopy_.html
Basically, the process of creating an animation is this:
Store your state in variables, or in a buffer.
Use those variables to draw your scene every frame.
Change those variables over time to change your scene.
One approach is to draw your stars to a buffer. The createGraphics() function is your friend. Then draw that buffer to the screen using the image() function. Then move the y position of the buffer down by some amount each frame.
Another approach is to store your star positions in a set of variables, such as an ArrayList of PVector instances. Draw those positions to the screen, and move each one down a bit each frame.
The translate() function should work fine for points, and it's just another approach to the steps I outlined above. As is Tobias's answer. There are a bunch of different ways to do this. If you're still having trouble, please post a MCVE in a new question post. Good luck.
Shameless self-promotion: I wrote a tutorial on creating animations in Processing available here.

User interaction in Processing

I have a general question (I know I should present specific code with a problem, but in my case the problem is of a more general nature).
In Processing, let's say I make an ellipse:
ellipse(30, 30, 10, 10);
Now, is there a way to get the pixels where this ellipse is on the canvas? The reason would be to have a way of creating user interaction with the mouse (for instance). So when someone clicks the mouse over the ellipse, something happens.
I thought of turning everything into objects and use a constructor to somehow store the position of the shape, but this is easier said than done, particularly for more complex shapes. And that is what I am interested in. It's one thing to calculate the position of an ellipse, but what about more complex shapes? Are there any libraries?
Check out the geomerative library. It has a way to check whether the mouse is inside any SVG shape. I can't remember off the top of my head but it works something like you make a shape:
myShape = RG.loadShape("shape.svg");
and a point:
RPoint p = new RPoint(mouseX, mouseY);
and the boolean function contains() will tell you if the point is inside the shape:
myShape.contains(p);
It's better to use a mathematical formula than pixel-by-pixel checking of the mouse position (it's much faster, and involves less code).
For a perfect circle, you can calculate the Euclidean distance using Pythagoras' theorem. Assume your circle is centred at position (circleX,circleY), and has a radius (not diameter) of circleR. You can check if the mouse is over the circle like this:
if(sq(mouseX-circleX)+sq(mouseY-circleY) <= sq(circleR)) {
// mouse is over circle
} else {
// mouse is not over circle
}
This approach basically imagines a right-angled triangle, where the hypotenuse (the longest side) runs from the centre of the circle to the mouse position. It uses Pythagoras' theorem to calculate the length of that hypotenuse, and if it's less than the circle's radius then the mouse is inside the circle. (It includes a slight optimisation though -- it's comparing squares to avoid doing a square root, as that can be comparatively slow.)
An alternative to my original mathematical answer also occurred to me. If you can afford the memory and processing power of drawing all your UI elements twice then you can get good results by using a secondary buffer.
The principle involves having an off-screen graphics buffer (e.g. using PGraphics). It must be exactly the same size as the main display, and have anti-aliasing disabled. Draw all your interactive UI elements (buttons etc.) to this buffer. However, instead of drawing them the normal way, give each one a unique colour which it uses for fill and stroke (don't add any text or images... just solid colours). For example, one button might be entirely red, and another entirely green. Any other RGB value works, as long as each item has a unique colour. Make sure the background has a unique colour too.
The user never sees that buffer, so don't draw it to the screen (unless you're debugging or something). When you want to detect what item the mouse is over, just lookup the mouse position on that off-screen buffer. Get the pixel colour at that location, and match it to the UI element.
After you've done all that, just go ahead and draw everything to the main display as normal.
It's worth noting that you can cut-down the processing time of this approach a lot if your UI elements never (or rarely) move. You only need to redraw the secondary buffer when something appears/disappears, animates, or changes size/position.

Collision detection on diagonal lines in cocos2d

I'm working on a small iPhone/iPad game with the Cocos2d framework. Basically the idea of the game is very simple, there a ball which is moving at a certain speed. The user can draw a line so that the ball bounces in the opposite direction.
Now I've already have the ball moving and the user can draw a line also. (When he draw a line it becomes a member of an array and dissapears a few seconds later.)
But the question is how to detect a collision between the ball and a line? The line doesn't need to be horizontal or vertical, it can be diagonal also. It all depends on how the user draws this line.
I've the coordinates of the line: see the answer to my previous question: CGRect with an angle
Can you guys point in me right direction?
Thanks in advance!
You can use box2d to detect collisions. Actually, if your ball and these lines will have bodies in b2World, you will be able not to check collisions by yourself. All changes, bounces, etc. physics will done for you. You will only have to synchronize object's view position (also you can synchronize angle) according to the position of it's body in physical world.
You can use the Pixel perfect collision so when ball and line collide it is not check the rect but it check the pixel collision...
in this forum you got that you want..
http://www.cocos2d-iphone.org/forum/topic/18522/page/3

JavaFX timeline inaccuracies

I have two timelines in JavaFX that moves an image on the screen. One is in the onMouseEntered function to move the image left, and the other in the onMouseExited function to move the image back to the right. The keyframe has translateX starting at 0 and going to 10, and the other starts at 10 and goes to 0. The problem is that the two timelines don't move the image the same distance, so that if you activate it several times it starts creeping to the right. Is there something I'm missing here? How do I set up an animation so that it moves the same distance forward and backward?
Figured it out, the return trip should make translateX go to 0, not to -10

Resources