Smooth Window Movement And Manipulation | Voster Coaster | VR Roller Coaster Building And Tycoon Game

in #utopian-io6 years ago (edited)

Repository

https://github.com/ajayyy/VosterCoaster/

Smooth Window Movement

Before, the window would just teleport to the new location necessary. Now it slowly moves over using Slerp (spherical interpolation). It feels a lot better when dragging around the windows as they feel more realistic. Objects in real life do not teleport.

Window Resizing

Another important feature of windowing system is resizing. I decided to use a pinch to zoom system to make this window resizing. By pointing at the window and pinching your controllers together, you make it smaller. When you move your controllers away from eachother, they grow (as shown in the video).

resizingCurrentHitLeft = gameController.leftWindowHit;
resizingCurrentHitRight = gameController.rightWindowHit;

Vector3 leftPosition = resizingCurrentHitLeft.point;
Vector3 rightPosition = resizingCurrentHitRight.point;

if (!gameController.leftWindowBHit) {
    leftPosition = gameController.leftControllerObject.transform.position + gameController.leftControllerObject.transform.forward * leftDistance;
} else {
    leftDistance = gameController.leftWindowDistanceAway;
}

if (!gameController.rightWindowBHit) {
    rightPosition = gameController.rightControllerObject.transform.position + gameController.rightControllerObject.transform.forward * rightDistance;
} else {
    rightDistance = gameController.rightWindowDistanceAway;
}

Vector3 newSize = (Vector3.Distance(leftPosition, rightPosition) / Vector3.Distance(resizingStartHitLeft.point, resizingStartHitRight.point)) * resizingStartSize;

The program takes the points that the raycast from the controller hit the window. These points are then compared to the points that were originally hit when the resizing process was started (the trigger button was pressed with both controllers pointing at the window)

Smooth Resizing

Very similar to how the smooth movement works, resizing cannot be just teleporting to a new size. Using Slerp and a start and target size, these sizes can be interpolated.

//change size based on current animation
if (animatingResize) {
    transform.position = Vector3.Slerp(animatingStartPosition, animatingTargetPosition, (Time.time - animatingStartTime) * 20f);

    transform.localScale = Vector3.Slerp(animatingStartSize, animatingTargetSize, (Time.time - animatingStartTime) * 20f);

    transform.rotation = Quaternion.Slerp(animatingStartRotation, animatingTargetRotation, (Time.time - animatingStartTime) * 20f);

    if ((Time.time - animatingStartTime) * 20f >= 1) {
        animatingResize = false;
    }
}

Movement While Resizing

In real life, if you grab a paper and are dragging your fingers along it, you expect moving your hand to move the entire Window. To make this seem more realistic, I made the position of the window while it was being resized be the average position between the two points (or a 0.5 interpolation).

animatingTargetPosition = Vector3.Lerp(leftPosition, rightPosition, 0.5f);

Track Offset Issues

Partial Track Pieces Between Segements

One track segment is made up of many track pieces. The last piece on the track segment might not need to be full filled, so it ends up being a partial track piece. When a new track segment is created after, the rest of the partial track piece is used in the next track segment.

Before, the amount being used for that was being subtracted from the amount of curve tracks that were needed to be generated for the next segment, but that ended up not being correct. The correct way is to subtract it from the amount of tracks needed to be generated going from the end of the curve to the target.

Horizontal Offset

Each track piece is split up into 10 "bones". This is to make the track piece look curved. When a partial track piece is created, it might need to use only part of a bone.

Before, that bone's size was then shrunk accordingly, same with the angle it was at.

rails[i][r].transform.localPosition = ((percentageOfTrack - ((r - 1) / rollerCoaster.boneAmount)) * rollerCoaster.boneAmount) * defaultBonePosition;
rails[i][r].transform.localEulerAngles = ((percentageOfTrack - ((r - 1) / rollerCoaster.boneAmount)) * rollerCoaster.boneAmount) * currentAngle;

That turned out not to be accurate enough. Instead, that track piece has to just not exist, rounding down to the nearest bone.

rails[i][r].transform.localPosition = Vector3.zero;
rails[i][r].transform.localEulerAngles = Vector3.zero;

Pull Request

https://github.com/ajayyy/VosterCoaster/pull/18

GitHub Account

https://github.com/ajayyy

Sort:  

Thanks for the contribution, @ajayyy! In the video you posted everything does look really smooth, so great job on that!

Only problem I really have with your code is that it's super wide. I remember asking you if you were using ReSharper, but I can't remember what you answered, haha. I'm pretty sure running that will automatically keep your line width below a certain number, which definitely improves the code's readability in my opinion.

Also, as for your commit messages it would be best to keep the subject around 50 characters (if you need to write more, separate the subject from the body with a blank line and write more information there).


uncommented code that should have been uncommented for the previous c…

…ommit

Also for stuff like this you could use git commit --amend command since it, as you say yourself, should've been part of the previous commit.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thanks for the review! I do not have resharper, and have been manually shortening lines of code when I feel them to be too long for my screen. I got a new computer with a wider display, so that could be the issue. I am going to keep it to a more reasonable width instead of just monitor width from now on.

I had already pushed, so I really didn't want to force push, but as you say, I am working on this alone so I could easily force push, especially when force pushing to a non master branch. I have done that before, but I think this was just after branching so I didn't want to mess anything up.

I've added a reminder to download resharper in trello, so I should remember this time ;)

Thank you for your review, @amosbastian!

So far this week you've reviewed 7 contributions. Keep up the good work!

Hey, @ajayyy!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!

good stuff building up the steem blockchain

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 62205.55
ETH 2397.85
USDT 1.00
SBD 2.50