Jibble

Author Topic: MODULE: rellax 0.1.4  (Read 3078 times)

eri0o

Re: MODULE: rellax 0.1.4
« Reply #20 on: 04 Sep 2020, 23:56 »
@Flugeldufel Thanks, I had never noticed that issue.

It happens if you get to a lower than 12 distance between Current Camera Position and the Wanted Camera Position apparently.

For now you can replace the rellax.asc file with the content below.

rellax.asc

Spoiler: ShowHide
Code: Adventure Game Studio
  1. // Rellax
  2. // 0.1.5~
  3. // A module to provide smooth scrolling and parallax!
  4. // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  5. // Before starting, you must create the following Custom Properties
  6. // in AGS Editor, for usage with Objects.
  7. // Just click on Properties [...] and on the Edit Custom Properties screen,
  8. // click on Edit Schema ... button, and add the two properties below:
  9. //
  10. // PxPos:
  11. //    Name: PxPos
  12. //    Description: Object's horizontal parallax
  13. //    Type: Number
  14. //    Default Value: 0
  15. //
  16. // PyPos:
  17. //    Name: PyPos
  18. //    Description: Object's vertical parallax
  19. //    Type: Number
  20. //    Default Value: 0
  21. //
  22. //  The number defined on Px or Py will be divided by 100 and used to increase
  23. // the scrolling. An object with Px and Py 0 is scrolled normally, an object
  24. // with Px and Py 100 will be fixed on the screen despite camera movement.
  25. //
  26. // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  27. //
  28. // based on Smooth Scrolling + Parallax Module
  29. // by Alasdair Beckett, based on code by Steve McCrea.
  30. //
  31. // Permission is hereby granted, free of charge, to any person
  32. // obtaining a copy of this software and associated documentation
  33. // files (the "Software"), to deal in the Software without
  34. // restriction, including without limitation the rights to use,
  35. // copy, modify, merge, publish, distribute, sublicense, and/or
  36. // sell copies of the Software, and to permit persons to whom the
  37. // Software is furnished to do so, subject to the following
  38. // conditions:
  39. //
  40. // The above copyright notice and this permission notice shall be
  41. // included in all copies or substantial portions of the Software.
  42. //
  43. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  44. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  45. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  46. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  47. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  48. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  49. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  50. // OTHER DEALINGS IN THE SOFTWARE.
  51.  
  52. #define MAX_PARALLAX_OBJS 39
  53.  
  54. Character *_TargetCharacter;
  55.  
  56. Object *_pxo[MAX_PARALLAX_OBJS];
  57. int _pxoRoomStartX[MAX_PARALLAX_OBJS];
  58. int _pxoRoomStartY[MAX_PARALLAX_OBJS];
  59. int _pxoOriginX[MAX_PARALLAX_OBJS];
  60. int _pxoOriginY[MAX_PARALLAX_OBJS];
  61. int _pxo_count;
  62.  
  63. int _cam_window_w, _cam_window_h;
  64. float _scroll_x,  _scroll_y;
  65. int _next_cam_x, _next_cam_y;
  66. int _prev_c_x, _prev_c_y;
  67. int _off_x, _off_y;
  68. int _look_ahead_x = 48;
  69. int _look_ahead_y = 16;
  70.  
  71. int _partial_c_height;
  72. int _count_still_ticks;
  73.  
  74. bool _is_doRoomSetup;
  75. bool _SmoothCamEnabled = true;
  76. bool _ParallaxEnabled = true;
  77.  
  78. float _Abs(float v) {
  79.   if(v<0.0) return -v;
  80.   return v;
  81. }
  82.  
  83. int _Lerp(float from, float to, float t) {
  84.   return FloatToInt(from + (to - from) * t, eRoundNearest);
  85. }
  86.  
  87. int _ClampInt(int value, int min, int max) {
  88.   if (value > max) return max;
  89.   else if (value < min) return min;
  90.   return value;
  91. }
  92.  
  93. void doObjectParallax(){
  94.   int camx = _next_cam_x;
  95.   int camy = _next_cam_y;
  96.  
  97.   for(int i=0; i<_pxo_count; i++){
  98.     if(_pxo[i].GetProperty("PxPos") !=0 || _pxo[i].GetProperty("PyPos") != 0) {
  99.       float parallax_x = IntToFloat(_pxo[i].GetProperty("PxPos"))/100.0;
  100.       float parallax_y = IntToFloat(_pxo[i].GetProperty("PyPos"))/100.0;
  101.  
  102.       _pxo[i].X=_pxoOriginX[i]+FloatToInt(IntToFloat(camx)*parallax_x);
  103.       _pxo[i].Y=_pxoOriginY[i]+FloatToInt(IntToFloat(camy)*parallax_y);
  104.     }
  105.   }
  106. }
  107.  
  108. void _enable_parallax(bool enable) {
  109.   _ParallaxEnabled = enable;
  110. }
  111.  
  112. void _enable_smoothcam(bool enable) {
  113.   if(enable == true){
  114.     doObjectParallax();
  115.   }
  116.  
  117.   _SmoothCamEnabled = enable;  
  118. }
  119.  
  120. void _set_targetcharacter(Character* target) {
  121.   _TargetCharacter = target;
  122. }
  123.  
  124. // ---- Rellax API ------------------------------------------------------------
  125.  
  126. void set_TargetCharacter(this Rellax*, Character* target)
  127. {
  128.   _set_targetcharacter(target);
  129. }
  130.  
  131. Character* get_TargetCharacter(this Rellax*)
  132. {
  133.   return  _TargetCharacter;
  134. }
  135.  
  136. void set_EnableParallax(this Rellax*, bool enable)
  137. {
  138.   _enable_parallax(enable);
  139. }
  140.  
  141. bool get_EnableParallax(this Rellax*)
  142. {
  143.   return _ParallaxEnabled;
  144. }
  145.  
  146. void set_EnableSmoothCam(this Rellax*, bool enable)
  147. {
  148.   _enable_smoothcam(enable);
  149. }
  150.  
  151. bool get_EnableSmoothCam(this Rellax*)
  152. {
  153.   return _SmoothCamEnabled;
  154. }
  155.  
  156. void set_CameraOffsetX(this Rellax*, int offset_x)
  157. {
  158.   _off_x = offset_x;
  159. }
  160.  
  161. int get_CameraOffsetX(this Rellax*)
  162. {
  163.   return _off_x;
  164. }
  165.  
  166. void set_CameraOffsetY(this Rellax*, int offset_y)
  167. {
  168.   _off_y = offset_y;
  169. }
  170.  
  171. int get_CameraOffsetY(this Rellax*)
  172. {
  173.   return _off_y;
  174. }
  175.  
  176. void set_CameraLookAheadX(this Rellax*, int look_ahead_x)
  177. {
  178.   _look_ahead_x = look_ahead_x;
  179. }
  180.  
  181. int get_CameraLookAheadX(this Rellax*)
  182. {
  183.   return _look_ahead_x;
  184. }
  185.  
  186. void set_CameraLookAheadY(this Rellax*, int look_ahead_y)
  187. {
  188.   _look_ahead_y = look_ahead_y;
  189. }
  190.  
  191. int get_CameraLookAheadY(this Rellax*)
  192. {
  193.   return _look_ahead_y;
  194. }
  195.  
  196. // ----------------------------------------------------------------------------
  197.  
  198. void doSetOrigins (){
  199.   _pxo_count=0; // Reset the total number of parallax objects to zero
  200.   float cam_w = IntToFloat(Game.Camera.Width);
  201.   float cam_h = IntToFloat(Game.Camera.Height);
  202.   float room_w = IntToFloat(Room.Width);
  203.   float room_h = IntToFloat(Room.Height);
  204.  
  205.   for(int i=0; i<Room.ObjectCount; i++){
  206.     if (object[i].GetProperty("PxPos")!=0) {
  207.                         _pxo[_pxo_count]=object[i];
  208.       float parallax_x = IntToFloat(object[i].GetProperty("PxPos"))/100.0;
  209.       float parallax_y = IntToFloat(object[i].GetProperty("PyPos"))/100.0;
  210.  
  211.       float obj_x = IntToFloat(object[i].X);
  212.       float obj_y = IntToFloat(object[i].Y);
  213.  
  214.       // initial positions for reset
  215.       _pxoRoomStartX[_pxo_count]= object[i].X;
  216.       _pxoRoomStartY[_pxo_count]= object[i].Y;
  217.  
  218.       //Set origin for object:
  219.       _pxoOriginX[_pxo_count] = object[i].X -FloatToInt(
  220.         parallax_x*obj_x*(room_w-cam_w) / room_w );
  221.  
  222.       _pxoOriginY[_pxo_count] = object[i].Y -FloatToInt(
  223.         parallax_y*obj_y*(room_h-cam_h) / room_h );
  224.  
  225.                         if(_pxo_count<MAX_PARALLAX_OBJS) _pxo_count++;
  226.                 }
  227.    }
  228.   doObjectParallax();
  229. }
  230.  
  231. void doRoomSetup(){
  232.   Game.Camera.X = _ClampInt(_TargetCharacter.x-Game.Camera.Width/2,
  233.     0, Room.Width-Game.Camera.Width);
  234.  
  235.   Game.Camera.Y = _ClampInt(_TargetCharacter.y-Game.Camera.Height/2,
  236.     0, Room.Height-Game.Camera.Height);
  237.  
  238.   _next_cam_x = Game.Camera.X;
  239.   _next_cam_y = Game.Camera.Y;
  240.   doSetOrigins();
  241.  
  242.   ViewFrame* c_vf = Game.GetViewFrame(_TargetCharacter.NormalView, 0, 0);
  243.   float scaling = IntToFloat(GetScalingAt(_TargetCharacter.x, _TargetCharacter.y))/100.00;
  244.   _partial_c_height = FloatToInt((IntToFloat(Game.SpriteHeight[c_vf.Graphic])*scaling)/3.0);
  245.  
  246.   if (_ParallaxEnabled) _enable_parallax(true);
  247.   else _enable_parallax(false);
  248.   _is_doRoomSetup = true;
  249. }
  250.  
  251. void doSmoothCameraTracking(){
  252.   if(_prev_c_x == _TargetCharacter.x && _prev_c_y == _TargetCharacter.y)
  253.     _count_still_ticks++;
  254.   else
  255.     _count_still_ticks = 0;
  256.  
  257.   if(_TargetCharacter.x-Game.Camera.Width/2-Game.Camera.X<=-_cam_window_w/2 ||
  258.      _TargetCharacter.x-Game.Camera.Width/2-Game.Camera.X>_cam_window_w/2 ||
  259.      _TargetCharacter.y-Game.Camera.Height/2-Game.Camera.Y<=-_cam_window_h/2 ||
  260.      _TargetCharacter.y-Game.Camera.Height/2-Game.Camera.Y>_cam_window_h/2 ||
  261.      _count_still_ticks > 5){
  262.  
  263.     int x_focus=0,y_focus=0;
  264.     if(_TargetCharacter.Loop == 2) x_focus = _look_ahead_x; // right
  265.     else if(_TargetCharacter.Loop == 1) x_focus = -_look_ahead_x; // left
  266.     else if(_TargetCharacter.Loop == 0) y_focus = _look_ahead_y; // down
  267.     else if(_TargetCharacter.Loop == 3) y_focus = -_look_ahead_y; // up
  268.  
  269.     float y_multiplier;
  270.     if(_count_still_ticks<=30) y_multiplier = IntToFloat(_count_still_ticks)*0.138 ;
  271.     if(_count_still_ticks>30) y_multiplier = 5.0;
  272.  
  273.     float target_cam_x = IntToFloat(_ClampInt(_TargetCharacter.x + _off_x + x_focus - Game.Camera.Width/2,   0, Room.Width-Game.Camera.Width));
  274.     float target_cam_y = IntToFloat(_ClampInt(_TargetCharacter.y + _off_y + y_focus - _partial_c_height - Game.Camera.Height/2,   0, Room.Height-Game.Camera.Height));
  275.     float current_cam_x =  IntToFloat(Game.Camera.X);
  276.     float current_cam_y =  IntToFloat(Game.Camera.Y);
  277.     float lerp_factor_x = 0.04;
  278.     float cam_distance_x = _Abs(target_cam_x - current_cam_x);
  279.    
  280.     if( cam_distance_x < 32.0 && cam_distance_x >= 1.0) {
  281.       lerp_factor_x  = 0.913*Maths.RaiseToPower(cam_distance_x, -0.7268);
  282.     }    
  283.  
  284.     _next_cam_x = _Lerp( current_cam_x, target_cam_x, lerp_factor_x );
  285.     _next_cam_y = _Lerp( current_cam_y, target_cam_y, 0.01*y_multiplier);
  286.   }
  287.  
  288.   _prev_c_x = _TargetCharacter.x;
  289.   _prev_c_y = _TargetCharacter.y;
  290. }
  291.  
  292. // --- callbacks --------------------------------------------------------------
  293.  
  294. function on_event (EventType event, int data){
  295.   // player exits any room
  296.   if (event==eEventLeaveRoom){
  297.     for(int i=0; i<_pxo_count; i++){
  298.       _pxo[i].X=_pxoRoomStartX[i];
  299.       _pxo[i].Y=_pxoRoomStartY[i];
  300.     }
  301.     _is_doRoomSetup = false;
  302.   }
  303.  
  304.   // player enters a room that's different from current
  305.         if (event==eEventEnterRoomBeforeFadein){
  306.     if(!_is_doRoomSetup){
  307.       doRoomSetup();
  308.     }
  309.   }
  310. }
  311.  
  312. function game_start(){
  313.  
  314.   System.VSync = true;
  315.   _cam_window_w = 40;
  316.   _cam_window_h = 40;
  317.   _set_targetcharacter(player);
  318.   _enable_parallax(true);
  319.   _enable_smoothcam(true);
  320. }
  321.  
  322. function late_repeatedly_execute_always(){
  323.   if(_SmoothCamEnabled) doSmoothCameraTracking();
  324.   if(_ParallaxEnabled) doObjectParallax();
  325.   if(_SmoothCamEnabled) Game.Camera.SetAt(_next_cam_x, _next_cam_y);
  326.   else {
  327.     _next_cam_x = Game.Camera.X;
  328.     _next_cam_y = Game.Camera.Y;
  329.   }
  330. }
  331.  
  332. function repeatedly_execute_always(){
  333.   if(!_is_doRoomSetup) doRoomSetup();
  334. }
  335.  


This should fix for now, but I am not completely satisfied with the resulting camera movement, kinda wanted to be a bit smoother, so I need to think a little bit more. Please confirm that the above code temporarily solves for you.

Flugeldufel

  • Gary G. Gerisson
    • Flugeldufel worked on one or more games that was nominated for an AGS Award!
Re: MODULE: rellax 0.1.4
« Reply #21 on: 05 Sep 2020, 07:10 »
Oh thank you for your quick response  :)
I‘m currently on the phone, but I will test it within the next hours, as soon as I‘m at home.

Flugeldufel

  • Gary G. Gerisson
    • Flugeldufel worked on one or more games that was nominated for an AGS Award!
Re: MODULE: rellax 0.1.4
« Reply #22 on: 05 Sep 2020, 09:39 »
Yeah, it works. You're right, the movement isn't as smooth as before, but for a low-res old school game it's not that crucial...
So, no hurry - I just wanted to know if there's something wrong with the module or I'm just stupid  ;)

And thanks again for your fast help!

Re: MODULE: rellax 0.1.4
« Reply #23 on: 13 Sep 2020, 18:45 »
Hey eri0o!

Just tried it in my upcoming game, and some issues occured.. And here is what happens if you try to use this module in a game sorta like Day of the Tentacle or Thimbleweed Park where you have different playable characters. The Parallax does not seem to recognise the character change, so the parallax effect gets stuck. Maybe it's just me, that don't understand how to do it, but just maybe you could do some kind of check for what player character is active, so the parallax follows that specific player.

Obviously since i've played Thimbleweed Park lately i was excited to try parallax scrolling, however when running in the Thumbleweed Template, the player gets transported back in the picture when running to the sides, so it looks funny. (laugh). Also the Parallax scrolling is stuttery looking, maybe that could be solved if i understood how to change the speed of the parallax, i'm not sure (Not using any objects, just followed the intro post's posy, posx in properties).

Btw, is there a manual of it somewhere? The syntax you wrote in the intro post, i just don't get how to use those in the scripts, it's way over my head, so examples would be great. I've tried several ways that i thought might would work, but to no success. However i did follow the instructions

Thanks, and i hope you deicide to keep working on this, tho it is a really cool feature for usage in AGS games  :)


eri0o

Re: MODULE: rellax 0.1.4
« Reply #24 on: 15 Sep 2020, 00:11 »
Hey Olleh19, I still need to figure out Flugerdufel problem. I use this module in my game, like my main use case is myself, which means if the problem doesn't affect me I tend to postpone solving - most from simply lack of time.

So you need to set the target character, basically the variable player points to a character, say cBob, when you assign player as target character, Rellax.TargetCharacter = player, you are actually assigning Rellax.TargetCharacter = cBob. Let's say now you change the player to cAlice, cAlice.SetAsPlayer(), NOW the player pointer will point to cAlice. But you have previously assigned the TargetCharacter as cBob. So you need to actually do something like this when changing player character:

Code: Adventure Game Studio
  1. cAlice.SetAsPlayer();
  2. Rellax.TargetCharacter = player;

About parallax scrolling being stuttery it will depend on many things so I can't comment. I recommending using SetGameSpeed(60). Basically the parallax is an illusion and the math is made simple, the object posx/posy (X position, Y position) is increased/decreased (divided by 100, this way I mentally think in percentages) an additional amount when the camera scrolls. The code is simple enough to be posted below, I find it easy to follow. The pxo are the room objects, the variable naming is stolen from Ali's module.

Code: Adventure Game Studio
  1. void doObjectParallax(){
  2.   int camx = _final_cam_x;
  3.   int camy = _final_cam_y;
  4.  
  5.   for(int i=0; i<_pxo_count; i++){
  6.     float parallax_x = IntToFloat(_pxo[i].GetProperty("PxPos"))/100.0;
  7.     float parallax_y = IntToFloat(_pxo[i].GetProperty("PyPos"))/100.0;
  8.  
  9.     _pxo[i].X=_pxoOriginX[i]+FloatToInt(IntToFloat(camx)*parallax_x);
  10.     _pxo[i].Y=_pxoOriginY[i]+FloatToInt(IntToFloat(camy)*parallax_y);
  11.   }
  12. }

So you set the room objects custom properties, and then you basically only use Rellax.EnableParallax = true on Room Load and you are done.

Re: MODULE: rellax 0.1.4
« Reply #25 on: 15 Sep 2020, 22:07 »
Hey Olleh19, I still need to figure out Flugerdufel problem. I use this module in my game, like my main use case is myself, which means if the problem doesn't affect me I tend to postpone solving - most from simply lack of time.

So you need to set the target character, basically the variable player points to a character, say cBob, when you assign player as target character, Rellax.TargetCharacter = player, you are actually assigning Rellax.TargetCharacter = cBob. Let's say now you change the player to cAlice, cAlice.SetAsPlayer(), NOW the player pointer will point to cAlice. But you have previously assigned the TargetCharacter as cBob. So you need to actually do something like this when changing player character:

Code: Adventure Game Studio
  1. cAlice.SetAsPlayer();
  2. Rellax.TargetCharacter = player;

About parallax scrolling being stuttery it will depend on many things so I can't comment. I recommending using SetGameSpeed(60). Basically the parallax is an illusion and the math is made simple, the object posx/posy (X position, Y position) is increased/decreased (divided by 100, this way I mentally think in percentages) an additional amount when the camera scrolls. The code is simple enough to be posted below, I find it easy to follow. The pxo are the room objects, the variable naming is stolen from Ali's module.

Code: Adventure Game Studio
  1. void doObjectParallax(){
  2.   int camx = _final_cam_x;
  3.   int camy = _final_cam_y;
  4.  
  5.   for(int i=0; i<_pxo_count; i++){
  6.     float parallax_x = IntToFloat(_pxo[i].GetProperty("PxPos"))/100.0;
  7.     float parallax_y = IntToFloat(_pxo[i].GetProperty("PyPos"))/100.0;
  8.  
  9.     _pxo[i].X=_pxoOriginX[i]+FloatToInt(IntToFloat(camx)*parallax_x);
  10.     _pxo[i].Y=_pxoOriginY[i]+FloatToInt(IntToFloat(camy)*parallax_y);
  11.   }
  12. }

So you set the room objects custom properties, and then you basically only use Rellax.EnableParallax = true on Room Load and you are done.

Thanks for your reply! I've used setplayer a lot of course, can't believe i didn't try that one! (doh!!). (laugh)
I'll play around with gamespeed aswell, thanks and once again keep working on it. I think it could be really cool if in the future it was inside the thumbleweed template. Since it's missing the parall feature.  :)

Edit: Rellax was it, now i'm making it look way better turns out all i needed was CameraLookaheadX and a minus value!

« Last Edit: 15 Sep 2020, 22:26 by Olleh19 »