Hello everyone,
I am new to game programming, i am looking to code a 2-d side scroller which could do such things :
As the game begins, the ball starts rolling from left to right, and speeds up as the time goes on.
In its path, it faces obstacles like Triangles and Squares and combination of the two at random distances.
The player needs to jump the ball over the obstacles, suppose he hits the triangle he loses a life and stops if he hits a square and needs to jump over it, roll on the top and go ahead.
I need to code this very urgently, and i am confused in the proper C++ and ALLEGRO coding for
1. JUMP
2. Random Triangles and Squares.
Waiting for your replies.
Why is this urgent?
There are many threads in this forum on how to implement jumping in platformers. Try to do a search and see if that helps you.
What do you mean by random triangles and squares? Do you know how to generate random numbers?
Also: showing some code always helps.
I need to submit this in 4 days!
By random Triangles and Squares, i mean. when a ball rolls from right to left, triangles and squares and their combinations such as triangle-triangle, triangle-square etc are encountered as obstacles, and the player needs to jump over these to move ahead.
Yes i know the SRAND dunction, but how tu use it for these figures, is still cryptic.
I'll search again for JUMP, was looking for a simpler logic so as to understand it quickly and apply easily.
You can do jumping simply by having a vector to represent the velocity of a ball going in a direction. What you do is, when the player jumps, the Y value of the vector is increased. Each frame, the position of the ball increases by this vector. In addition, each frame the Y value of the vector is affected thus producing the parabola of jumping. To make sure the player doesn't keep jumping, you can figure out if he is on top of an object by finding the normal or some other hackish way. You only let the ball jump when it is on top of something.
For the other part, if your level is a plane, you could move by random increments along the plane and at each stop, if random number is 0, put triangle, else, put square or something.
So this is homework?
As I have recently been implementing some jump mechanics myself, I took the time to produce a short tutorial covering one technique on how to implement jumping. It's based on some code you could have found if you had done a search I hope it's of use to you!
I guess you meant the rand() function. If you can generate random numbers within a range, you should then be able to generate the coordinates for your triangles and squares, and also what order they should come in.
It's easier to help if you show some code, and explain in more detail what you're having problem with.
Thank you so much to both of you, will try and implement things and will post the code asap with the help needed.
PS : It is not a homework, i am an engineering graduate. So no more curriculum studies for now, but it is more of a demo kind of game, i need to submit. Your help may prove of a career paving path for me.
The code i have been stuck at :
PRESSING SPACE KEEPS THE BALL IN AIR!! REQUEST YOU TO CORRECT ME:-/
You really should do it with vectors. Your hack is hard to visualize. Also, it's not a good idea to multiply by a Boolean, because in theory a true Boolean only needs to be non zero, it does not have to be 1.
for a start you don't break after:
case ALLEGRO_KEY_ESCAPE: done=true;
Try using velocities (i.e. vectors) as I've done in my tutorial and other people have suggested.
I'm not sure if you want to be able to control the jumping height or not.
If not, then it's simply this:
when the player pressed up, set dy to a negative value.
if player is not on ground, increase dy with a positive value (gravity)
update y using dy: y += dy
If you want controlled jumping height, see my tutorial I posted.
wow... good luck man
The ball bounces, but if the jump key is pressed, it bounces in the mid air.
All Triangles are generated in a small place, i.e all 4 triangles show up overlapping or inside each other, no random distance!
if (pos_y >= 380) { // means the ball is on the ground right? if(keys[SPACE]) { if (jumping) { dy= -10; jumping = false; } } else jumping = true; } // a more proper name for the jumping variable would be canJump, or mayJumpAgain
Regarding the triangle problem, I would generate random numbers for just one xy-coordinate and create the other two vertices by using that coordinate. This way you can easily change the size of the triangles if you want to.
Whoa, thanks a lot The bounce is working now! But i am getting the obstacles of the game(have been testing only on triangles for now) all wrong. Have attached an image.
Have modified, only x1 of the triangle and its way better now, but i get overlapping triangles!!
{"name":"605625","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/2\/9235031d8a61a4cb19d0ed47e6b819cb.jpg","w":1015,"h":122,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/2\/9235031d8a61a4cb19d0ed47e6b819cb"}
void InitTriangle(Triangle shapeT[], int size) { for(int i=0; i<size; i++) { shapeT[i].ID = TRIANGLE; shapeT[i].pos_x1 = 100 + rand() % 600; shapeT[i].pos_y1 = 400; shapeT[i].pos_x2 = shapeT[i].pos_x1+20; shapeT[i].pos_y2 = 360; shapeT[i].pos_x3 = shapeT[i].pos_x1+40; shapeT[i].pos_y3 = 400; } }
Have modified, only x1 of the triangle and its way better now, but i get overlapping triangles!!
Therefore, you should..?
You have all the information you need to solve that problem: a list of all the generated triangles and their coordinates.
[EDIT]
I really hope somebody doesn't just give you the solution to this too. You need to start thinking for yourself.
[UPDATED]
I have been trying. I have now managed to make things scroll and triangles and squares coming, but i am really in confusion with 2 things :
1. Squares and Triangles OVERLAP each other(as seen in the image).
{"name":"605627","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/f\/efd5069bfe3772701fee009ee6d06eca.jpg","w":806,"h":194,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/f\/efd5069bfe3772701fee009ee6d06eca"}
2. How can i make the screen scroll? I mean, for ex mario: you keep on going and things keep coming!!
You scroll by moving the world around the player. The player is always at the center of the screen except at the left and right extents of the level.
It's easier to think of it in terms of a camera. You can look into ALLEGRO_TRANSFORMations for this: http://www.liballeg.org/a5docs/refman/transformations.html
If you were able to get a degree in Engineering, which involved maths far more complex than simple transformations, then the maths required to simulate a camera should be a breeze.
The idea of transformations is that you make the Player's position 0,0 on the screen which will cause your character to be at the center of the screen which consequently scrolls your props too.
If you need an example on transformations in Allegro, there is ex_transform.c that you can have a look at. It comes with the Allegro source code.
In theory, these 3 functions should do it for you:
ALLEGRO_TRANSFORM transform; al_identity_transform(&transform); al_translate_transform(&transform, x, y); al_use_transform(&transform);
It is up to you to figure out x and y.
Problem: Triangles overlap
What we want to achieve: Make them not overlap
Solution: Is there a way to see if they overlap and then do something about it?
So you're trying to remake the impossible game:
{"name":"images","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/7\/07445f55dd03592bdf31dede6f94f276.jpg","w":300,"h":168,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/0\/7\/07445f55dd03592bdf31dede6f94f276"}
Neil Walker : Why impossible ? What i am trying to code, already exists as an smart phone app. The game in the picture isnt the exact model of it.
Arvidsson : As in, you want me to paste the code? I am confused with the statement
"Solution: Is there a way to see if they overlap and then do something about it?"
jmasterx : will try it, have been into the overlapping issue.
As i added, the Ball move functions, for left, right and jump, the JUMP Logic seems to be not working. Cant figure out why?
Here's the updated code :
Trying to make a quick buck piggybacking off people here?
I doubt he'd label it "urgent" if he was trying to develop an app, it's homework that he procrastinated on.
That's why I said a "quick" buck.
Arvidsson : As in, you want me to paste the code? I am confused with the statement "Solution: Is there a way to see if they overlap and then do something about it?"
What Arvidsson meant is the same as I did: THINK. You can't expect other people to keep solving everything for you. Sooner or later, you're going to have to think for yourself. What he posted was a basic recipe to solving your problem.
I'll restate it in the form of three questions you should ask yourself:
What is the problem?
What are the conditions that cause the problem?
Now that you know what the problem is and how it occurs, how do you fix it?
No No No Sir.
Please dont get me wrong, and this isnt any home work, i need to code it by weekend in any case for then, i will miss an opportunity, that's it.
I have been just asking, since this is an allegro forum, and there are people like you who have hell lot of exp, while i am a total newbie.
If this is against, any rules, i am really sorry!
[EDIT]
I am trying to sir. But, for a first timer, it gets, little confusing at times.
Ok, I'll take your word. Sounds like something for a job interview. Good luck.
Yes exactly, thankyou sir.
Its my dream to be a game programmer, and this opportunity means a lot to me, got just 3 days left!
Sadly though, here in, we do not get much resources or motivation for Game Programming, so gets stiff.
Well try a little harder, and you'll get it done and get answers. Ask specific questions relating to your code, which you should post if it has changed.
For example "my triangles overlap, how do I fix it" is a pretty general question. You should try fixing it. You might be able to do it yourself (don't sell yourself short!) If you can't figure it out, show us the code where you tried to figure it out and someone can probably tell you how to fix your code.
People here just don't like giving out a 100% ready made solution, they want you to try and figure it out yourself first.
Ok, for the triangles: check the variables defining the positions of the triangles before you draw them.
Anyway, if game programming skills are crucial for the career opening, something says me you should wait for the opening 2 or 3 years and meanwhile improve your skills.
If I put something 10ft wide 7ft away from something else that is 10ft wide do you think they will touch....?
The London Symphony Orchestra has an opening in the Oboe section! I just got an Oboe! Quick, what do the buttons to!?
I'm not sure, but I'm thinking they change the pitch of the sound. Try putting the keys in an array called pitch[numNotes] or something, and then do something else blah blah blah yawn. Comon varynoob give us all hope with a custom solution....
Guys, look at this, everything seems to be ohkay, except the Ball hangs in the air, when Jump key is pressed. Before i made Ball Struct & functions for Ball Movement, ball was jumping fine. But now, there is this weird error with the same code.
It has something to do with the Draw function of the Ball, but i am not able to figure it out.
Looking to put in collision detection and spritesheets, so need functions for ball movement, such as MoveRightBall/Left/Jump.
This is the code i have changed
Neil Walker : Why impossible ?
Because that's the name of the game
The ball hangs in the air you say. Then you should ask yourself, what is the condition for the ball to fall. Could it be gravity? What is the condition for gravity to be applied? Is it ever true?
Arvidsson : Sir, after trying several times, got the mistake, corrected for the JUMP. Have been working on collisions.
[EDIT]
CIRCLE_TRIANGLE
Is this okay ?
if((shapeB.pos_X + shapeB.r > shapeT[i].pos_x1) && (shapeB.pos_X - shapeB.r < shapeT[i].pos_x3) && (shapeB.pos_Y + shapeB.r > shapeT[i].pos_y2 && shapeB.pos_Y - shapeB.r < shapeT[i].pos_y3))
[edit]
HOW DO I CHECK IN BALL(bounding rectangle)<->RECTANGLE, IF THE BALL HITS LEFT SIDE OF RECTANGLE, THE SCREEN PAUSES, ELSE IF BALL IS ON TOP EDGE OF SQUARE, IT ROLLS DOWN AND SCREEN KEEPS SCROLLING AND GAME CONTINUES!
Suggestions plz. Using this (not working):
Oh God, he's shouting now.
We'll never make the London Symphony.
*sniff... screw it! screw it all!
{"name":"605641","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/d\/6de24c7282296dab186ca05f9e5d0e22.jpg","w":268,"h":331,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/d\/6de24c7282296dab186ca05f9e5d0e22"}
What are you talking about? Got no idea!! Spamming ??
Anyone with Circle-Triangle & Circle-Rectangle collision detection simultaneous checking ? ?:-[
With your education, you should have no trouble figuring out a formula for each type of detection; even if it's inefficient, it will do.
Once you come up with some formulas, we will gladly guide you on how to turn that formula into C++ code.
If you can figure out if a point is in a circle, if a point is in a triangle, and if a point is in a rectangle, you should be able to figure out a way to do it.
You might find it easier to treat a circle as a rectangle because if the circle's radius is less than the box's height, you'll have to check if the circle collides with its edge.
Also, he is not spamming, this is the atmosphere at a.cc .
Mark, that guy in the background may want to see about getting his uni-brow waxed
varynoob, without trying to sound demeaning, it's very obvious that you are out of your depth here. If you do manage to get the help you need from people here in order to get the job, then what? It won't take long before your employer realizes that you don't have the necessary experience.
He's absolutely right I'm afraid, that's why I stopped giving you hints. Sorry, but maybe it's time to get realistic.
I have learnt from my mistakes sir, as everybody had told here, to try and try, and come up with things, i did the same.
Used Bounding box for circle, and bounding box of triangle to check collisions between the two, while the rectangle itself, has sides, so it is easy for that.
But what i am facing is, the collision comes out to be ok at times and weird at other times. So asked you of the flaw.
Here is the code.
PS : SO I HAVE BEEN TRYING, AND HAVE COME UP WITH THE COLLISION TECHNIQUES AS WELL.
IT IS ALL ABOUT MORE UP THAN BOTTOM, MORE BOTTOM THAN UP, MORE LEFT THAN RIGHT & MORE RIGHT THAN LEFT.
Why would you add/subtract bound_triangle_x from posy ??
Dizzy Egg : Typo sir! It was meant to be 'bound_triangle_y'.
I have been trying out different things, but havnt really landed into any good one, as in i want to achieve if Circle hits Left of square, everything freezes except the ball, which if made to bounce, should make things back normal and scrolling.
What i did is, checked the collision with the sides of square i.e Right of circle more right than square's left & Left of circle more left than Square's right.
If the condition is true, Leftcollision is true.
If Leftcollision is true, All the update functions are skipped, thus screen is freezed.
But this stuff doesnt do well for me. Code below :
if((shapeS[i].pos_rect_x1 -5 < shapeB.pos_X + shapeB.boundx)
...think about it; if shapeS[i].pos_rect_x1 = 20, and shapeB.pos_X = 50, this is ALWAYS going to be true! THINK ABOUT THE LOGIC!!!!!!!!
if((shapeS[i].pos_rect_x1 -5 < shapeB.pos_X + shapeB.boundx)
&&(shapeS[i].pos_rect_x1+ 40 > shapeB.pos_X - shapeB.boundx)
But then, this condition also has an && thus if 1st is always true, the 2nd will only be true, if the left of the ball is less than the right of the square!!
boundx = 23.
So for shapeS[i].pos_rect_x1 = 20, and shapeB.pos_X = 50
it will be : (15<73) && (60 >27> | TRUE
For shapeS[i].pos_rect_x1 = 20, and shapeB.pos_X = 90
it will be : (15<113) && (60 > 67 ) | FALSE
So, it should be correct?? What's wrong?
I don't know you tell me! What is wrong! Explain whats wrong when you run the code...
Oh yes, sorry, i mean it collides with some squares while it doesnt with others! All random, it is something to do with my Update and Draw functions of square!
How do i check, that only if there is not square already on (x,y) position, next square should be drawn else, neither drawn nor updated(coz sometimes, invisible squares are coming up, i.e which are not drawn, but updated!)
THE SQUARE FUNCTIONS ARE AS :
varynoob, without trying to sound demeaning, it's very obvious that you are out of your depth here. If you do manage to get the help you need from people here in order to get the job, then what? It won't take long before your employer realizes that you don't have the necessary experience.
Gonna have to echo this. You'll finish the game, get the job, and be fired in two weeks. The point of this assignment is to see if you can do the job. If you could, you wouldn't be back in this thread every ten minutes.
I work as a video game programmer. When I applied for the job, they told me to make a Breakout clone, and I was budgeted two days. Difference was I could actually do it. And good thing, because I've had ample opportunity to laugh at some of the things other people send in.
Here, have a video break:
So You Want To Be A Developer Part One
So You Want To Be A Developer Part Two
Awesome videos, 23.
Thankyou 23, will try harder! If you see, i have asked many a times, but have only taken from this thread "Jumping mechanism" and lots of advice to KEEP THINKING and TRYING. I have been doing so, and will watch these videos and take your words as motivation. Everyone here has been saying the same, that until you do it yourself, it wont be of any use!
I am almost done with the game, it may not be perfect, but atleast i have tried and got something working! Thanks again everyone.
I don't believe we normally give this treatment to every user here. We try to be very helpful when someone comes to us with a problem where they already did most of the hard work and have a few bugs with their implementation. The problem is that you started the thread practically expecting us to do everything up front, so it gave a very bad impression. Not to mention that this seems to be something for a job interview or homework which most of us here would rather see you do some research beforehand. We will gladly help you with any of the problems, but you can't sound so desperate and demanding up front.
I did a quick Google search for "circle triangle collision" and "circle square collision" and there are some pretty relevant results returned. You can find most of the information you are needing from sources that practically give you the answer if you look. You may try that and if it fails to work for you, create a new thread and say, hey I found this algorithm for collision detection, this is how I implemented it and it's not quite working when I do this or that, can you help?
You may get a different response from us if that were the case.