Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 12-30-2009, 06:25 AM
Twinny Twinny is offline
My empire of dirt
Twinny's Avatar
Join Date: Mar 2006
Location: Australia
Posts: 2,422
Twinny is just really niceTwinny is just really nice
Send a message via AIM to Twinny
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 =)
Reply With Quote
  #2  
Old 12-30-2009, 06:31 AM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva


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.
Reply With Quote
  #3  
Old 12-30-2009, 06:32 AM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
You've taken trigonometri in a math class before?
__________________
Reply With Quote
  #4  
Old 12-30-2009, 06:40 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
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.
__________________
Quote:
Reply With Quote
  #5  
Old 12-30-2009, 06:55 AM
Twinny Twinny is offline
My empire of dirt
Twinny's Avatar
Join Date: Mar 2006
Location: Australia
Posts: 2,422
Twinny is just really niceTwinny is just really nice
Send a message via AIM to Twinny
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
Reply With Quote
  #6  
Old 12-30-2009, 06:58 AM
12171217 12171217 is offline
Banned
Join Date: Jan 2009
Posts: 453
12171217 has a spectacular aura about
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.
Reply With Quote
  #7  
Old 12-30-2009, 07:27 AM
Twinny Twinny is offline
My empire of dirt
Twinny's Avatar
Join Date: Mar 2006
Location: Australia
Posts: 2,422
Twinny is just really niceTwinny is just really nice
Send a message via AIM to Twinny
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
Reply With Quote
  #8  
Old 12-30-2009, 07:33 AM
Catbert Catbert is offline
handsome
Catbert's Avatar
Join Date: Aug 2009
Location: Canada
Posts: 154
Catbert has a little shameless behaviour in the past
im in grade 6
__________________
Reply With Quote
  #9  
Old 12-30-2009, 03:52 PM
Loriel Loriel is offline
Somewhat rusty
Loriel's Avatar
Join Date: Mar 2001
Posts: 5,059
Loriel is a name known to allLoriel is a name known to allLoriel is a name known to allLoriel is a name known to all
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.
Reply With Quote
  #10  
Old 12-30-2009, 04:27 PM
Twinny Twinny is offline
My empire of dirt
Twinny's Avatar
Join Date: Mar 2006
Location: Australia
Posts: 2,422
Twinny is just really niceTwinny is just really nice
Send a message via AIM to Twinny
Quote:
Originally Posted by Loriel View Post
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
Reply With Quote
  #11  
Old 12-31-2009, 03:34 AM
coreys coreys is offline
N-Pulse Assistant Manager
coreys's Avatar
Join Date: Mar 2005
Posts: 2,180
coreys has a spectacular aura about
Send a message via AIM to coreys Send a message via MSN to coreys Send a message via Yahoo to coreys
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
__________________

Quote:
*SlikRick: so should I even ask about your aim status?
*Xor: well if you want to
*Xor: but i am LARPING
*SlikRick: While on a computer?
*Xor: yes
*Xor: in my living room
*SlikRick: ahh
*Xor: i have a fort setup to hide from beasts
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 11:15 PM.


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