<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Cheryl Ng on Medium]]></title>
        <description><![CDATA[Stories by Cheryl Ng on Medium]]></description>
        <link>https://medium.com/@cherylngql?source=rss-4c3eccdf3bfb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*wFE9QxQce2fnjaQL</url>
            <title>Stories by Cheryl Ng on Medium</title>
            <link>https://medium.com/@cherylngql?source=rss-4c3eccdf3bfb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 07:29:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cherylngql/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Perspective engineering in Three.js]]></title>
            <link>https://medium.com/@cherylngql/perspective-engineering-in-three-js-b076822c07dd?source=rss-4c3eccdf3bfb------2</link>
            <guid isPermaLink="false">https://medium.com/p/b076822c07dd</guid>
            <category><![CDATA[dragcontrol]]></category>
            <category><![CDATA[firstpersoncontrol]]></category>
            <category><![CDATA[threejs]]></category>
            <category><![CDATA[pointerlockcontrol]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Cheryl Ng]]></dc:creator>
            <pubDate>Mon, 13 Aug 2018 04:55:43 GMT</pubDate>
            <atom:updated>2018-08-13T04:55:43.067Z</atom:updated>
            <content:encoded><![CDATA[<p>Having spent the past whole week figuring out Three.js has been one of the most challenging phase of my journey towards becoming a developer. Three.js is an awesome library that allows anyone with little or no background in mathematics/physics/engineering to simulate engaging 3D user experiences in your applications. With the myriad of built modules at <a href="https://github.com/mrdoob/three.js/tree/master/examples/js/controls">https://github.com/mrdoob/three.js/tree/master/examples/js/controls</a>, you can create the user interface you envision for your web app in no time. However, things get very tricky very quickly when you are trying to modify these control systems to suit your need.</p><p>Pointer lock controls give you the first-person control experience that is, in my opinion, the most important part of making an engaging 3D game. But, as the name suggests, you are locking your cursor. What if I want to keep my cursor for other kinds of interaction that are equally significant for the interface I was trying to build? The internet offers an excellent suggestion: do not use pointer lock API if you want to use your mouse. And that is exactly what I did not do.</p><p>Even with a physics and mathematics background, the working details of these control modules are daunting. Raycaster, yawObject, pitchObject. If these are the terms you are having trouble with manipulating in these modules, not to mention all the three vectors operations that you already have to keep in mind, I hope you find this article helpful.</p><p>Raycaster is a Three.js object ( THREE.Raycaster()). It is an object that contains information about that “ray” from your camera to your cursor. Most significantly, Raycaster.ray.direction gives you the unit vector pointing from your camera to your cursor. Raycaster is very useful when you are building a highly cursor-interactive system.</p><pre>let renderer = new THREE.WebGLRenderer()<br>let raycaster = new THREE.Raycaster()<br>let mouse = new THREE.Vector2()<br>const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)</pre><pre>function onMouseMove(event) {<br>  event.preventDefault()<br>  let rect = renderer.domElement.getBoundingClientRect()<br>  mouse.x = (event.clientX - rect.left) / rect.width * 2 - 1<br>  mouse.y = -((event.clientY - rect.top) / rect.height) * 2 + 1<br>  ...<br>}</pre><pre>raycaster.setFromCamera(mouse, camera)</pre><p>To offer a little more explanation, the code snippets above shows how mouse is usually defined in the 3D world. With this definition, when your cursor is in the center of your perspective, its x and y values are both 0. At the corners of your screen, they are either -1 or 1.</p><p>With a raycaster properly defined, you can then decide how you want objects in your 3D world to interact with a cursor.</p><pre>let intersects = raycaster.intersectObjects(objects)</pre><p>Here objects is left undefined, but it is an array where you can keep every Three.js objects you want the raycaster to interact with. intersects now holds an array of all objects that your cursor intersects. You can move these intersects with your cursor like in the Drag Control system.</p><p>Yaw objects and pitch objects are Three.js 3D objects. When I first encounter these variables when diving into the Pointer Lock Control, I have no idea they are variables with actual meaning. If only I have did the research earlier, I might have understood them more easily.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BOwbHOyhLlo7jM_NfoQwEQ.png" /></figure><p>So imagine the person you are controlling in your 3D game as the aircraft show above. Basically, yaw is the direction when you try to turn on your foot, and pitch is when you try to look up and down. That’s it! I had no idea! If you have a physics background, yaw is the phi direction and pitch is the theta! (Sorry mathematicians, I know you guys have it the other way round). With Pointer Lock Control, you would like to be able to turn left and right or look up and down, so you have to associate trackpad movements to the rotations of the yaw and pitch objects.</p><pre>let pitchObject = new THREE.Object3D()<br>let yawObject = new THREE.Object3D()</pre><pre>pitchObject.add(camera) //camera becomes pitchObject&#39;s child<br>pitchObject.rotation.x = Math.PI/2 //initializing to some angle</pre><pre>yawObject.position.y = 0 //initializing to zero height<br>yawObject.add(pitchObject) // pitchObject becomes yawObject&#39;s child</pre><pre>function onMouseMove(event) {<br>  ...<br>  yawObject.rotation.y -= movementX * 0.002<br>  pitchObject.rotation.x -= movementY * 0.002<br>  ...<br>}</pre><p>The first time I encountered the above code they look completely alien and I have no intention to attempt to understand them at all. But with the help of the above diagram, it is simple enough. The initialization code is adding pitchObject as the child of camera and adding yawObject as the child of pitchObject, so that rotating a parent will render the same effect on the child. Why do we want that? Remember we are talking about a concrete airplane, what will happen if the wings and the body rotates individually?</p><p>For our onMouseMove function, we can typically imagine the yawObject as the camera. In fact, when we are in pointer lock mode, to get the orientation of the camera, we have to get the yawObject’s orientation. It is, after all, where we, as the pilots, are facing at any given point of time. When we move our mouse to the right or to the left, we are rotating about ourselves, the y-axis. When we move our mouse up and down, we want to look up and down, so we are turning about the plane’s wing. Straightforward, no?</p><p>Now, what’s the problem with having a pointer lock controlled and cursor-interactive environment for my users, I just don’t have to hide my mouse right? Is raycaster.ray.direction still pointing from the camera to the mouse? Yes. But is camera still our perspective in this 3D world? NO.</p><p>I will make the context for this article clear for the purpose of explanation. I was actually trying to drag objects with my cursor while being able to have first person control with pointer lock API, all while still keeping my movement controlled by keys. What I failed to realize earlier was that, the position of the objects in my 3D world no longer have a consistent relationship with the camera. What should be my perspective now is not the camera, but the yawObject.</p><p>Actually, the camera is still our window to the world, but the camera is now a GRANDCHILD of the yawObject, and hence it’s position is with respect to the yawObject.</p><p>When trying to move my perspective using WASD control, updating the camera’s position has unexpected effect. The below was what I was trying to do which is not right.</p><pre>function onDocumentKeyDown(event) {<br>  switch (event.which) {<br>    case 87: // W<br>    _camera.position.z -= 1<br>    ...<br>  }<br>}</pre><p>The code above moves the camera away from the yawObject, and now re-orienting with the cursor after supposedly re-positioning ourself with key events moves our perspective, the camera, away from the origin which is where we are! Super confusing. That’s what I get for diving right into Three.js without doing research on 3D perspective engineering.</p><p>The above code should have been replaced by the code below.</p><pre>function onDocumentKeyDown(event) {<br>  switch (event.which) {<br>    case 87: // W<br>    yawObject.translateZ(-1)<br>    ...<br>  }<br>}</pre><p>An important point to note here is that, we have to use the translateX/Y/Z methods for Three.js 3D object, which magically takes care of transformations of our reference frame as our cursor moves to orientate the view of our camera.</p><p>And now you should be able to a first-person control system with a cursor!</p><p>This article is far from technically correct and I am fully aware that I did not explain many of the low-level details. While I wish I could do that, I have only been exposed to so much awesomeness of Three.js in a week. I will update this article as frequently as I get enlightenments from my project.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b076822c07dd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Full-stack workflow]]></title>
            <link>https://medium.com/@cherylngql/full-stack-workflow-cd70390f48bb?source=rss-4c3eccdf3bfb------2</link>
            <guid isPermaLink="false">https://medium.com/p/cd70390f48bb</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[sequelize]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[full-stack]]></category>
            <dc:creator><![CDATA[Cheryl Ng]]></dc:creator>
            <pubDate>Sun, 22 Jul 2018 16:11:25 GMT</pubDate>
            <atom:updated>2018-07-22T16:11:25.459Z</atom:updated>
            <content:encoded><![CDATA[<p>My first time developing a full-stack javascript application solo without a workflow guide went unexpectedly smooth and I thought I should document my workflow for future references.</p><p>My project was to develop a single page web application for a hypothetical school’s CTO to manage campuses and students. I used react, redux for my front end, express and sequelize for my back end.</p><p>I am not good with planning in advance. At all. So I am really surprised my instinct, or rather the experience from the many workshops I have done at Fullstack Academy, led me on the right path.</p><p>The very first thing I did was my schema design. For the application I was building, it was really simple and straightforward. And being familiar with Sequelize helps data visualization. Association declaration like, hasOne, belongsTo, hasMany on Sequelize simplify the relational structure of your data. Clearly, student belongs to one and only one campus, while a campus can have many students. In larger scale application, it is probably not the best idea to decide everything your database is going to have, but the key components should not change. For example, we can add reviews to the campus, given by the students. Adding this part at a later stage of your project requires all reviews to be associated with a certain campus, given by a specific student, which is a rather minor change. You just have to attach a campus id and a student id to the reviews without changing existing data. However, if you later on decide that you want to manage teachers in your application too, that involves major modification to your database since teachers belongs to some campus(es) and a student can have multiple teachers and vice versa!</p><p>So, carefully plan your schema design with at least the key structures you want your application to have and do not change those! If you do, it might be simpler to just start from scratch.</p><p>With the schema design done, I took a step back and thought about my front end. What are the most important aspects of my application? Well I definitely want to see all students and all campuses. I know that I probably want to have the ability to view a single student and a single campus too, but I really suggest the ‘zooming in’ approach. Some of my peers are particular about details and went ahead with more isolated components. They told me they ended up with lots of modifications and changes to make. I haven’t really tried that approach, but it doesn’t seem natural to me to have the furnitures bought before you know the layout of your home. You might only realize later that, you need a way to get to this isolated component, and without the big picture it is really hard to visualize!</p><p>I decided that I would start with getting all campuses and all students to display first. I started with building my api routes (and of course seeded some data in my database first so that I could check my api routes were indeed working). What do I need these api routes for? To fetch the information for my front end! So I defined action creators and reducers to handle these actions. You may or may not be using redux, so you can just have your front end component fetch data from the api.</p><p>The above flow works well for the big picture components, but not so much for the smaller, more isolated ones in my opinion. For example, now I have a front end react component that fetches all campuses and display them. What if I now want to delete some of the campuses? Ok start making an api route for deleting! And what if you want other functionality on the same component? To add a campus? To edit one? Or even to search for a campus by name or by student? I believe it is very easy to lose track of what features you are working on if you always go back to the api, and start building your feature bottom-up. Once you have rented or bought your home, you wouldn’t be “Oh I’d really like to have a sofa, a bed and a TV, let me go get them first and decide how to position them later!”, you are more likely to be “Ok, I want this room to be my living room, let me design how I want it to look like. I think I’d like a sofa and a TV here. Oh and definitely a bed in my room!”. What’s the difference? You don’t buy your furnitures until you have planned where to put them! Sorry if you hate this analogy, I like it though. So, work top-down on features! I want a search bar here. Let me just build a search bar that does nothing first! Okay, then to make it do what it is supposed to do, let me write this function to invoke the search through the database! And then, yes, I need the api route to do that.</p><p>I really prefer this top-down approach for features, because it gives you visual feedback as to whether the feature is looking like how you wanted it to look. But, I know, I know, this is all personal preference. I can’t even guarantee that I would stick to this workflow for the next 6 months.</p><p>Whether you choose top-down or bottom-up, do try to stick to the feature by feature approach. Unless you already have the application planned so well and have a todo list in case you forget to implement a route for a certain feature, I definitely do not recommend trying to come up with every api you think you are going to use, or draft up every react components you would like the user to see. Why? Because you can’t test! You do all the front end at once, then the back end, now you have a working application, something broke! What causes it? It can be anything! All developers I know suggests you build your application one feature at a time. You want to see all campuses? Work on that and do not try to display students until you get all your campuses displayed as you would like them to!</p><p>To summarize, I am going to use my favorite analogy again.</p><p>Buy your home first before you play how your kitchen, your living, your bedroom are going to look like!</p><p>Decide how your kitchen, your living, your bedroom are going to look like before you buy the furnitures.</p><p>DO NOT buy every furnitures you think you need for the whole house at once! Buy each as you design your home!</p><p>Good luck and have fun!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cd70390f48bb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>