Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Editing Terrain Demo...

Author
Message
Under_score
14
Years of Service
User Offline
Joined: 5th Oct 2010
Location:
Posted: 6th Oct 2010 02:26
Hello! This is my first post on here, and I've been using Dark GDK for a few weeks now. Usually, I learn by taking an already good script, and manipulating it so it could be better. I'm working on the terrain demo, and I want there to be a change of speed when a user enters a key, so what would the syntax be to press 'S' or 'W'?

Also, I am trying to make it so if you fall off, the camera resets itself. How can I attach 'Gravity' to the camera, and how would I get it to reset? The code is mainly that of the original tutorial, I've only added a jump feature.

-->Sottolineamento<--
Under_score
14
Years of Service
User Offline
Joined: 5th Oct 2010
Location:
Posted: 11th Oct 2010 03:24
Could someone please help with this? T'would make the game a lot better.

-->Sottolineamento<--
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 11th Oct 2010 23:26
Watching for keypresses is pretty easy, you only need to know the scan codes. Have a look at the dinput.h file of the DirectX SDK. It can be found somewhere under Program Files, DirectX (version) SDK, include directory, if it is installed in the default location. It contains defined constants for all keys, if you need only S and W then copy them out into your program:



(It is possible to include the whole header but I don't like to do it because it triggers "DirectX version" warnings during compiling. The warning is probably harmless but I rather copy the necessary definitions, that doesn't hurt either.)

Then you can watch the keys like this:



or if there are many keys to watch, a switch is better:



By "resetting" the camera do you mean to return to a default position? You should be able to do that by storing the position of the camera in the beginning of the program (before the main loop), e.g.



When you want to reset, just apply the default values with



Actually the terrain tutorial uses hardcoded numbers for the initial camera position so you can just use the same values for resetting.

To answer the "gravity" question it would be good to see how you implemented the jump. Can you post your current code?
Under_score
14
Years of Service
User Offline
Joined: 5th Oct 2010
Location:
Posted: 12th Oct 2010 01:40
First of all, let me just say thank you.

As for the gravity, I put the jump in like this:

However, the 'S' and 'W' won't work. Could I see how you do that, if I'm doing something like

and pressing 'W' will make speed increase and 'S' decrease. If you could work that in, that would be much appreciated.

-->Sottolineamento<--
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 12th Oct 2010 21:38
Here is a modified terrain demo code. I have included adjusting the speed with W and S keys, and resetting the camera if the player gets outside the terrain area ("falls off" the edge).

I have removed the original comments of the tutorial and added my own comments, to explain the code that I've added. This is the whole cpp file so you can just exchange the whole thing.

The jump is not in it yet, I will post another code for that later. (In advance, I can say that it will be much shorter than your code. I think I more or less understand what you tried to do with that jump code but it has several problems.)

Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 12th Oct 2010 22:42 Edited at: 12th Oct 2010 22:45
Part 2. Here is the next code with jumping. I removed comments again, so only the changes are commented. To introduce the jump I had to rearrange the main loop a bit because I needed the values of the variables earlier. So I moved the camera move and terrain height commands to the beginning of the loop.

In your original code, I guess you wanted to change the camera height with different amounts depending on how high it currently is. But you seem to confuse range checking with "for" loops, these two are not the same. Then you are repeating code many times which is bad practice. Finally, the usual method of implementing gravity is different: there is a vertical speed for the jump, which is initially high, then it is decreased every loop by a constant gravity value. The speed is then added to the vertical position. Thus, the vertical position is changing by different amounts.

You can play with the initial vertical speed and the gravity value to make the jump higher or smaller. If you want, you can also experiment with different methods to perform the jump but this is a good general starting point. In very advanced programs, they use physics engines anyway, making the behaviour closer to real life.


Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 12th Oct 2010 22:57
One more comment. I don't know if I've done a good thing or not by writing the complete programs for you. It's generally not the habit on the forum to present whole solutions and it's not good if you don't work it out yourself. However, seeing that even increasing/decreasing a speed variable presented a problem for you (when basically you should have added maybe two lines to the code that you already had, since you had already substituted "speed" into the "control camera" command and I've given you the rest with the keyboard check), I felt that some general hints will not be enough, especially for such a complex program reorganization as the gravity. Anyway, I hope that this example will help you to figure out the next task yourself.
Under_score
14
Years of Service
User Offline
Joined: 5th Oct 2010
Location:
Posted: 13th Oct 2010 00:45
Thanks! The program runs great! I'm sorry if I'm a bit ambitious, and I understand from reading other posts that it is not the custom to post entire codes. However, in the past, I've learned best by deconstructing codes, and rebuilding from there. For a while, I think I will stick to easier stuff. Thanks again for your time and help!

-->Sottolineamento<--
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 13th Oct 2010 08:29 Edited at: 13th Oct 2010 08:31
You're welcome! Actually I'd like to add some more explanation and correction to the above code. I've noticed that I didn't handle CamPosY as I originally wanted to. It was supposed to be "jump height above terrain" and not "combined terrain and camera height". If you change some lines, the code will be more convenient: the camera height above terrain is calculated separately and added to the terrain height only when the camera is repositined. I have also removed the "else" branch of the jumping which was not really necessary. Here are the changed parts.



I'd also like to explain the main issue with your original jump code. It would be possible to implement a jump using your idea (height increments based on current height), but you tried to write the whole movement sequentially (jump until you finish) and a game is not sequential. Usually there are several moving objects, for example enemies running around and shooting at the player, but even static objects like the terrain may need updating. If you loop until the jump is finished, you don't give other objects a chance to update and display themselves and everything else on the screen will freeze. Instead, you have to cut up the movement into many little pieces: move as much as you can in the current frame, then start the next frame. If the jump is still not finished, then move a little again, next frame again. That's why we need the status indicator. It's a bit difficult to think in frames at first but you'll quickly get used to it.

You are right that it's good to learn from examples. It's easy to say work it out but if you've never seen at least one example then it's very difficult. When I was learning programming, there used to be quite a number of books showing example codes, game parts and other code too. Now such books are not common unfortunately, but you can still get examples from forums.

I have some ideas how you could further develop this program:

1. Try to change it so that the camera does not reset when you reach the edge of the terrain, but the player is not allowed to walk outside the terrain area.

2. Try to modify the code to keep the forward speed when jumping during a run. When you press the forward arrow and then jump, then the forward movement stops because the jump is always in one place. That's not realistic, it would be better to move forward at a constant speed during jump, if the player was running when the Space key was pressed.

I think these would be good exercises for practice too.
Under_score
14
Years of Service
User Offline
Joined: 5th Oct 2010
Location:
Posted: 14th Oct 2010 00:47
Thanks again for your help!

Also, I've already done the second, and would you recommend using an invisible object with texture that looks like a fence for the former suggesting?

_______________
/Sottolineamento\
\_______________/
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 14th Oct 2010 20:09 Edited at: 14th Oct 2010 20:10
I was only thinking about limiting the movement coordinates, but a fence is a good idea. If you make a fence, and a player character (I say character but a simple object like a cube will do) then you can even experiment with basic collision detection between the player object and the fence.

Login to post a reply

Server time is: 2024-11-19 18:39:11
Your offset time is: 2024-11-19 18:39:11