"Newton's Hello World"
Displaying 1-4 of 4 total.
1
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
Glenshope

I can't see what I'm doing wrong here, anyone wanna jump in and point out what I'm doing wrong. For some reasons the tabs didn't translate over so I apologize for the awful way it looks.


//This program was originally meant to help me in my Physics for Engineers
//class. Unfortunatly I botched the code or my physics somewhere and thus the code doesn't
//work as advertised.


void AutoExec()
{
SetAppName("Newton's Hello World");
// Give the program a name in the title bar - just to be nice
int x = ImageWidth(screen) / 2; //Initial Hello World Position
int y = ImageHeight(screen) / 2;
int forceX=0; //My Forces on the X and Y axis
int forceY=0;
int accelerationX=0; //My acceleration on the X and Y axis
int accelerationY=0;
int velocityX=0; //My velocity on the X and Y axis
int velocityY=0;
int mass=50; //My mass
int Ptimer=1; //Just a timer to keep track of odd and even times
int xi=x; //Initial and final x position used with my timer
int xf=x;
int yi=y; //Initial and final y position used with my timer
int yf=y;
int friction=1; //Plans for friction, my Hello World slows down.
while (!b3)
{
if(Ptimer=1)
{
xi = x;
yi = y;
Ptimer--;
}
else if(Ptimer=0)
{
xf= x;
yf= y;
Ptimer++;
}
// Now we are going to check the arrow keys (or gamepad directionals) one at a time
// Then move the text around if a button is pressed at the last ShowPage() call
// We check up OR down then separately left OR right
// So the text will move diagonally if two nonconflicting buttons held down together
// We modify the Forcex and Forcey variables according to the button presses, moving the text
if (up) // If up arrow is pressed
{
ForceY -= 1; // Decrease y force
}
else if (down) // If down arrow is pressed
{
ForceY += 1; // Increase y force
}
if (left) // If left arrow is pressed
{
ForceX -= 1; // Decrease x coordinate
}
else if (right) // If right arrow is pressed
{
ForceX += 1; // Increase x coordinate
}
//Makes sure it doesn't fly off the screen, doesn't work yet for some reason
/*if(x>ImageWidth(screen)-100)
forceX -= 1;
if(x<100)
forceX += 1;
if(y>ImageHeight(screen)-100)
forceX -= 1;
if(y<100)
forceX += 1;*/
//All our Xiness, Has all the equations of movements,
//All of them are done on each iteration so time in our equation is 1
accelerationX=forceX/mass;
x=x + velocityx + (1/2)*accelerationX;
velocityX=accelerationX;
//All our Yiness
accelerationY=forceY/mass;
y=y + velocityY + (1/2)*accelerationY;
velocityY=accelerationY;

RectFill(0, 0, ImageWidth(screen), ImageHeight(screen), 0, screen);
// Reset the screen buffer to plain black
PrintCenter(x, y, screen, 0, "Hello World!");
// Prints said text centered from coordinates x, y
ShowPage(); // Displays the buffer on the screen
}
Exit("Bye!");
}

Posted on 2007-09-18 09:26:10 (last edited on 2007-09-18 09:27:37)

Kildorf

Hey, Glenshope.

First things first: If you want to post code just stick it inside handy-dandy pre tags. That'll preserve your formatting.

Like
so.


Anyway, the main issue that you have here is that you're working with integers, and small integers don't work well with physics simulations. You have to remember that integers, VergeC's number format, can't represent fractional bits at all. If a mathematical operation produces a fractional part, it's truncated - just thrown away. So, for example, (1/2) becomes 0, no matter what.

We get around this with what's called "fixed point" arithmetic. If you want an in-depth description, it'd be best to look it up, but basically you choose a multiplier and assume that your numbers that require fractional parts are multiplied by this.

In your case, you'd want to store force, position, velocity, and acceleration as a fixed point number. One of the easiest (but not super-precise) ways of doing this is to just treat 1000 as 1 ... so when you are adding 1 to your force, for example, add 1000 instead. When you're going to print out the string, you need to convert it back to actual screen coordinates, so you divide x and y by 1000.

More specifically, here are the changes you'll need:

	int x = (ImageWidth(screen) / 2) * 1000; //Initial Hello World Position
int y = (ImageHeight(screen) / 2) * 1000;



if (up) // If up arrow is pressed
{
ForceY -= 1 * 1000; // Decrease y force
}
else if (down) // If down arrow is pressed
{
ForceY += 1 * 1000; // Increase y force
}
if (left) // If left arrow is pressed
{
ForceX -= 1 * 1000; // Decrease x coordinate
}
else if (right) // If right arrow is pressed
{
ForceX += 1 * 1000; // Increase x coordinate
}


		x += velocityx + (accelerationX/2); // remember that (1/2) == 0!


		PrintCenter(x/1000, y/1000, screen, 0, "Hello World!");


I haven't tested that code specifically (I do fixed-point slightly differently myself; I tend to use bit shifts but they're somewhat more confusing so disregard this sentence if you don't understand what I mean) but it should work.

You might want to add a bit less force each iteration; currently it speeds up mighty fast. But now that you're using fixed-point, you can just add 200 or something rather than 1000. :)

Did that help out any or did I just confuse the issue more?

Posted on 2007-09-18 18:56:21

Glenshope

OK, I can understand that and thanks for the info about the pre tags.

Now how come the people working on the source code haven't given us doubles yet? I would think it wouldn't be too hard a thing to add, and would probably make things easier for new people coming in.

Posted on 2007-09-19 01:42:44

Kildorf

Well, actually, adding floats opens up several cans of worms that I think all of the engine devs just sort of want to avoid (I know I'm certainly not keen to go add them) - the parser has to be taught how to tell the difference, you've got to worry about casting between the types, you've got to duplicate functions to have float and int varieties, etc. etc. Also I seem to recall that vecna has said that there are a lot of places that would require reworking.

It's true that floats might make some things somewhat easier, but fixed-point isn't toooo bad as long as uh... someone actually explains it to you. And now that verge (in theory) can use Lua as a scripting language, if you really need floats you can switch to that (Lua's number type is always a double, I think; it doesn't have run-of-the-mill integers).

Posted on 2007-09-19 16:58:54


Displaying 1-4 of 4 total.
1
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.