forums

TNT Basic Forums > Programming Q&A
SIN wave help
< Last Thread     Next Thread >
Author
Thread        Post A Reply

02-23-2003 23:23

Posted by:
No Frills

Click Here to Email No Frills   Find more posts by No Frills

I have a little routine to draw a line between a 'start' and 'end' point - halfway through it changes from drawing UP the screen to drawing DOWN the screen.

Problem is - it looks like an unfinished triangle :-)
......CODE.......
int ypos,startpos,midpos,endpos,dir,loop,frcount
startpos=0
endpos=250
midpos=0
ypos=300
dir=-1
frcount=0
graphics mode 640,480
fade up
midpos=(endpos-startpos)/2
loop=startpos
while loop<endpos
set pixel colour loop,ypos,white
loop=loop+1
ypos=ypos+dir
if loop>midpos then dir=1
frcount=frcount+1
if frcount>1
frcount=0
draw frame
end if
wend
.....ENDCODE......

I know there are some maths commands to draw sin waves etc which would do pretty much what I'm after (smooth transition between startpos and endpos rather than a 'sharp' line).

Anyone help me out here?

02-25-2003 00:15

Posted by:
Holmes

Location:
Santa Rosa, Ca

Click Here to Email Holmes   Find more posts by Holmes

Here is how you do sine waves:

y = a * sin( b * (x-h) ) + k

a will be how steep you want the curve
b will be how quickly you want it to repeat
x will be your variable loop
h will be how many pixels you want it shifted over horizontally
k will be how many pixels you want it shifted up

-Holmes

02-25-2003 12:02

Posted by:
No Frills

Click Here to Email No Frills   Find more posts by No Frills

SIN Wave help

Thanks Holmes - I'll give it a shot.

Oh, 'off topic' I download your Raptor game a while ago from the iDevgames competition site.

Rather excellent job you did buddy.

Reminds me of the old S.E.U.C.K games - but that's not a bad thing.

Mightily impressed - I'm just a humble TNT beginner.

02-25-2003 13:04

Posted by:
macfixation

Click Here to Email macfixation   Find more posts by macfixation

I'm a programming beginner.

This forum can expect alot of questions from me when i get up to speed. :)

02-25-2003 13:56

Posted by:
No Frills

Click Here to Email No Frills   Find more posts by No Frills

More SIN wave help please :)

OK, I played with the variables suggested by Holmes.

What I need is a SIN wave that starts no LOWER than point 'starty' - and then heads off towards 'target' I also need it to stop as soon as it gets lower than its initial starty positon.

All the sin wave's I've tried continue the wave formation.

I just need one complete wave - sort of like a top-half moon. Not to concerned with what angles etc it head off in the direction - just so long as it doesn't go lower than 'starty' position.

03-04-2003 23:23

Posted by:
Holmes

Location:
Santa Rosa, Ca

Click Here to Email Holmes   Find more posts by Holmes

Okay, it sounds like you want to just have the sine wave go up, then come down and stop, correct? If so, you're talking about half the period.

To find the period, remember that pb = 360 when were in degree mode. So:

pb = 360
p = 360 / b

so then we have one half the period = p/2 = (360/b) / 2

So just stop the line when x > (360/b) / 2

Just plug in the value your using for b and that should work

03-05-2003 00:24

Posted by:
No Frills

Click Here to Email No Frills   Find more posts by No Frills

Thanks Holmes - problem was solved...

Once again Holmes, thanks for the help.

Turns out, it was me being stupid. Wasn't "really" SINE commands I needed - I was looking for a way to 'fire' a weapon like in Pocket Tanks or Worms, and have it launch, and do a sort or 'arc' effect.

I found the answer - and posted the original source here:

http://www.tntbasic.com/community/forums/showthread.php?threadid=522

Thanks once again for the help.

03-06-2003 23:10

Posted by:
Holmes

Location:
Santa Rosa, Ca

Click Here to Email Holmes   Find more posts by Holmes

No problem. I was about to ask if you were really looking for a sine wave in the first place.

I assume you just set it to some initial upward velocity and then add a bit to that each frame, driving it towards the ground? That makes a parabolic curve. Talk to me if you need help with setting the angle of the projectory or anything, I can send you some code.

03-06-2003 23:12

Posted by:
Holmes

Location:
Santa Rosa, Ca

Click Here to Email Holmes   Find more posts by Holmes

Ah whoops, it apears you incorporated projectile angle into your code (I followed the link). Good job!

03-07-2003 09:35

Posted by:
No Frills

Click Here to Email No Frills   Find more posts by No Frills

Parabolic Curve

Quote
No problem. I was about to ask if you were really looking for a sine wave in the first place.

I assume you just set it to some initial upward velocity and then add a bit to that each frame, driving it towards the ground? That makes a parabolic curve. Talk to me if you need help with setting the angle of the projectory or anything, I can send you some code
EndQuote

heheheh - NOW you tell me :-)

I searched high and low for this sort of code, anyone who has written a version simply doesn't want to give it out.

In the end, I think I found the link on google using 'parabolic trajectory' or something similar.

You may still be able to help though.

How would I add 'wind' into the equation?

03-07-2003 10:58

Posted by:
mark_667

Location:
England

Click Here to Email mark_667   Find more posts by mark_667

Maybe this is all a bit above my high-school math, but why would you need to incorporate wind into the equation unless it is something very noticeable like a hurricane? Projectiles such as bullets and missiles are never affected by this in real life otherwise. Also, have you thought of using MOAN patterns to simulate 'wobbles' on the affected sprites caused by wind?

On a less serious note: are you trying to make a game or a physics engine?!

03-07-2003 12:57

Posted by:
matteo

Location:
Venice, ITALY!

Click Here to Email matteo   Find more posts by matteo

Q: How do you add wind?

A:
treat wind as a constant force:

F=m*a

So simply decide the force of the wind, then divide it for the projectile (the bullet)' s mass so you find the acceleration, that is what you must add to the projectile velocity at every loop (just as you add gravity acceleration to the y velocity every loop).

03-07-2003 18:15

Posted by:
Holmes

Location:
Santa Rosa, Ca

Click Here to Email Holmes   Find more posts by Holmes


"Maybe this is all a bit above my high-school math, but why would you need to incorporate wind into the equation unless it is something very noticeable like a hurricane? Projectiles such as bullets and missiles are never affected by this in real life otherwise. Also, have you thought of using MOAN patterns to simulate 'wobbles' on the affected sprites caused by wind?

On a less serious note: are you trying to make a game or a physics engine?!"

Saying bullets aren't affected by wind is like saying they aren't affected by gravity. In fact, the effect on their velocity is great due to their miniscule mass. The only reason we don't see this in real life is because their trip from source to target is usually the blink of an eye. In a computer game, this is not so. But anyhow, ignoring my inane ramblings about physics, wind is usually just put into worms type games to add stradegy.

So anyhow, wind has magnitude and direction, which calls for a vector. First, figure out at what degree angle the wind is going so that we can get the x and y components of the vector which the program can use.

windSpeed = random(0,10)
windAngle = random(0,360)
xComponentWind = cos(windAngle)*windSpeed
yComponentWind = sin(windAngle)*windSpeed

Then, on every frame, adjust the bullets speed from the winds speed (force causes acceleration)

xComponentBulletSpeed = xComponentBulletSpeed+xComponentWind

yComponentBulletSpeed = yComponentBulletSpeed+yComponentWind

Hope that helps.

03-07-2003 18:17

Posted by:
Holmes

Location:
Santa Rosa, Ca

Click Here to Email Holmes   Find more posts by Holmes

P.S. I forget how TNT handles random numbers so I donno if my random functions will work....

03-10-2003 08:51

Posted by:
mark_667

Location:
England

Click Here to Email mark_667   Find more posts by mark_667

random numbers

QUOTE:
P.S. I forget how TNT handles random numbers so I donno if my random functions will work....
END QUOTE

Some languages, such as C use the system clock time to calculate random numbers, is this the case in TNT?

03-10-2003 21:57

Posted by:
Mark Tully

Location:
TNT HQ, England

Click Here to Email Mark Tully   Find more posts by Mark Tully

Random Numbers

If I remember correctly, TNT Basic sets the random seed to the system clock when it loads up. You can change the random seed yourself at runtime using 'set random seed'.

Cheers,

Mark

03-15-2003 03:25

Posted by:
Holmes

Location:
Santa Rosa, Ca

Click Here to Email Holmes   Find more posts by Holmes

Oh, no I meant the names of the random functions actually. Wasn't sure if I had them set up right.

03-15-2003 18:15

Posted by:
eLL

Click Here to Email eLL   Find more posts by eLL

Define Seed

Mark Tully,

When you say 'Seed', do you mean it in the sense;

Pick a number between 1 and Seed.

where seed is the highest number which the random number generator may select?

03-16-2003 20:48

Posted by:
Mark Tully

Location:
TNT HQ, England

Click Here to Email Mark Tully   Find more posts by Mark Tully

Random number seeds

When you say 'Seed', do you mean it in the sense;

Pick a number between 1 and Seed.


No. Because computers are completely deterministic machines, they can't actually generate random numbers - all they can do is add numbers together in interesting ways very quickly. So when people say 'random numbers' in programming circles, they really mean pseudo-random numbers, that is numbers that appear random but are actually produced by some algorithm that will give the same results every time.
So when I say random, I really mean pseduo-random too.

A random number generating algorithm typically stores a value internally that it uses to generate the next random number. Each time a number is asked for, the algorithm applies some maths to the stored value to get a number out of it between the min and max values that you specify. Once it has the value, it does some more maths to its internal value so that next time a number is asked for it will be different from the previous.

Because the random number generator will return the exact same sequence of numbers every time it is ran, the only way of varying what results are returned is to change the starting value that the algorithm uses. This starting value is called the random number seed, presumably because from that one value an entire sequence of numbers is generated.

If you run the same algorithm with the same seed you will get the same results. Example:

int x
for x=1 to 100
print random (0,100)
next

Run this example several times. Each time it will return different numbers. Now insert the following line at the top of the program:

set random seed 10

Now each time the same results are returned, because the random number generator was seeded with the same value, in this case 10, each time.

The seed bears no resemblence to the number actually returned from the random number generator, this relation is determined by whatever algorithm is generating the numbers.

When a TNT Basic is first launched it sets the random seed to the current time and date. This makes it pretty unlikely that any two runs of TNT Basic will ever generate the same set of random numbers. This is common practise.

There are other ways of generating random numbers that can give less predictable results. Some of these methods involve using the frequency of network packets received by the computer or the frequency of the key presses the user is making to adjust the random seed on the fly. These certainly lead to less predictable results, but some would still argue they aren't random. At the end of the day, very few things are random until you get down to the quantum level (not something I know much about!), but I don't think TNT Basic needs to go quite that far yet...

Enjoy,

Mark

03-17-2003 01:03

Posted by:
matteo

Location:
Venice, ITALY!

Click Here to Email matteo   Find more posts by matteo

what a philosoph... ;-)

Cheers

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.