Page 1 of 2

Calculating sine and cosine

Posted: Thu Oct 01, 2020 10:46 pm
by HitCoder
Hello!

I'm attempting to make a sonic game for DSGM, and ran into an issue very early on as there seems to be no functions to calculate sine or cosine. This is likely going to cause a big problem for this engine, but if anyone can provide any insight that would be very helpful.

I would try using the traditional approach of setting the angle per object but I feel like calculating movement in a direction would rely on sin and cos a lot, so i'm not sure how I would work around that anyway.

If it's not built into the game could anyone help me come up with scripts for the game to calculate this?

Re: Calculating sine and cosine

Posted: Thu Oct 01, 2020 10:50 pm
by DigitalDesignDude
There are certainly cos, and sin functions available through the Palib coding library.

Check under math functions. In the palib docmentations. I’ll share a 5.12 example of using them shortly.

viewtopic.php?f=10&t=39

Re: Calculating sine and cosine

Posted: Thu Oct 01, 2020 10:58 pm
by HitCoder
Thank you DDD! I literally just found this when double checking the PAlib.chm file just now (I forgot about it) and was going to post to let you all know I just found it, but thank you for directing me there, because it had only just crossed my mind. It's silly for me to have not checked, haha.

Hopefully I can make more progress now :D

Re: Calculating sine and cosine

Posted: Thu Oct 01, 2020 11:43 pm
by DigitalDesignDude
That's good you found it. :)

The Palib coding library is pretty much essential if you wish to accomplish anything that DSGM doesn't already do.

Here are some important things to know when working in with Angles in DSGM:
  • Angles are in radians from -256 to 256 or 0 to 512
  • DSGM rounds float values when used for positioning sprites. So movement speeds will increase when a sprite is moving perfectly left, right, up or down with cos and sin. You'll may notice that in the tank movement demo I have below.

Tank Movement Example - Moving with Cos and Sin
https://drive.google.com/drive/folders/ ... sp=sharing

Re: Calculating sine and cosine

Posted: Fri Oct 02, 2020 4:48 am
by Ruffsta
thanks for helping out DDD!

more general DSGM5.12 questions

Posted: Fri Oct 02, 2020 5:03 pm
by HitCoder
Posting on this thread as to not clutter the forum with any more threads;
I'm gonna dump several questions on this post, don't feel that you have to answer all in one

1) when declaring variables in the create event, I cannot "access" these in the step event. I get a compile error saying the variable doesn't exist upon calling it in the step event. I have declared using Public and Dim, and also declared with neither of these. I don't understand what I am doing incorrectly. I get this error regardless of whether I declare the variable in code or in the "set variable" drag and drop function

2) investigating drawing functions I find that the DS uses palettes of 256 colours. This is no problem to me, as I am familiar with palettes of such size (doom engine). How would I go about manually creating a palette and making the game use it?

3) float variables take up a lot of memory. are floats the only way I can get precision beyond a singular pixel? I want to give objects x and y coordinates with decimal points (position on screen will obviously be rounded up) to make movement and momentum more precise. I mostly want to do this for my sonic engine (see Sonic Retro's Sonic Physics Guide) but I'm feeling like I would have to use a float value. This is fine but due to still facing issue #1 mentioned above I cannot achieve this yet.

4) Is there a list of constants and/or built in variables (such as stylus, button listeners, object properties eg angle/image index/etc) anywhere?

I'm going to look through DSGM5.12's source code to find the latter and I will write down my findings somewhere so I can upload it as documentation resource at some point.

Thanks for all the help so far!

Re: Calculating sine and cosine

Posted: Fri Oct 02, 2020 5:56 pm
by Ruffsta
if you do a documentation, please submit your findings to me and i will pass it over to James.. he may want to overlook it and edit it if needed and then he'll pass it back to me and i'll put it either on the website or forum - or both

more general DSGM5.12 questions

Posted: Fri Oct 02, 2020 7:29 pm
by HitCoder
5) How can i check collisions based on a mask? I can only check collisions AFTER they have already happened according to the documentation despite the drag and drop blocks including a "if object at position" block.

6) can line collisions only be horizontal and vertical? what about diagonal? why would it be programmed in such a way? (i'll have to see if PALib has anything that can work in place)

Re: Calculating sine and cosine

Posted: Fri Oct 02, 2020 8:03 pm
by DigitalDesignDude
@HitCoder, thank you For posting your questions in the same area. It makes answering them in rapid succession easier for me :D


1) True public variables are created as global variables from the Global variables menu in the top tool bar. Another way of declaring them is in a .h include file which is just a script that you save with the extension “.h” . Under the “Game Settings” menu there’s a option for Included files where you can upload the script to. I typically declare variables in C code and they are public regardless of stating so in an .h file. Below are an example of declarations.

Code: Select all

int myInteger=1;
int myIntegerTwo;
myIntegerTwo = 2;
2) Can’t say I have any experience creating palettes to use in DSGM for code generated game art. Best place to look for any palette related code would be in the palib documentation under the “palette system” topic.

3) Unfortunately all float values are rounded when used for x and y coordinates of a sprite. You’ll never have an object with a .1 to .9 x or y position. Best way to compensate for this is too always ensure the position caculations always rounds down instead of being a perfectly even at any point. This will prevent sudden increases of speed when the conditions are meet that cause even coordinates. For example MoveSpeed*PA_Cos(angle)/256 will result in a whole number when angle = 0, 128, 256 or 384.

But (MoveSpeed*PA_Cos(angle))/255 will never divide evenly, at least from my memory it shouldn’t.


4) Only other place beside the ds coding documentation is looking at the code in the .action files within the dsgm program folder labelled “ACTIONS” from there you can open each file in a text editor to see their code and use them in your projects. These code statements are the same code used in “action blocks” which are the visual drag and drop coding blocks available in dsgm 5.12. When you see “!1!” In an action’s code, that is just the value that would normally be inserted by someone using a coding block. Just replace it with whatever variable or value you want when using in your script.

Re: Calculating sine and cosine

Posted: Fri Oct 02, 2020 8:16 pm
by DigitalDesignDude
5) Prehaps you could try to make your own collision code by testing two objetc’s [X], [Y], [X]+[Width], [Y]+[Height] against each other to see if they overlap. Or use PA_Distance(px,py, p2x,p2y) to find the distance between 2 X and Y points. That’s the only way I could think of to get a different timing. Not sure if even that will make a difference.

6) Not sure if this helps but to do an angled type of collision test, you might need to use a combination of coordinate testing with PA_GetAngle(px, py, p2x, p2y) to get the angle between two points. I’m not familiar with line collisions and just use the standard if collision event. Sorry if I’m not much help for this question.