Tutorials

 Reply to this postStart new topic

[TUT]Using "sine" and "cosine"

Deji
post Jan 26 2010, 04:44 AM
Post #1


Coding like a Rockstar!

Group Icon

Posts: 1,468
From: ???
Joined: 28-May 09



using "sine" and "cosine"


Sine and Cosine are two useful mathematical functions for getting an offset. For example:


SANNY
0679: put_camera_on_car 0@ with_offset 10.0 0.0 2.0 rotation 0.0 0.0 0.0 0.0 2


This line of code will point the camera to a car, using an offset. The problem is, this only covers one angle. There are 360 degrees that an angle could be at. We could still do this by messing with the offset for hours, but there is a much faster way to do it. A way where you can insert the angle/degrees instead of the offset, and get the offset made for you.

This is also useful if you are not going to know what the angle you will be setting is. For example, if you want keys to control the angle.


So how do we get the right offset for the angle? Sine and Cosine. First, two important things to remember:


SineX Offset
CosineY Offset


So we're going to use sine for getting the first offset, and cosine for the second. Lets make our code circle the camera around the car.


SANNY
0007: 7@ = 0.0 // store the starting angle/degree in a variable.
{-------------------------------------
Simply wait until player is in a car
-------------------------------------}

:LOOP
wait 0
if
    0449:   actor $PLAYER_ACTOR in_a_car
else_jump @LOOP
01B4: set_player $PLAYER_CHAR can_move 0 // make sure player can't mess up the camera
03C0: 0@ = actor $PLAYER_ACTOR car // store players car
000B: 7@ += 3.0 // increase the angle/degrees by 3, making the camera move left/right. the higher the float number, the faster the camera movement
02F6: 8@ = sine 7@ // get the x offset from the angle
02F7: 9@ = cosine 7@ // get the y offset from the angle
0679: put_camera_on_car 0@ with_offset 8@ 9@ 2.0 rotation 0.0 0.0 0.0 0.0 2 // put camera at the offset
jump @LOOP // do it again, making the camera move more


Compile, run GTASA, get in a car, the camera will move in circles around the car... kinda.


The problem is, the camera is too close to the car, which is not very impressive... Can we do anything about that? Duh...


What we need to do is to change the offset that the sine and cosine output. We need to add some distance. We can do this by simple multiplication. Because we all know that multiplication increases things, right? If not, maybe you shouldn't be coding tongue.gif


SANNY
0013: 8@ *= 10.0 // Increase to make the X Pos of the camera further away from the car.
0013: 9@ *= 10.0 // Increase to make the Y Pos of the camera further away from the car.


This is what we need. And this also helps if we wanted to vary the way the camera moved. We could change only one value to make it an oval spin, instead of a circle spin. But keeping them the same makes it a perfect circle. Again, primary school level math, really.



So we have our code in full:


SANNY
0007: 7@ = 0.0 // Starting rotation.

:LOOP
wait 0
if
   0449:   actor $PLAYER_ACTOR in_a_car
else_jump @LOOP
01B4: set_player $PLAYER_CHAR can_move 0
03C0: 0@ = actor $PLAYER_ACTOR car
000B: 7@ += 3.0 // Movement multiplier. Increase for more camera speed. Decrease for less speed.
02F6: 8@ = sine 7@
02F7: 9@ = cosine 7@
0013: 8@ *= 10.0 // Increase to make the X Pos of the camera further away from the car.
0013: 9@ *= 10.0 // Increase to make the Y Pos of the camera further away from the car.
0679: put_camera_on_car 0@ with_offset 8@ 9@ 2.0 rotation 0.0 0.0 0.0 0.0 2
jump @LOOP



Try it. Get in a car, and the camera will spin around it. Now try some other things! For example, replacing the "000B" line with:


SANNY
0494: get_joystick 0 data_to 7@ 6@ 6@ 6@


And then pressing the movement key in game. See what happens. If it doesn't work so well, try and fix it! I will be posting a tutorial on how to use it soon wink.gif


And by the way, you know them "sin" and "cos" buttons you get on scientific calculators? Guess what they do? Yeah, if you've followed this tutorial, you are now geeky enough to actually know what they do! Lol... I never did, either...



* More info + image on sine/cosine. Or just scroll down a few posts.


--------------------
Go to the top of the page
 
+Quote Post
Huckleberry Pie
post Jan 30 2010, 03:29 AM
Post #2


Ameteur Member

Posts: 49
Joined: 5-August 09



I guess I should've paid more attention to my math teacher then. Working with sine and cosine can be a bit complicated, you know, especially when coding a videogame.
Go to the top of the page
 
+Quote Post
Deji
post Jan 30 2010, 05:57 AM
Post #3


Coding like a Rockstar!

Group Icon

Posts: 1,468
From: ???
Joined: 28-May 09



If you understand this tutorial, you'll do well working with sine and cosine. I've tried to explain things as simply as possible...

Angle+Sine = X Offset
Angle+Cosine = Y Offset

All you really have to do is input random values into sine and cosine and use the offsets they give. Play around with it for long enough and you should get the basic idea of things wink.gif


Slight image error: second code line (with 1@) should say "cosine", not "sine"... whoops


So sine and cosining "45.0" and setting the object to sine and cosine, you have the green blob in it's position wink.gif


--------------------
Go to the top of the page
 
+Quote Post
Th3MaN1
post Jul 5 2011, 05:38 AM
Post #4


The New Guy!

Posts: 4
Joined: 2-July 11



Can I use this on the player(actor)?If yes,how?
Go to the top of the page
 
+Quote Post
Deji
post Jul 5 2011, 08:35 AM
Post #5


Coding like a Rockstar!

Group Icon

Posts: 1,468
From: ???
Joined: 28-May 09



SANNY
0172: 0@ = actor $PLAYER_ACTOR Z_angle
02F6: 1@ = sine 0@
02F7: 2@ = cosine 0@


--------------------
Go to the top of the page
 
+Quote Post
Th3MaN1
post Jul 7 2011, 08:19 AM
Post #6


The New Guy!

Posts: 4
Joined: 2-July 11



Thanks!
Go to the top of the page
 
+Quote Post
Th3MaN1
post Jul 9 2011, 05:26 PM
Post #7


The New Guy!

Posts: 4
Joined: 2-July 11



I have another question.What the opcode for using this when the player aims?
Go to the top of the page
 
+Quote Post
Reply to this postStart new topic

7 User(s) are reading this topic (7 Guests and 0 Anonymous Users)
0 Members: