Alice Community (http://www.alice.org/community/index.php)
-   How do I...? (http://www.alice.org/community/forumdisplay.php?f=16)
-   -   Limit my object to only go on the screen where I can see it.. (http://www.alice.org/community/showthread.php?t=4469)

 spatu1a 05-21-2010 10:00 AM

Limit my object to only go on the screen where I can see it..

Hey all~ I'm new to the alice community, but I need help badly!
I'm creating a pretty simple game in which you control two ships at the same time, each on their own side of the screen as they pass through barriers (I've already split the screen into two.)
The problem I'm encountering is I don't know how to limit how far left and right the ships travel. (You can only control left and right movement for each) Right now, the user can move the ship completely off the screen.. which I don't want to happen. Can you suggest how to fix this problem?

Additionally, I would love to be able to host my game on the web... how do I do that?

 spatu1a 05-21-2010 12:13 PM

What exactly I'm looking to do..

Essentially, what I'm looking for is to limit the object to the camera's view.

 Niteshifter 05-22-2010 02:20 AM

You could implement some kind of vector system using variables to keep track of the movements.

There's also a few algorithms to determine the field of view of a camera, although I don't think there's anyone able to completely help you with finding this out. There are some really good tutorials and guides on how to do this. I can also tell you that you'll need a bit of experience programming with trigonometry in order to make the field of view algorithms work.

 Dameria 05-22-2010 10:32 AM

1 Attachment(s)
I just made this really quickly, its an example of how you can stop something from going out of the camera field of view by pushing it forwards. This might not be exactly what you were looking for, but I don't know what you want, you would have to let me see your project for me to assess the best way to go about doing it.

All I did was put 4 cubes in the world (one for the left of the screen, one for the right, one for the front near the camera, and one as the object that is moving), and I made a simple infinite loop determining if the distance that the object is in front of each square, and if the distance is less than 0, meaning it is at the edge of the camera screen, it moves the object forwards into the field of view at a faster rate than the object itself is moving (for instance if it is moving 0.05 meters in 0 seconds, it pushes the object forward 0.1 meters so that it can not wiggle its way through the barrier). This is essentially the same thing as collision detection, but instead the walls (the half invisible black cubes) are vehicle to the camera so that they move wherever the camera goes. You can also move the camera with the mouse (move slowly to see best results) and you will see that when you move the camera so that it does not look at the cube, the cube will follow the camera and try as hard as it can to get back into the camera's view (it can only go 0.1 meters at a time though, so moving the camera too fast will make the cube go out of the view until it can catch up the the camera again).

Tell me if you need any further help or if this answered your question.

 spatu1a 05-22-2010 11:28 AM

1 Attachment(s)
Hey Niteshifter and Dameria,

Niteshifter:
I'm going into calculus next year, but I'm not sure what you mean by a vector system. Also, I know trigonometry but I have no experience programming with it, and I'm not sure how it would apply here. Do you have a program with this in it?

Dameria:
I've attached my program for you to have a look at. Note that I haven't begun the part where the levels actually start; I'm just getting all the basics done. The user will NOT be able to control vertical movement, and the plane should not move on the z-axis, but only be cappable of movement left and right, on the x-axis. I think I understand what you are doing in the other program you made, but I noticed that the square wiggles away from the camera- I don't know how to stop that.. Hopefully the program can make it clearer what I'm looking for~

Thanks again for all your help so far!

 Dameria 05-22-2010 12:58 PM

1 Attachment(s)
I think that niteshifter is going into way too much advanced math than he needs to, there are much simpler ways than using trigonometry, for example I just solved your problem with a simple set of inequalities, which is learned in Algebra 1. I made it so that there is a world variable called "canMove" and I set it to false, and it will only be set to true once you click on the text to start the game. I also made the collision that you asked for, you can see what I did in the world.stayInScreen method. If for some reason the spaceships can still go past the barriers slowly, just change the 0.05 meters in each of the lines of code in "stayInScreen" to something a little higher, like 0.06 or more, but if it is good, just keep it the way it is (also the other way around, if it gets pushed back too quickly and it is choppy, you can lower the number until you find the one that gives the best result).

Tell me if this is what you wanted (it was much simpler than I thought it was going to be).

 spatu1a 05-22-2010 04:47 PM

Dameria-
That works fantastically- don't know why I didn't manage to think of that before. Thanks so much- will let you know what the final product looks like!

 Dameria 05-22-2010 10:48 PM

Your welcome, and I look forward to seeing the finished product.

 Niteshifter 05-23-2010 02:31 AM

[QUOTE=spatu1a;21608]
Niteshifter:
I'm going into calculus next year, but I'm not sure what you mean by a vector system. Also, I know trigonometry but I have no experience programming with it, and I'm not sure how it would apply here. Do you have a program with this in it?
[/QUOTE]

I have a vector system in my slide puzzle game that keeps track of the location of the cursor that you can check out and see how it works: