TetroniMike's Forum Posts

  • Just sharing knowledge! Edit: formatting pseudocode is a nightmare apparently

    A few days ago I ran into an issue. I'm "flipping" a 2D object 180deg via 3D Euler rotation over the Y axis (object.RotationY or object -> Set Euler rotation), in a game using a 2D camera with Perspective projection, because it looks more interesting and engaging than my initial solution which was to just "squash" its width down to 0 then back up to its original ImageWidth. The object is a playing card on its face showing its back, and when it "disappears" to the user's perspective at 90deg to the camera (the equivalent of 0 width), I update the card sprite to show the face of the card.

    Except... with Perspective projection the angle at which it disappears depends on the position in the layout, and is not necessarily 90deg. So how do we get the "disappearing angle" of this object so that we can properly "flip" the sprite at the exact moment that the sprite's underside starts showing?

    Well, we need the angle from the object to the camera, right? So I tried using this code:

    screenCenter = ViewportWidth(CardVisuals.LayerNumber)*(VanishingPointX*0.01)+ ViewportLeft(CardVisuals.LayerNumber)

    disappearingAngle = 180 - angle(CardVisuals.X, CardVisuals.Z, screenCenter, ??? )

    ...but I realized, as you can intuit with the '???' above: what is the camera's "height" when it's a 2D camera?? Construct HAS to have some value representing camera height so that it can render 3D with depth, but unless we're using a 3D camera (this project is not), that value is not exposed!

    SO, the brilliant Skymen helped me out. He figured out the math by digging in to Construct 3's code, and I created a Function:

    static const number FIELD_OF_VIEW = 45

    static number TAN_FOV_DIV_2 = 0

    On start of layout: Set TAN_FOV_DIV_2 = tan(FIELD_OF_VIEW/2)

    On function GetCameraZ(Params: layerID) -> Number:

    number normalScale = 0

    number defaultZ = 0

    Set normalScale = (LayerScale(layerID) * LayoutScale-1) * LayerScaleRate(layerID) + 1

    Set defaultZ = ViewportHeight(layerID) / (2 * TAN_FOV_DIV_2)

    Set return value = defaultZ / normalScale

    I start by caching TAN_FOV_DIV_2, as this is a constant value based on FIELD_OF_VIEW, which will never change (NOTE: if you change Field Of View in the project properties, update it here to match!!), so that we don't need to do the math every time we call GetCameraZ. The rest of the pseudocode above explains the function and the math it uses.

    OK SWEET, now we can get disappearingAngle by doing the following:

    screenCenter = ViewportWidth(CardVisuals.LayerNumber)*(VanishingPointX*0.01)+ ViewportLeft(CardVisuals.LayerNumber)

    disappearingAngle = 180 - angle(CardVisuals.X, CardVisuals.Z, screenCenter, Functions.GetCameraZ(CardVisuals.layerNumber) )

    Now, as I flip the card via its Euler RotationY value, I can watch for RotationY to be > disappearingAngle, and update CardVisuals to show its face sprite! I also have it mirror the sprite when the card's face is showing.

    Here's before and after gifs - before simply uses 90deg, after uses our new disappearingAngle. Watch the cards on each end and you can see them update their visuals a little early or a little late:

    Before | After

    Here's the camera math copied directly from my project:

    {"is-c3-clipboard-data":true,"type":"events","items":[{"eventType":"group","disabled":false,"title":"Camera Math","description":"","isActiveOnStart":true,"children":[{"eventType":"comment","text":"var defaultZ = viewportHeight / (2 * tan(fovY / 2))\nvar normalScale = (layer.scale * layout.scale - 1) * layer.scaleRate + 1\nvar cameraZ = defaultZ / normalScale"},{"eventType":"variable","name":"FIELD_OF_VIEW","type":"number","initialValue":"45","comment":"","isStatic":true,"isConstant":true},{"eventType":"variable","name":"TAN_FOV_DIV_2","type":"number","initialValue":"0","comment":"","isStatic":true,"isConstant":false},{"eventType":"block","conditions":[{"id":"on-start-of-layout","objectClass":"System"}],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"TAN_FOV_DIV_2","value":"tan(FIELD_OF_VIEW/2)"}}]},{"functionName":"GetCameraZ","functionDescription":"Get 2D camera's z height","functionCategory":"","functionReturnType":"number","functionCopyPicked":false,"functionIsAsync":false,"functionParameters":[{"name":"layerID","type":"number","initialValue":"0","comment":""}],"eventType":"function-block","conditions":[],"actions":[],"children":[{"eventType":"variable","name":"normalScale","type":"number","initialValue":"0","comment":"","isStatic":false,"isConstant":false},{"eventType":"variable","name":"defaultZ","type":"number","initialValue":"0","comment":"","isStatic":false,"isConstant":false},{"eventType":"block","conditions":[],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"normalScale","value":"(LayerScale(layerID) * LayoutScale-1) * LayerScaleRate(layerID) + 1"}},{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"defaultZ","value":"ViewportHeight(layerID) / (2 * TAN_FOV_DIV_2)"}},{"id":"set-function-return-value","objectClass":"Functions","parameters":{"value":"defaultZ / normalScale"}}]}]},{"eventType":"block","conditions":[],"actions":[]}]}]}

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thanks for the detailed answer Ashley!

  • Is it possible to set a maximum fps, while still allowing for dynamic framerate? I've considered doing the following, but I fear that will have unintended consequences if/when the game taxes the device such that it would drop under the maximum:

    1. Set project default Framerate mode to V-Synced

    2. Watch FPS. If it climbs above 90 (averaged over a short period of time), set Framerate mode to Fixed, 90fps

    I'd like my game to appear to run smoothly, of course, but there is no reason it needs to hit 240hz on the latest high-performance mobile phones. In fact, I feel like most folks would appreciate the battery savings of keeping the framerate at a reasonable level, vs constantly targeting the maximum the device can handle.

    So I guess my question is, given the above, if I set it to Fixed at 90fps, and the game struggles to hit that target, will I notice slowdown and jankiness? (edit: upon reading the docs, it appears that YES I will notice slowdown and jank)

    And if the answer to the above is YES, (it is yes) would I be able to fix this by watching the FPS while in Fixed mode and if it dips below, let's say 80fps (averaged over a short period of time), switch back to V-Synced mode?

    Tagged:

  • Found this, which may explain it:

    https://www.construct.net/en/forum/construct-3/general-discussion-7/not-work-mobileadvert-ios-159106#forumPost1037989

    Post by Artpunk:

    I found that the MobileAdvert > Test Mode property doesnt seem to work for iOS in the same way it does for Android.

    In order to test ads on iOS, you have two options:

    You can use the test ad-unit IDs that AdMob provides (ths is the easiest way to test)

    You replace your real ad-unit ID with test ad-unit IDs.

    https://developers.google.com/admob/ios/test-ads

    The second option is to add you test device (iPhone, or iPad) to 'Test Devices' in your AdMob account. To do that you need to use an app to retrieve your IDFA number from Apple device. Doing it this way, you can use your real ad-unit IDs.

    https://support.google.com/admob/answer/9691433

    In both cases, you export your xcode project from C3 > open Xcode on Mac > connect your test device to Mac > build project, game launches on device and you should get ads.

    So I suppose I'll test a new build with Test Mode off. I already have my devices registered as test devices in AdMob. We'll see how it goes!

  • No, I wait for the user to request a video ad, then do 'Create rewarded advert'. In my testing it generally only takes a few seconds to create/load a rewarded ad. My 'On rewarded complete' code just gives the user their reward.

    Do subsequent 'Create rewarded advert' processes take a significantly longer time (e.g. multiple minutes), and Creating well ahead of when I actually need the ad will fix the issue?

  • (Continuing the conversation from this post: https://www.construct.net/en/forum/construct-3/how-do-i-8/ios-reward-ad-show-once-184263?kws=rewarded#forumPost1115233 )

    When testing my app on iOS, on an actual iPhone device, installed for testing via XCode, the 'Rewarded advert' only shows once. If I continue playing my game and trigger the reward again, the ad never loads. Interstitials work fine, and banner ads work fine.

    Post by Resurrected Studio:

    The IOS Simulator shows only 1 reward add and multiple interstitial adds(confusing i know).

    Test on your mobile with remote preview to see the reward ads working :).

    So iOS Simulator limits the number of reward ads you can show, I guess. Is this also true when installing to an actual device from XCode? I'm experiencing exactly this (only 1 reward, multiple interstitial) on an actual device. Note: the MobileAdvert plugin is in "Test mode", could that be affecting this?

    The ads are working as intended on Android, both when installed manually via APK or from the Google Play Store on a Closed Testing track (both versions have MobileAdvert using "Test mode").

    • Post link icon

    Thank you Ashley! Noted!!

    • Post link icon

    I attempted replacing the "project-name" folder and the "www" folder, and that seems to have worked quite well. Please let me know if I'm mistaken and need to include other folders when updating an existing Xcode project.

    • Post link icon

    When I export to iOS as an Xcode project, and then update my game and make a new export, what files do I need to replace in the Xcode project folder? If I can just move over the updated project files and avoid all the setup and tweaking I otherwise need to do Xcode each time, that would be incredible.

    Here's a file/folder list of the root folder of the export:

    • project-name/
    • project-name.xcodeproj
    • project-name.xcworkspace
    • cordova/
    • CordovaLib/
    • frameworks.json
    • ios.json
    • platform_www/
    • Podfile
    • Podfile.lock
    • Pods/
    • pods-debug.xcconfig
    • pods-release.xcconfig
    • pods.json
    • www

    What files represent the actual game, that I can just copy over and overwrite in the project-name.ios.project folder?

    Tagged:

  • Without a layer you could draw them to a drawing canvas at 100 opacity and then make the canvas have opacity.

    Ah! I haven't messed with the canvas features yet, I'll try that!

  • I have a bunch of objects that can overlap. These objects can be in front or behind (z-wise) other objects in the scene. I'd like to darken all the COLLECTIVE pixels behind a selection of these objects, without having them darken each other. I cannot have the shaded area be its own layer, unfortunately, as the shading can exist between objects in the main layer. Please see the photo below for a very simplified example.

    Is this possible? How do I achieve this?

  • I got it working! First, whenever I call my function that captures the windows [X] close button, I first release control (to help prevent duplicate "listeners") by removing all listeners from the 'close' signal. You'll note the below code is very similar to previous solutions, but adds an extra line that I guess was a recent requirement to listen for 'close' signals: var gui = require("nw.gui")

    Browser -> Execute javascript:

    "var gui = require(""nw.gui""); var win_main=gui.Window.get(); win_main.removeAllListeners('close');"

    Then, I run a second command that listens for the 'close' signal.

    Browser -> Execute javascript

    "var gui = require(""nw.gui""); var win_main = gui.Window.get(); win.on('close', function() { c2_callFunction('nw.js close',[]); });"

    I have a Function titled "nw.js close" where I run the code I want to run when the player clicks the window [X] close button.

    And then when I want to release control I run just that code at the top of my post, removeAllListeners.

    This works in the latest NWJS 0.84.0 on Windows, and I'll be testing my update on Mac and Linux next week.

  • This is in response to this post, which I cannot reply to as it is over 1 year old:

    https://www.construct.net/en/forum/new-post?topicID=157025

    in turn, that post was a response to this post:

    https://www.construct.net/en/forum/construct-2/how-do-i-18/window-close-113911

    The above mentioned Browser -> Execute Javascript solution works on NWJS v0.54.0, but I can't get this to work with NWJS v0.84.0 - now I recognize that version is not officially supported by Construct 2, but everything else NWJS-related works just fine, this is the only item that is not working. Any ideas?

    This is for the game Hypnospace Outlaw, and before you suggest it we cannot port to Construct 3 for a number of reasons. I'm trying to update the game to fix (1) Steam Cloud Saves, (2) mod.io integration, and (3) issues with newer graphics cards that new NWJS versions seem to resolve. While I was at it I figured I'd add some quality-of-life fixes and additions, like overriding clicking the [X] button so that players don't accidentally close the game when they're trying to click something at the top-right corner of the window.

  • I did some Googling and found this, so take this answer with a grain of salt: if you have not, you may need to "Publish to the web". Via this article: philipdev.se/ideas/google-spreadsheet-data

  • Alright so I've learned a few things:

    1) AdvancedRandom can take a string of presumably any length - I used Update Seed and put in a string with over 1,000 characters and it worked as expected.

    2) Repeating the random() function 1,000,000 times BARELY noticeably lags, maybe 1/20 of a second.

    3) Updating the seed each time I use random() to "MYSEED_[n]" has no noticeable effect on FPS, even if calling it 100 times every single tick. (n = number of times I've used random())

    For both 2 and 3 I enabled "Replace system random" in the AdvancedRandom properties.

    So it looks like my current implementation will work fine (unless the player has been playing on a single save file for literally days without a game over). And so will your idea of updating to MYSEED_[n] each time random() is used :)