Alice Community

Alice Community (
-   How do I...? (
-   -   Limit my object to only go on the screen where I can see it.. (

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,

Thanks for all your help!

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?

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

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

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?

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:


I don't have a program that actually applies this since I have limited knowledge in the subject, however it basically goes into using vector and trigonometric math to create a 3D (right-angle) triangle to detect whether the object is seen by the camera.

On a side note, I'm going to warn you, Calculus is a tough subject, but it's a really good course to take. I ended up getting 66.6% in the course and 68.2% on the final, which is a fairly good score considering the many topics covered in the year.

@Dameria: Mine was taking into account if there were walls and obsticals that were going to be in the way, which is why it's a lot more complicated :p

All times are GMT -5. The time now is 06:59 PM.

Copyright ©2019, Carnegie Mellon University
Alice 2.x 1999-2012, Alice 3.x 2008-2012, Carnegie Mellon University. All rights reserved.