Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Understanding and using sin() (https://forums.graalonline.com/forums/showthread.php?t=134257501)

Twinny 12-30-2009 06:25 AM

Understanding and using sin()
 
First and foremost: I hate maths. This hatred has been bred into me from an early age.

But I realise how important it is in game programming. So I want to understand =)

I want to make a sine wave in Graal. I would like to be able to put in a value for it's amplitude, it's frequency and the time it takes to complete a cycle.

Could someone be so awesome as to explain how this works to me using sin? Hopefully I'll be able to incorporate it into graal =)

DustyPorViva 12-30-2009 06:31 AM

http://i49.tinypic.com/izon4y.png

I'm horrible at math as well, but sin and cos map out the vertical and horizontal placement, respectively, of an angle(θ), as the picture demonstrates(ignore all the extra stuff, as just the blue and red lines are important).

Now you just wait until someone good at math comes and rips my post apart.

Just remember, gscript uses radians(0-pi*2) instead of degrees(0-360):
PHP Code:

function onCreated() {
  
temp.ang 0;
  for (
temp.i=0;i<500;i++) {
    
temp.px i;
    
temp.py sin(ang)*50// this is the wave, the 50 would be the amplitude, I guess
    
ang += pi/10// increases the angle, change this to change the frequency, I think
  
}


Should show a wave at px/py if you display it somehow with showimg or something.

Chompy 12-30-2009 06:32 AM

You've taken trigonometri in a math class before?

fowlplay4 12-30-2009 06:40 AM

Here's a nifty little block sine wave.

PHP Code:

//#CLIENTSIDE
function onCreated() {
  
setTimer(0.05);
}

function 
onTimeout() {
  
drawSine(timevar2);
  
setTimer(0.05);
}

function 
drawSine(initial) {
  
temp.inc pi 6;
  
temp.radius 64;
  for (
temp.initialtemp.< (initial+(pi*2)+temp.inc); temp.+= temp.inc) {
    
temp.tx 100 24 temp.j;
    
temp.ty = (screenheight 2) + temp.radius sin(temp.i);
    
temp.img showimg(200 temp.j"block.png"temp.txtemp.ty);
    
temp.img.layer 4;
    
temp.j++; 
  }


It's important because you can throw any number at it, and it'll return between -1 and 1. I only really know how it works through playing with it, so I would probably explain it wrong but we'll just have to wait for Tig and his Uni Math knowledge skills.

Twinny 12-30-2009 06:55 AM

One of my friends just gave me this explanation

"if y = sinX
amp = 1
period = 2π or 180ยบ
if y = msin(bX)
m changes the amplitude
b changes the period"

That could work =D

12171217 12-30-2009 06:58 AM

You could also think about it like Sin is the Y movement of an angle, and Cos is the X movement of an angle, I guess. That's it's most popular use on Graal, probably.

Twinny 12-30-2009 07:27 AM

Ended up with,

PHP Code:

function onCreated()
{
  
this.amp 0.2;
  
this.period 4;
  
this.initialy 2;
  
setTimer(0.05);
}

function 
onTimeout()
{
  
this.value += 0.05;
  
temp.value = (this.amp*sin((this.value*this.period)+this.initialy));
  
setTimer(0.05);


Created a nice little bob =)

End result is on Litheria above a sign =D

Catbert 12-30-2009 07:33 AM

im in grade 6

Loriel 12-30-2009 03:52 PM

Do not worry about hating maths, I was playing around with sin and cos before ever taking a trigonometry class and then was immediately disappointed that it did not turn out to be very useful for my custom movement system.

Twinny 12-30-2009 04:27 PM

Quote:

Originally Posted by Loriel (Post 1547872)
Do not worry about hating maths, I was playing around with sin and cos before ever taking a trigonometry class and then was immediately disappointed that it did not turn out to be very useful for my custom movement system.

My hatred of maths has prevented alot of stuff i wished to do with programming like 3d programming...I would quit with disgust at first mention of a matrices.

I was quick to understand and pick up concepts in high school but my overwhelming hatred made it horrible and my brain didn't let me retain it.... the original issues causing this have calmed down...so hopefully I can do it now :cool:

coreys 12-31-2009 03:34 AM

I hate matrixes, but they're easy enough. Although they don't use sin and cos in matrix math in high school algebra. Sin and Cos are used most frequently for finding information about a triangle or a circle.

Wikipedia is a big help too, ya know ;)


All times are GMT +2. The time now is 03:43 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.