Create perspective with andengiine GLES2 - andengine

I was trying to create a game the involves the user walking into the screen. I found some idea on how to do this here being lead by this question. But the tutorial is based on GLES1 and I could not adopt it for GLES2.
Here is what the design looks like,
Here is what I tried so far,
I modified GLState and added
public void frustumProjectionGLMatrixf(final float fov_degrees, float aspect, float zNear, float zFar) {
this.mProjectionGLMatrixStack.glFrustumf(fov_degrees, aspect, zNear, zFar);
}
and I modified GLMatrixStack and added
public void glFrustumf(float fov_degrees, float aspect, float zNear, float zFar) {
float[] mTemp = new float[2 * GLMatrixStack.GLMATRIX_SIZE];
Matrix.setIdentityM(this.mMatrixStack, this.mMatrixStackOffset);
// Matrix.frustumM(this.mMatrixStack, this.mMatrixStackOffset, 0, this.getWidthRaw(), this.getHeightRaw(), 0, 10, 300);
Matrix.perspectiveM(this.mMatrixStack, this.mMatrixStackOffset, fov_degrees, aspect, zNear, zFar);
Matrix.setLookAtM(this.mMatrixStack, this.mMatrixStackOffset, 0, 120f, zNear * 10, 0, 0, 0f, 0, 1, 0);
Matrix.scaleM(this.mMatrixStack, this.mMatrixStackOffset, 1, -1, 1);
System.arraycopy(this.mMatrixStack, this.mMatrixStackOffset, mTemp, GLMatrixStack.GLMATRIX_SIZE, GLMatrixStack.GLMATRIX_SIZE);
Matrix.multiplyMM(this.mMatrixStack, this.mMatrixStackOffset, mTemp, GLMatrixStack.GLMATRIX_SIZE, mTemp, 0);
}
and I created the camera as follows
this.camera = new Camera(0, 0, WIDTH, HEIGHT) {
#Override
public void onApplySceneMatrix(final GLState pGLState) {
super.onApplySceneMatrix(pGLState);
this.setZClippingPlanes(-3000, 3000);
setFrustum(pGLState);
}
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private void setFrustum(GLState pGLState) { // set field of view to 60 degrees
float fov_degrees = 60;
float fov_radians = fov_degrees / 180 * (float) Math.PI;
float aspect = this.getWidth() / (this.getHeight());
float camZ = this.getHeight() / 2 / (float) Math.tan(fov_radians / 2);
pGLState.frustumProjectionGLMatrixf(fov_degrees, aspect, -100, 100);
pGLState.flush();
}
};
But there is nothing drawn on the screen. The scene is totally black. Any if there is no way to adapt perspective in AndEngine GLES2, other tricks to make this work will be appreciated.

Honestly i gave up and made my own pseudo 3D projection engine. The problem with this is that it is very slow, as it is all done without GL coding.
I had to use Sprite groups and 1 really big mesh(Mesh being the road), which really hurt the layering on the Z axis if you have rolling hills or tunnels etc. You will see in the pictures below.
In the end i had to create 3 sprite groups and 3 meshes. Detect if a sprite was behind the ground/tunnel/etc.
From that point backward(into the distance) i would
1: take sprites out of the first group and into the 2nd
2: stop all the first meshes vertices and draw the 2nd mesh from that point onward
3: repeat, if collision occurred behind the 2nd mesh, start the 3rd
hard to explain, took me a while to get it working and it's very quirky with a lot of restrictions. It took me months to get the FPS from 3 back to 60.
Without Layering (Trees show over the ground)
With collision detection layering
I can go into more detail if you would like.
Also if anyone has a better way of doing this, drop in some information for us.

Related

How to write dynamically to whole stencil buffer in Unity

What do I want to achieve ?
I'd like to achieve an effect in Unity3D, where I superpose a few cameras on top of each other. Each cameras would draw to a specific area of the screen. If possible, I'd like these areas to change dynamically.
I am using unity (latest version), and URP.
How technically I see it :
For implementation and performances reasons, it seems writing to the stencil buffer is the way to go. That way, I can only render what part of the screen I want for each camera. It is also quite easy once the stencil is made, cause the ForwardRendering settings in Unity offer such capabilities out of the box.
What I can't figure out :
The problem is, I don't know to efficiently write to the whole stencil buffer (each frame). The best way would be to use a compute shader (or maybe a simple script), that directly write the values after some calculations. Is there a way for that ? If yes, How ?
Another alternative may be to use a transparent quad in front of one of each camera, and to write to the stencil buffers like that. But 1) It seems there exist a SV_StencilRef keyword in the fragment buffer, but not supported by Unity yet ? 2) I will still lose performance nevertheless.
Thanks for any help / ideas about how to tackle this problem.
Edit (Clarification) : I'd like to be able to render free shapes, and not only rects, which prevent the use of the standard ViewportRect.
After some search, I found the Voronoi split screen to be quite similar (with a technical view) to what I'd like to achieve (See here)
If I understand correctly, you only need to play with the different camera Viewport Rect (https://docs.unity3d.com/ScriptReference/Camera-rect.html) to determine what camera should render what part of the screen.
Response to comment: no, it's not stretched. Here is an example with four cameras:
Create a scene with four cameras, add this script to it and add the cameras to the array on the script. I added the _movingObject just to see something moving, but it's not necessary.
using UnityEngine;
public class CameraHandler : MonoBehaviour
{
[SerializeField] private Transform _movingObject;
[SerializeField] private float _posMod = 10.0f;
[SerializeField] private float _cameraPosMod = 0.1f;
[SerializeField] private Camera[] _cameras;
private void Update()
{
float t = Time.time;
float x = Mathf.Sin(t);
float y = Mathf.Cos(t);
if (_movingObject) _movingObject.position = new(x * _posMod, 1.0f, y * _posMod);
Vector2 center = new(0.5f + x * _cameraPosMod, 0.5f + y * _cameraPosMod);
// bottom left camera
_cameras[0].rect = new(0.0f, 0.0f, center.x, center.y);
// bottom right camera
_cameras[1].rect = new(center.x, 0.0f, 1.0f - center.x, center.y);
// upper left camera
_cameras[2].rect = new(0.0f, center.y, center.x, 1.0f - center.y);
// upper right camera
_cameras[3].rect = new(center.x, center.y, 1.0f - center.x, 1.0f - center.y);
}
}
Not exactly an answer to your question about stencil buffer but I had a (hopefully) similar use case recently.
The main issue: In the URP Camera stack
If your camera is set to Base it will overdraw the entire screen
you can not adjust the Viewport on any Overlay camera
You can actually try to set the viewport via code -> result your camera renders only the correct part of the scene ... but it gets stretched to the entire screen ^^
What I did in the end was
Leave all content and cameras at the origin position
Apply according masks to filter the content per camera
Make your camera Overlay (as usual)
go through a custom Camera.projectionMatrix
m_Camera.projectionMatrix = Matrix4x4.Translate(projectionOffset) * Matrix4x4.Perspective(m_Camera.fieldOfView, m_Camera.aspect, m_Camera.nearClipPlane, m_Camera.farClipPlane);
where the projectionOffset is an offset in viewport space (normalized 0 - 1) from the bottom left corner.
For example in my case I wanted a minimap at 400, 400 pixels from the top-right corner so I did
var topRightOffsetPixels = new Vector2(400, 400);
var topRightOffsetViewport = Vector2.one - new Vector2(topRightOffsetPixels.x * 2 / Screen.width, topRightOffsetPixels.y * 2 / Screen.height);
m_Camera.projectionMatrix = Matrix4x4.Translate(topRightOffsetViewport) * Matrix4x4.Perspective(m_Camera.fieldOfView, m_Camera.aspect, m_Camera.nearClipPlane, m_Camera.farClipPlane);
See also Matrix4x4.Perspective

Unity mobile joystick problems

I am creating a mobile top down shooter with Unity and my problem is that aiming with the joystick feels very stagnant.
It's like the joystick knows about 100 different directions, but I need 1000. I hope this is somewhat understandable what I mean. The joystick just doesn't feel smooth.
Is there any way to change this? Something like increasing a sampling rate so that the screen on the smartphone detects even the smallest changes?
For the joystick I use the following asset:
https://assetstore.unity.com/packages/tools/input-management/joystick-pack-107631
The implementation of my rotation and trying to make it smooth looks like this:
float eulerY = (Mathf.Atan2(_JoystickShoot.Direction.x, _JoystickShoot.Direction.y) * 180 / Mathf.PI);
if(_LastJoystickShootEulerY == 0.0f || Mathf.DeltaAngle(eulerY, _LastJoystickShootEulerY) > 30)
_Weapon.GetBarrelContainer().transform.rotation = Quaternion.Euler(0, eulerY, 0);
else
_Weapon.GetBarrelContainer().transform.rotation = Quaternion.Lerp(Quaternion.Euler(0, _LastJoystickShootEulerY, 0), Quaternion.Euler(0, eulerY, 0), 0.5f);
_LastJoystickShootEulerY = eulerY;

Unity | Get backward vector and play turn anim

Struggling with the implementation of character rotation animation by 180 degrees.
For clarity, I made a Pivot, which indicates the target of the direction, then compares the angle between the direction of the character and the direction of the target, calculates the difference in the nearest angles and, if the difference is greater than 179, plays the Turn180 animation.
Works, but crooked.
Screenshot
Problem:
Since the control of the character and the target are identical, the script does not have time to calculate the difference and gives out values at full turn are not always needed. Like: (-178.6661), (169.8465), (168.1936) and so on.
I'm sure there is an easier way. The only thing that comes to mind is to compare the TargetPisition with the -transform.forward and then play the reversal animation. But I can't figure out how to do it. I need help with Movement and Target.
UPDATE
I rewrote the code from scratch more carefully, but the issue with the turn remains relevant. The target and the Player are turning at the same time, how do I tell the program that I'm going to turn in the opposite direction and play the animation?
void Movement() {
// WASD
float Horizontal = Input.GetAxis("Horizontal");
float Vertical = Input.GetAxis("Vertical");
Vector3 move = Quaternion.Euler (0, playerCamera.transform.eulerAngles.y, 0) * new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
//Movement
controller.Move(move * Time.deltaTime * playerSpeed);
if (move != Vector3.zero)
{
gameObject.transform.forward = move;
}
//
//Pivot
Pivot.transform.position = gameObject.transform.position;
PivotDirection.transform.rotation = gameObject.transform.rotation;
PivotTarget.transform.position = gameObject.transform.position + move;
Vector3 PivotTargetDirection = Vector3.RotateTowards(PivotTarget.transform.forward, move, 10, 0);
PivotTarget.transform.rotation = Quaternion.LookRotation (PivotTargetDirection);
PivotArrow.transform.rotation = Quaternion.Euler (0, playerCamera.transform.eulerAngles.y, 0);
//
// ReceivedAngle
var PlayerDirection = new Vector3(0, transform.eulerAngles.y, 0);
var TargetDirection = new Vector3(0, PivotTarget.transform.eulerAngles.y, 0);
var ReceivedAngle = Mathf.DeltaAngle(PlayerDirection.y, TargetDirection.y);
print(Mathf.RoundToInt(ReceivedAngle));
//
if (ReceivedAngle >= 180)
{
animator.Play("Turn 180");
}
}
P.S. Sorry for my English.

Splitting Screen in Areas to controll object

im kinda stuck with this problem.The idea is to have the blue square (player2) move when the top half of the screen is touched and the red square (player1) when the bottom half is touched.
I tried to make rect for the top and and bottom half and telling for example the red one to only move when the touch is within the bottom rect.
But the current behavior is like this: https://www.youtube.com/watch?v=yyVzuXFXipM
Player1 Script: https://www.codepile.net/pile/jAN8RgMl
Player2 Script: https://www.codepile.net/pile/6dxO7lX3
The coords i determined i would need:
coords in the middle might be inaccurate but the corners should be correct.
In general please post your code here and not in some external link!
At first glance: Be aware that you would have to provide the texts in screen pixel coordinates!
You are once starting at y = 0 and once at y = 5 .. that's just 5 pixels from the bottom!
You probably would rather want to use
var bottomRect = new Rect(0, 0, Screen.width, Screen.height / 2f);
and
var topRect = new Rect(0, Screen.height / 2f, Screen.width, Screen.height / 2f);
Btw If this is the only difference between your scripts you shouldn't use two different scripts but rather use the same script and only use different Rects like e.g.
public enum Player
{
Top,
Bottom
}
// Adjust this via the Inspector
public Player player;
and then
private Rect rect;
private void Awake ()
{
rect = player == Player.Top ? new Rect(0, Screen.height / 2f, Screen.width, Screen.height / 2f) : new Rect(0, 0, Screen.width, Screen.height / 2f);
}

check if centerPlane is not near edge in unity

I'm trying to make whack a mole game using project tango.
When user start the game, the program will create holes at random point for the moles to come out. Right now, I already can make the hole and spawn the mole at random, though I have a problem with the created hole.
The hole sometimes spawn at an edge contour of table and stuff and make the hole partially floating in the air. Is there any way to check if the centerPlane is near an edge or not?
Here's the screenshot of the problem I meant. I want the "hole" not to spawn on area that doesn't fit with it's height and width. Currently, I'm using farandole release.
EDIT 1:
I'm trying to do as Hristo suggest. But it doesn't work, the FindClosestPoint always return -1, even when I use the center of the screen. Here's the script I used. And for some additional info, I'm using the unitySDK and unity 5.5.2f1
bool CheckTheCorner(Camera cam,Vector3 planeCenter){
Vector2 firstPointInScreen = WorldToScreenConverter(cam,new Vector3 (planeCenter.x,planeCenter.y,planeCenter.z-holeheight));
Vector2 secondPointInScreen = WorldToScreenConverter(cam,new Vector3 (planeCenter.x,planeCenter.y,planeCenter.z+holeheight));
Vector2 thirdPointInScreen = WorldToScreenConverter(cam,new Vector3 (planeCenter.x-holewidth,planeCenter.y,planeCenter.z));
Vector2 fourthPointInScreen = WorldToScreenConverter(cam,new Vector3 (planeCenter.x+holewidth,planeCenter.y,planeCenter.z));
DebugText.text = m_pointCloud.FindClosestPoint (cam, new Vector2(Screen.width / 2, Screen.height / 2), 1).ToString ();
Vector3 firstPoint = m_pointCloud.m_points[m_pointCloud.FindClosestPoint(cam, firstPointInScreen, 1)];
Vector3 secondPoint = m_pointCloud.m_points[m_pointCloud.FindClosestPoint(cam, secondPointInScreen, 1)];
Vector3 thirdPoint = m_pointCloud.m_points[m_pointCloud.FindClosestPoint(cam, thirdPointInScreen, 1)];
Vector3 fourthPoint = m_pointCloud.m_points[m_pointCloud.FindClosestPoint(cam, fourthPointInScreen, 1)];
return false;
}
Vector2 WorldToScreenConverter(Camera cam,Vector3 worldPos){
Vector3 screenPos = cam.WorldToScreenPoint (worldPos);
return new Vector2 (screenPos.x,screenPos.z);
}
Ah yes, don't mind the return false one for the moment, I just put it there to avoid error since I'm still figuring out the FindClosestPoint.
What you can do is take the 4 corners on your plane and decide if they are all laying on a surface in the real world, if not you can make the plane elsewhere.
That can happen with the use of FindClosestPoint() method in the TangoPointCloud.cs. What that method does is makes a Raycast from your camera trough a certain point on the screen landing in the real world environment. The method then returns the index of that point. The list to search with the indexes is called m_points
So lets split it in steps:
Make 4 Vectors using the `FindClosestPoint().
Check if all 4 vectors are on the same plane (simple math).
If step 2 is true -> Instantiate your GameObject on that plane.
To get one of the vectors your code should be something like this:
Vector3 firstPoint = m_pointCloud.m_points[m_pointCloud.FindClosestPoint(Camera.main, new Vector2(Screen.width / 2, Screen.height / 2), 1)];
In this example I'm using the center of the screen as my Vector2 parameter. However you don't want the center of the screen . Instead you want the position of one corner from your plane translated as a screen point.
Hope that solves your problem. Cheers.