PDA

View Full Version : alice object question


Chaosbubba
03-29-2008, 02:34 PM
Aight im a little confused :P im doing a project for my class to create a "put put game" using alice. im doing pretty good on it untill i got to a part where i need to make an object move once the "ball" gets close to it. I can make the object move or preform and action only when the ball stops at the object. Just to give you a little more info, i am making a tennis ball (used as golf ball) run into a tennis racket and the tennis racket spin and shoot the ball off in a random direction. Unfortunately i can only get the tennis racket to spin and shoot the ball off in a random direction if the ball stops near the racket. I need the tennnis racket to spin and shoot the ball off even if the ball is moving through it and it would be even more amazing if i could get the ball to stop when it hits the racket then shoot it off.

lanceA
03-29-2008, 04:52 PM
Spelling and sentence structure are often prerequists for college. You might wish to practice these skills.

You have stated your problem in a somewhat very generic manner. What specifically are you having problems with?

If I am understanding your problem it appears that you first need to create a collision() method to determine if the tennis ball has met the tennis racquet. Once this function returns true then you need a method that accepts this returned value and deals with it appropriately.

For example, you could have a method, ballMeetsRacquet() which simply uses the world function, random number, and then based upon the number returned, calls the ball's move method to cause the ball to move in the appropriate direction based upon the value returned by the random number function.

Good luck,

Chaosbubba
03-29-2008, 05:12 PM
Collision method? That would require that the ball actually stop upon hitting the racket wouldn't it? At the present, the ball just goes straight through the racket.

Moonsword
03-29-2008, 10:29 PM
I suspect that Chaosbubba and I are in the same class, and in any case, I seem to be running across much the same problem.

The problem here is setting up the method itself. Either the 'distance to' function needs to be kept out of an If/Else statement's conditional, or it's measuring from the origin of an object to the origin of another object, not the 'outside' of the mesh to the outside of the mesh, which is how a human would measure distance. Essentially, we're both having trouble getting Alice to detect that a collision has occurred between two objects. Any advice on how to set that up?

DickBaldwin
03-29-2008, 11:10 PM
I suspect that Chaosbubba and I are in the same class, and in any case, I seem to be running across much the same problem.

The problem here is setting up the method itself. Either the 'distance to' function needs to be kept out of an If/Else statement's conditional, or it's measuring from the origin of an object to the origin of another object, not the 'outside' of the mesh to the outside of the mesh, which is how a human would measure distance. Essentially, we're both having trouble getting Alice to detect that a collision has occurred between two objects. Any advice on how to set that up?

If you will go to Google and search for the following keywords:

collision site:www.alice.org

you will find about 250 postings on collision detection. This is probably the most often discussed topic on the Alice 2.0 forum.

Good luck in solving your problem.
Dick Baldwin
Free Alice tutorials: http://www.dickbaldwin.com/tocalice.htm
Free programming tutorials: http://www.dickbaldwin.com/toc.htm

Moonsword
03-30-2008, 12:12 AM
EDIT: Never mind. I managed to find something after refining the search queries. Apparently, the if/else was fudging things. >.>

EDIT 2: For the record, Alice measures the distance between objects from the origin of said objects' bounding box, not edge of mesh to edge of mesh. Knowing this makes it much easier to understand why "collision detection" doesn't seem to work even when the code is right.

DickBaldwin
03-30-2008, 07:33 PM
EDIT: Never mind. I managed to find something after refining the search queries. Apparently, the if/else was fudging things. >.>

EDIT 2: For the record, Alice measures the distance between objects from the origin of said objects' bounding box, not edge of mesh to edge of mesh. Knowing this makes it much easier to understand why "collision detection" doesn't seem to work even when the code is right.

I haven't given collision detection in Alice a lot of thought and I may be completely wrong on this, but I believe that the collision detection issue is much more significant than simply how the distance between two objects is measured.

One of the great things about Alice 2.0 is that you can cause objects to perform long smooth motions with a single statement.

One of the bad things about Alice 2.0 is that you can cause objects to perform long smooth motions with a single statement.

Consider a baseball batting practice scenario. Assume that there is a pitching machine on the pitcher's mound and the distance from the machine to the backstop is known. A single statement can be used to cause the baseball to move from the mound to the backstop. A second statement could be used to cause it to drop to the ground when it reaches the end of its trip and hits the backstop.

Now consider what happens if the batter manages to put the bat in front of the baseball. Too bad, the baseball will go right through the bat. As far as I know, no amount of computation involving the distance of the ball from the bat will prevent the ball from completing its journey once that journey begins.

Therefore, when there is a need to detect a collision between two objects, it is necessary to use a series of short motions (probably in a while or for loop) instead of a single motion. That's OK, we do that all the time in other programming languages. However, Alice gets very grumpy when you force her to execute hundreds and possibly thousands of statements in a short period of time. The smaller you make the incremental motion, the more precise you can be in making the distance measurement necessary to detect a collision between the ball and the bat. Unfortunately, decreasing the length of the incremental motion also increases the computational load on Alice.

As an example of what can happen when you are forced to chop a large motion into a series of very small motions in Alice, see the trajectory programs at http://www.dickbaldwin.com/alice/Alice0930.htm

One program uses the equations of motion to make very small incremental movements of a projectile in order to produce a reasonably accurate representation of the trajectory of a projectile. That program doesn't run very smoothly on my machine, but granted, my machine isn't particularly fast.

Another program at that same site approximates the trajectory using a circular arc that is implemented with a single statement. While that program is not true to the equations of motion, it runs very smoothly on my machine. The conclusion is that we need to take advantage of the built-in motion capabilities of Alice whenever possible, but those built-in motion capabilities are generally incompatible with collision detection.

I would really like to be proven wrong on this. If anyone knows how to interrupt the movement of an Alice object after it has begun, I would like to hear how to do it.

Dick Baldwin
Free Alice tutorials: http://www.dickbaldwin.com/tocalice.htm
Free programming tutorials: http://www.dickbaldwin.com/toc.htm

DrJim
03-31-2008, 10:37 AM
If anyone knows how to interrupt the movement of an Alice object after it has begun, I would like to hear how to do it.


See the attached.

The mad scientist walks only when the world level variable Walk is true. Walk in turn is toggled by the spacebar with an “event when.”

Note it is important not to use an “event while” here – in the current version of Alice, that gets evaluated independent of the loop and can leave the loop index in an undefined state (and crash the program).

As shown, the while is evalated inside the loop and the method called executes in a fixed time, so the problem doesn’t occur.

(Note: I just used the canned method for the walk, however you can also use "move at speed" inside the while if you want to control the objects direction, etc. in addition to a simple start/stop. That will just keep an object moving forward - change directions by turning it.)

DickBaldwin
03-31-2008, 11:20 AM
See the attached.

The mad scientist walks only when the world level variable Walk is true. Walk in turn is toggled by the spacebar with an “event when.”

Note it is important not to use an “event while” here – in the current version of Alice, that gets evaluated independent of the loop and can leave the loop index in an undefined state (and crash the program).

As shown, the while is evalated inside the loop and the method called executes in a fixed time, so the problem doesn’t occur.

(Note: I just used the canned method for the walk, however you can also use "move at speed" inside the while if you want to control the objects direction, etc. in addition to a simple start/stop. That will just keep an object moving forward - change directions by turning it.)

Thanks for the reply. I could probably use your approach and figure this out on my own, but let me simply ask you since you probably already know the answer.

Assume that I have two spheres separated by 10 meters. I execute a single move command on each sphere causing it to move directly toward the other sphere with a specified movement distance for each sphere of 20 meters and a specified duration of five seconds for each sphere. Given this code alone, the two spheres will pass right through one another at approximately 2.5 seconds and will end up separated by 10 meters, but they will have swapped positions.

Is there a way to write and execute code that will monitor the distance between the centers of the two spheres and cause both of them to stop without human intervention when the distance between them is less than one meter? In that case, their centers will end up separated by slightly less than one meter in a little less than 2.5 seconds and neither sphere will have completed its trip of 20 meters originally specified by the two move commands.

If so, is there any requirement to do any cleanup as a result of neither the overall distance specifications nor the overall time specifications having been satisfied?

Thanks,
Dick Baldwin
Free Alice tutorials: http://www.dickbaldwin.com/tocalice.htm
Free programming tutorials: http://www.dickbaldwin.com/toc.htm

DrJim
03-31-2008, 03:57 PM
Hey, I’m retired. :cool: Don’t you have a class or a TA that you can assign this kind of stuff (noticed that was a requirement for being a Beta tester for 3.0)?

Think the attached isn’t exactly what you asked for but does generally what you wanted – didn’t work out detailed times or distances or add the logic for the “miss” case. The only thing I know that has to be reset is the subsequent poistions of the spheres.

Also had a bit of a trouble with drag and drop for the test. Ended up first defining a Boolean variable and then draging its argument into the while and deleting the variable itself – not an unusual thing to have to do in Alice.

DickBaldwin
03-31-2008, 07:04 PM
Hey, I’m retired. :cool: Don’t you have a class or a TA that you can assign this kind of stuff (noticed that was a requirement for being a Beta tester for 3.0)?

Think the attached isn’t exactly what you asked for but does generally what you wanted – didn’t work out detailed times or distances or add the logic for the “miss” case. The only thing I know that has to be reset is the subsequent poistions of the spheres.

Also had a bit of a trouble with drag and drop for the test. Ended up first defining a Boolean variable and then draging its argument into the while and deleting the variable itself – not an unusual thing to have to do in Alice.

Hi Jim,

Thanks for the response. I will need some time to study it.

Unfortunately, we don't have TAs at the college where I teach. Apparently that luxury is reserved for wealthy universities such as CMU. So, they found a way to eliminate me as a beta tester for V3.0, :mad:which doesn't make me particularly happy.

Dick Baldwin
Free Alice tutorials: http://www.dickbaldwin.com/tocalice.htm
Free programming tutorials: http://www.dickbaldwin.com/toc.htm

DickBaldwin
03-31-2008, 07:21 PM
Hey, I’m retired. :cool: Don’t you have a class or a TA that you can assign this kind of stuff (noticed that was a requirement for being a Beta tester for 3.0)?

Think the attached isn’t exactly what you asked for but does generally what you wanted – didn’t work out detailed times or distances or add the logic for the “miss” case. The only thing I know that has to be reset is the subsequent poistions of the spheres.

Also had a bit of a trouble with drag and drop for the test. Ended up first defining a Boolean variable and then draging its argument into the while and deleting the variable itself – not an unusual thing to have to do in Alice.

It works great. So, collision detection in Alice isn't as complicated as I had been led to believe.

Thanks,
Dick Baldwin

DrJim
03-31-2008, 11:01 PM
So, collision detection in Alice isn't as complicated as I had been led to believe.


I think that reputation started in part because, unlike some of the game oriented programming systems and gaming engines, there is no specific method or function in Alice to do the tasks - you have to make your own.

There were also a group of maze games being developed at one time, with which the developers initially were having trouble figuring out how to identify collisions with walls. It might be worth a search specifically on "maze" and "collision" for references to some of that work.

Finally, like animating walks, it's something that is difficult to do well - regardless of the language you are programming in. The maze guys, in particular, choose a very difficult, but great looking, setup.

DickBaldwin
04-01-2008, 09:08 AM
I think that reputation started in part because, unlike some of the game oriented programming systems and gaming engines, there is no specific method or function in Alice to do the tasks - you have to make your own.

There were also a group of maze games being developed at one time, with which the developers initially were having trouble figuring out how to identify collisions with walls. It might be worth a search specifically on "maze" and "collision" for references to some of that work.

Finally, like animating walks, it's something that is difficult to do well - regardless of the language you are programming in. The maze guys, in particular, choose a very difficult, but great looking, setup.

Actually, upon second thought, your program didn't satisfy my objective, which is to avoid having to execute a move method multiple times for very short moves within a loop. What I want to do is to execute a move command one time and specify a long time or distance and then be able to interrupt and stop that move midstream as a result of it colliding with another object. If that could be done, Alice programs involving the motion of many objects, such as billiard balls, would run much more smoothly.

Dick Baldwin
Free Alice tutorials: http://www.dickbaldwin.com/tocalice.htm
Free programming tutorials: http://www.dickbaldwin.com/toc.htm

DrJim
04-01-2008, 11:50 AM
Not completely sure what you’re asking for. To really check for collisions without doing incremental steps, I think you would have to use something like the old MathCAD to check for an intersection of two trajectories (something I know at least one "physics based" pool simulator does).

Consider the very simple case of two objects moving in 2D along the following paths:
Y= 1 + X
Y= 2 – X
These trajectories have a common point (and hence a collision) at X=1/2, Y= 1 1/2.

However, if the second object moves along the path Y= 2 + X, there is no common point and thus no collision.

This is a simple example to set up by hand or do with some algebra program like MathCAD (or even some scientific calculators) – but I really have no idea how to do it Alice :confused: without just incrementially tracing out the trajectories and doing comparisons.

Additionally, you do have to do the comparisons often enough so you don’t miss the collision – something very easy to do in Alice (and something covered quite well in one of texts, though I don't recall which one).

Think this last step is where the “art” in collision detection comes in. Is in worth making the testing routine smart enough to know in advance that a wall is always “behind” an object and thus there will never be a collision – or is it better just to move the object in small increments and test at each step? In the latter case, using a “move at rate” statement inside a “while” simplifies things – but if you make the duration of the “move” statement too long, you can miss the collision.

Note: Actually my pool simulator program has a rather simple - and accurate - collision test. Once I enter my player name, only the eight ball is allowed to "collide" with a pocket - and that event occurs randomly early in the game.

DickBaldwin
04-01-2008, 01:36 PM
What I would like to do would be great but may not be possible in Alice.

Consider the case of a battle game between one country firing ballistic missiles and another country firing interceptor missiles.

Solving and implementing the equations of motion for the trajectory of a single missile in small incremental steps is very demanding computationally. Solving and implementing the equations of motion for the trajectory of many missiles in small incremental steps is extremely demanding computationally and probably well beyond what is reasonable using Alice 2.0 on most computers.

However, assuming that a missile is pointing straight up, approximating the trajectory of the missile using a single statement something like:

turn forward, one-half revolution as seen by a point halfway between the missile and the target, duration = one second, begin smoothly and end abruptly

seems to be very economical from a computational viewpoint in Alice 2.0.

Therefore, approximating the trajectory of many missiles in this manner should be much less computationally demanding than solving and implementing the incremental equations of motion for each one of many missiles.

Also, when viewing the path of the missile in 3D with perspective, it is very difficult to tell that the circular arc produced by the above statement is different from the ideal trajectory of a missile based on the equations of motion for constant gravity.

Assume that 20 ballistic missiles will be fired. I would like to execute 20 different statements similar to the one above to cause the missiles to be fired toward their targets. In a video game war where the enemy doesn't have interceptor missiles, those 20 statements would cause the missiles to be launched and to land on their intended targets.

However, I want to also be able to have the other side launch interceptor missiles in an attempt to hit and destroy the incoming ballistic missiles. If an interceptor missile gets within a specified distance of a ballistic missile, both missiles must terminate the motion and disappear (or at least disappear mid-flight).

The objective is to be able to have many objects in motion at the same time without creating an excessive computational load, which is a real problem with Alice, especially when the Alice world is running on a relatively slow computer.

Another application for this capability would be in a game of pool where there are 15 pool balls and one cue ball all in motion at the same time with the potential of collisions between each pool ball and every other pool ball as well as collisions between each pool ball and any of the cushions on the four sides of the pool table.

In that case, it would be desirable to set each pool ball in motion in a straight line, in a specified direction, and with a specified velocity with the specified distance being the longest possible distance on the table (the diagonal distance from one corner to the other). If the distance between a ball and any other ball approaches zero, or the distance between that ball and any side cushion approaches zero, the ball would need to terminate its trip in that direction and begin a new trip for the diagonal distance in another direction.

In other words, I need to be able to write something like the following:

turn left or right by a specified angle
move forward 2 meters, duration = one second (but stop moving and be prepared to start a different trip in a different direction if a specified condition becomes true)

For example, I could easily write in Java (and place in a class library) a move method that accepts a reference to a very simple object containing a boolean instance variable and causes its behavior to depend on the value of that variable. For example, that method could implement a very complex corkscrew motion. The using programmer of the move method may not have any idea how to implement corkscrew motion, and wouldn't need to know anything about the internal implementation of the method. All that programmer would need to use the method would be an understanding of the user interface (and hopefully some user documentation, which is a major failing in Alice 2.0).

Just wishing.

Dick Baldwin
Free Alice tutorials: http://www.dickbaldwin.com/tocalice.htm
Free programming tutorials: http://www.dickbaldwin.com/toc.htm

DrJim
04-02-2008, 08:09 PM
Whew – you don’t want much.. I’ll take a look at this a bit more, but will probably put most of the notes in private messages since I’m afraid – with my current understanding – I’d cause more confusion to non-involved readers than I would help. If anyone else has ideas – please contribute. Good Alice examples at level of program development are sorely missing.

I believe you said your school was starting a game programming class – this might be an excellent thing to work with some of the people consulting for that. If you can find a book called something like “Object Oriented Game Programming with Blender” – it might also help.

To really have it look clean (or maybe even run at all with lots of objects), some of the computations will have to be offloaded to the graphics hardware (you can’t avoid the incremental computations for object display). The key is to make the hardware/software partition – which is far beyond the level of complexity I’ve seen addressed with Alice. (Even a decade ago, it was within the capability of a PC, Borland C++, a good Open GL graphics card and a good programmer – unfortunately I don’t fit in the last category, so that’s a problem.)

One final note is that I think this type of problem is where the lack of a true class in Alice 2.0 is really going to show up. :( It may be a good problem to start just to illustrate what Alice 3.0 can do (assuming it meets it’s design goals).