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.

DarkBASIC Discussion / Tutorial Code won't work

Author
Message
Niv
8
Years of Service
User Offline
Joined: 10th May 2011
Location:
Posted: 10th May 2011 12:18
So I didn't really know where to post this so I am just writing it here... I have been running through a tutorial for walking on matrices however I have a problem with a piece of the code... This code is supposed to allow the camera to move forward as long as the camera is not to close the edge of the matrix, however the camera won't move forward, is there anyone who can explain the problem?

If Upkey()=1
XTest# = Newxvalue(X#,CameraAngleY#,20)
ZTest# = Newzvalue(Z#,CameraAngleY#,20)
If XTest#>0 and XTest#<10000 and ZTest#>0 and ZTest#<10000
Move camera 10
Endif
Endif
Libervurto
13
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 10th May 2011 23:39
Could you post the rest of the code? That part looks okay to me.

PS if you highlight your code and press the "code" button your text gets surrounded by code tags and will look like this when posted...

You can type those tags yourself if you find that easier (that's what I do).

If you aren't positioning the camera before you start it will be placed at 0,0,0 pointing down the z axis, so xtest# would equal 0 and fail the check.

Your memory has been erased by a mod - Your new name is Brian.
Latch
13
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 10th May 2011 23:56
Hello Niv,

To enclose your source in a code box, select the text you want to include in a code box, then click on the CODE button at the top right of your message above the emoticons.

Anyway, this is what the code you posted is doing:


This is checking if the up arrow has been pressed. If so then it'll run the following code:


The XTest# and the ZTest# lines are calculating a distance of 20 units away from the camera whatever direction it is facing on the x z plane. The X Z plane is a way of identifying space that is in the x and/or z directions. We can tell the test is on the x z plane because the calculations are using CamerAngleY#. Rotations around the Y axis are on the x z plane.

Onward. Once XTest# and ZTest# have their values the next line tests if those values are within certain limits:


The matrix is likely positioned so that it's bottom left corner is located at (0,0,0). And based on the limit tests, It seems that the matrix is 10000 x 10000 units in the x and z directions. Therefore, the left X edge is at 0 , the right X edge is at 10000, the Z bottom edge is at 0, and the z top edge is at 10000.

According to the code, if XTest# and ZTest# are calculated so that their values are GREATER THAN 0 and LESS THAN 10000 , then the camera will move forward 10 units. Since this is all triggered by pressing the up arrow, whenever that key is pressed and XTest and Ztest are within the limits, the camera will move.

If the camera is NOT moving, then the calculation is out of bounds or the up arrow is not being pressed.

You'll want to check the values of XTest# and ZTest# . Try the following changes to your code to see these values:



Enjoy your day.
Niv
8
Years of Service
User Offline
Joined: 10th May 2011
Location:
Posted: 11th May 2011 11:54
ok so if there is nothing wrong with that piece of code it logically must be something else right?

Latch i tried adding that code to my program and it gave me this (i don't know if it is any help):
XTest# = 5000
ZTest # = -980

So as it isn't that piece I decided to include my entire code for you to examine properly <3 (the piece i included was from the source code which didn't seem to work for me, it did the same thing as mine, but here is what I have been working with)
Libervurto
13
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 11th May 2011 23:23 Edited at: 11th May 2011 23:25
Quote: "XTest# = 5000
ZTest# = -980"

Z is way out of bounds! You see how useful it is to print variables to the screen.

I'm going to go all Sherlock Holmes on you now.
After examining the values of XTest# and ZTest#, I can deduce that the camera is positioned a way behind the matrix (-1000), central on the x axis (5000) and pointing directly towards the matrix down the z axis; this is apparent because in both z/xtests you are testing a move of 20, ztest appears to have moved 20 from -1000 to -980, while xtest is less likely to have moved as 5000 is exactly half the matrix's width. So if z is changing and x is staying the same, we must be moving directly down the z axis.

Therefore I conclude that the camera is being automatically positioned at 5000,0,-1000 to view the matrix. To turn this off type SET AUTOCAM OFF before the line where the matrix is created. Alternatively/additionally you could position the camera in the start position before beginning the main loop.

Your memory has been erased by a mod - Your new name is Brian.
Niv
8
Years of Service
User Offline
Joined: 10th May 2011
Location:
Posted: 12th May 2011 11:03
I tried the changes you suggested but neither of them seemed to make a difference... is there something wrong my my camera positioning code possibly?
Libervurto
13
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 13th May 2011 21:07
Try displaying the camera angle and position on screen. When developing a program it's a good idea to display all the variables you are using so you can see what is going on in the background.

Some people even write debugging consoles, which is like a program within a program which lets you check on variables and edit them while the program is still running!

Your memory has been erased by a mod - Your new name is Brian.
Niv
8
Years of Service
User Offline
Joined: 10th May 2011
Location:
Posted: 14th May 2011 14:21
how exactly would i do that?

Latch
13
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 15th May 2011 01:23
Quote: "Some people even write debugging consoles, which is like a program within a program which lets you check on variables and edit them while the program is still running"


There is something in DBC called the Command Line Interface (CLI). It's not exactly a debugging console but can used as if it is one to a degree.

Read through the following post. In it I describe the basic concept of using the CLI for testing purposes while your program is running.

Using The CLI

In regards to your problem, as OBese stated, your camera start position is OUTSIDE of the bounds that XTest# and ZTest# are looking at.

At the top of your program, you may have something like this:



add autocam off tight after the sync rate:



When you create any objects or matrices, the camera's position automatically gets set to (0,0,0). So, right before your main loop, try positioning the camera in the center of the matrix (at a height of 35 Y units):



Enjoy your day.
Libervurto
13
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 15th May 2011 13:56
Here's a simple way to show/hide your variables when testing.

substitute variable1 and so on for your hidden variables (such as ztest#).

Your memory has been erased by a mod - Your new name is Brian.
Niv
8
Years of Service
User Offline
Joined: 10th May 2011
Location:
Posted: 17th May 2011 10:09
I set my camera position to inside the bounds of that are required... however it seems that what I did not realise is that
Camera Position X
Camera Position Z

do not seem to refer to the actual position of the camera but rather the direction it is pointing. I can now move forward when ever the camera is aimed +1,+1 or more however if i turn my camera around either left or right and come up upon a situation where it is -1,+1 or +1,-1 the camera no longer moves forward. Technically though the code does what it is programmed to do, however it doesn't do what it is supposed to do, how it is that the X and Z positions do not seem to be coordinates but rather directions?

Because of all of this would the CLI actually work to fix this problem?

Niv
Libervurto
13
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 17th May 2011 20:30 Edited at: 18th May 2011 20:16
@Niv
No, camera positions are definitely positions.
I don't really understand the problem you're having, place the camera in the centre of the matrix and see if it still happens.

May I suggest writing the x/z tests in a slightly different way to make it easier to follow...

As you are quite new this may look really complicated, maybe I shouldn't be chucking this stuff at you just yet but meh lol.

What I've done is changed the tests to equal 1 if true; moving on that axis is allowed, or 0 if false; moving on that axis would be out of bounds. So now we have to pass both tests to be allowed to move.

You can use relational operators (=,>=,<=,<,> to return a value. The value returned is the result of the comparison (1=true, 0=false), e.g. 5>1 so A= 5>1 would give A a value of 1 (true). In fact relational operators ALWAYS return a value whether you are storing it in a variable or not; when you type if A>B ... you are actually asking if A>B = 1 ... DB evaluates the expression A>B, and if it's true (A is greater than B) it returns a value of 1 which the IF statement reads as a success and so gives access to the code inside it.
IF statements always look for a true value, so we don't really need to tell them to look for it; we could type if spacekey() ... and it would work just the same as if spacekey()=1 .... All we are really doing by adding "=1" (apart from making it clearer to beginners) is adding another expression to evaluate, so we're asking if the state of the spacekey is equal to one, instead of just asking if the spacekey is being pressed.

& (AND) is another type of relational operator called a binary operator, along with | (OR) and ! (NOT). All three are used to compare two parameters and return a value.
! (NOT) works the same as <>, it will return 1 if the two parameters are NOT equal, or 0 if they are indeed equal. I think it works faster than <>, it's quicker to type anyway and looks better.

AND and OR work a little differently. OR returns all the bits present in either number, while AND returns only those bits which are present in both numbers. So 6&2 would return 2 because 4 is only present in 6 not 2, however 6|2 would return 6.

If I've lost you that's okay, look up "Binary Digits" (or "bits" for short) and "Binary Numbers". It's worth learning because your computer thinks in binary, and we are trying to tell it what to think.

Your memory has been erased by a mod - Your new name is Brian.

Login to post a reply

Server time is: 2019-08-18 02:34:07
Your offset time is: 2019-08-18 02:34:07