## martes, 29 de diciembre de 2015

### Parallax Scrolling in Love2d. Part 3: Different Camera Spaces

Hi!

This is the last installment of the series on parallax scrolling in Love2d. In the previous posts, we first discussed how we could implement this technique by defining a camera component and the concept of layers, and how we could reason about the two different coordinate systems that we had: the world and the camera coordinate systems. Consider reading these previous posts before going on, because this installment builds upon them.

We will see now how we can represent an object that is in a given camera space in another camera space, which can be useful for multiplayer games. The problem statement is depicted in Figure 1. We see that we have two cameras, each one representing a fragment of the same world. One of the cameras (camera 1) is in the position (x1, y1), whereas camera 2 is in (x2, y2). Again, for the sake of simplicity, we will focus on the X dimension, but the same reasoning applies to the Y dimension. Each camera follows a different player, namely player 1 and player 2, which have their positions in their camera spaces posPlayer1_cam1 and posPlayer2_cam2, respectively. Now, assume that we want to represent a ghost version of  player 2 in the camera space of player 1, so that player 1 can see the position of player 2. Which is the position of player 2 in the reference system of camera 1, that is, which is the value of posPlayer2_cam1?

Figure 1. Problem statement

At first sight, the problem seems trivial, and for most cases, it actually is. We simply need to represent the (world) position of player 2 in relation to the camera. Therefore, if the position of player 2 is posPlayer2, and as usual we assume that players belong to layers with scale = 1, we know that:

posPlayer2_cam1 = posPlayer2 - posCamera1 (1)

Likewise, the position where the player will be drawn would be:

posPlayer2_draw = posCamera1 + posPlayer2 - posCamera1 = posPlayer2 (2)

And that's it. This is what works in most of the cases.

However, there is a situation that we need to consider. Assume that there are obstacles with which the players can collide. If these obstacles belong to a layer with scale = 1, then everything works as we've just described. However, if obstacles belong to layers with a different scale, weird things happen. Suppose that obstacles belong to a layer with scale = 2. Then, as we saw in the previous post, advancing the camera u units result in the object being moved 2u units to the left. Therefore, if we used simply (1) or (2), player 1 would have the impression that player 2 collides (stops) before reaching the obstacle, as depicted in Figure 2.

Figure 2. Player 2 reaches the obstacle before it is perceived by player 1

This is happening because whereas the obstacles are moving with a rate of 2u units to the left, player 2 is being moved in camera 1 at a rate of 1u unit to the right. Therefore, player 2 reaches the obstacle in camera 2 before it reaches it in camera 1. The solution is moving player 2 in camera 1 at the same rate as the obstacles move but in opposite direction, that is, 2u units to the right.

If obstacles belong to layers with scale = n, 0<n<1, then player 2 would reach the obstacle in camera 1 before it reaches it in camera 2, and player 1 would therefore perceive as if player 2 had run through the obstacle, as depicted in Figure 3.

Figure 3. Player 1 perceives that player 2 run through the obstacle

Therefore, the solution consists of correcting this displacement: player 2 has to move in camera 1 at the same rate than obstacles. If camera 1 remains still and camera 2 advance u units, player 2 has to be moved 2u units to the right in camera 1. If camera 2 remains still and camera 1 advance u units, player 2 has to be moved 2u units to the left in camera 1. So the general pattern is: for every unit u of difference between the positions of camera 2 and camera 1, the position of player 2 has to advance 2u units.

posPlayer2_cam1 = posPlayer2_cam2 + 2*(x2 - x1) = posPlayer2 - x2 + 2*(x2 - x1)  (3)
posPlayer2_draw = x1 + posPlayer2_cam1 = x1 + posPlayer2 - x2 + 2*(x2-x1) (4)

We can simplify (3) and (4) as follows:

posPlayer2_cam1 = posPlayer2 + x2 - 2 * x1
posPlayer2_draw = posPlayer2 + x2 - x1

Finally, we can generalize the formulas for a player in a layer with scale = n, and obstacles in a layer with scale = m:

posPlayer2_cam1 = posPlayer2 + (m - n)*x2 - m*x1
posPlayer2_draw = posPlayer2 + (m - n)*x2 + (1 - m)*x1

And this is all! Hope you enjoyed this series of posts as much as I enjoyed writing it. As I mentioned in the first post, these are the considerations that I took into account while developing the parallax scrolling system for our game to come, Breaking Fast, the result of which you can watch in the following video:

See you!

#### 33 comentarios:

1. Avast Customer Support
Avast Support
Norton Tech Support
Norton Support
Gmail Customer service
www.office.com/setup
Office Setup
office.com/setup محسن چاوشی

1. Thanks for your submission. I also think that laptop computers are getting to be more and more popular right now, and now are sometimes the only sort of computer found in a household. This is due to the fact that at the same time potentially they are becoming more and more economical, their processing power is growing to the point where they’re as strong as desktop computers from just a few years ago.
norton.com/setup

2. Thanks for sharing the blog. Hey I am Alicen Carter From New jersey. Welcome to Geek Squad Technical Support. Electronic devices such as Pcs, laptops, Tvs, are now an essential element of the modern workplace. Like another piece of equipment, electronic devices are prone to issues too. Geek Squad Support is There to Support You we have a Best Technician for Help you and solve your issues very quickly. Just Dial Geek squad tech support number 1-856-673-221. Our Technical Team available for 24/7 hours for support you.
Geek support number

3. This product gives continuous security against PC infections, spyware, and other online dangers. Be that as it may, the Kaspersky login is required before utilizing this product on your gadget.

4. BullGuard Internet Security has presented comprehensive security suite packages in 2020 with quickest bullguard download and BullGuard login procedures. BullGuard users should be relaxed because BullGuard has launched additional security features. www.bullguard.com/is-mdl-install | www.bullguard.com is-install-mdl-install | bullguard login | bullguard Internet security | bullguard download

5. Amazing Post. Your writing is very inspiring. Thanks for Posting.

6. Trend Micro is one of the largest providers of internet and antivirus security in the world with over 250 million users. The company’s antivirus protects you from malware, email scams, and even ransomware, which is become more dangerous every day. www.trendmicro.com/bestbuypc |Trend Micro Geek Squad | www.trendmicro/bestbuypc

7. I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people. Thanks for sharing the information keep updating, looking forward to more posts.Very nice blog and articles.
install-trend-micro-n-new-computer

8. Thank you for sharing this wonderful article, And it is very useful information norton.com/setup
www.norton.com/setup

9. Thank you so much for this Post and all the best for your future. You are really a talented person I have ever seen. I am satisfied with the arrangement of your post i think i must share here www.norton.com/setup

norton.com/setup

10. I really like your post, I always like to read a quality content having accurate information regarding the subject. Thanks for sharing. It is such a very amazing post...reinstall malwarebytes paid version

11. Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for. malwarebytes downloads for already purchased

12. Great blog too very impressive! Excellent and nice post. This is such a great resource that you are providing and you give it away for free.I would like to thank you for the effort you have made in writing this article. visit: www.bitdefender.com/central

13. Great blog too very impressive! Excellent and nice post. webroot.com/safe

14. I really like your post, I always like to read a quality content having accurate information regarding the subject. Thanks for sharing. It is such a very amazing post...www.avg.com/activation

15. Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for. You may check our website also. www.malwarebytes.com/install

16. I really like your post, I always like to read a quality content having accurate information regarding the subject. Thanks for sharing. It is such a very amazing post...reinstall malwarebytes paid version

17. Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for. malwarebytes downloads for already purchased

18. Dear admin thank you so much for sharing this informative information with us, it's really useful for me. your blog is amazing. I wish you all the best for future comments. I have read it and I have also something here to share with you. norton.com/setup
www.norton.com/setup, Norton product key

19. Dear admin I really like the way you have to describe this blog it is awesome. All information is very informative I really appreciate your effort toward the reader. Thank you so much for this amazing content and I have also something to share here. www.office.com/setup, Office product key, office.com/setup

20. I like this concept. I visited your blog for the first time and became your fan. Keep posting as I am going to read it everyday. Check out the way to fix Error Code 2000-0151. Lean how you can fix it at your own or feel free to call our experts on our toll-free numbers or visit our website to know more!

21. McAfee Antivirus Tools are developed by Symantec Corporation, provides a complete Anti-Virus suite for PCs.o to your McAfee Antivirus software and Open it. · Enter the 25 digits alpha-numeric product key that was mailed to you
www.mcafee.com/activate
mcafee.com.com/activate"
www.Webroot.com/safe | Webroot.com/safe Product Key |
www.Webroot.com/safe | Webroot.com/safe Product Key |

22. www.malwarebytes.com/install :Malware may infect your computer when you click on a suspicious email attachment, but it is not restricted to that.

24. www.malwarebytes.com/install :Malware may infect your computer when you click on a suspicious email attachment, but it is not restricted to that.

26. Get Started with AOL Desktop Gold Download for Windows, 7, 8, 10 and MAC OS.