flying around 3D obj - rotation

I'm making a game space simulator. My spacesheep has to fly around the Earth in 3D.
I've made a simple controller for spacesheep:
public GameObject objToMove=GameObject.Find("Player");
public GameObject zeroPoint=GameObject.Find("Earth");
public float farFromZero=10;
void Update(){
Vector2 Ldirection;
Ldirection.x = Input.GetAxis ("Horizontal");
Ldirection.y = Input.GetAxis ("Vertical");
MoveObj (objToMove, Ldirection.x, Ldirection.y);
}
I also made a separated class just because i want to get control from another class (touchscreen)
public void MoveObj(GameObject LobjToMove, float AxsHoriz, float AxsVert){
AxsHoriz = AxsHoriz * speed;
AxsVert = AxsVert * speed;
AxsHoriz *= Time.deltaTime;
AxsVert *= Time.deltaTime;
LobjToMove.transform.Translate(AxsHoriz, AxsVert, 0);
//spase has to look at the earth
LobjToMove.transform.LookAt (zeroPoint.transform.position);
ZeroPointMagnitude (LobjToMove);
}
//fixing the distance between earth and spacesheep
void ZeroPointMagnitude(GameObject LobjToMove)
{
Vector3 direction = zeroPoint.transform.position - LobjToMove.transform.position;
float distance = direction.magnitude;
if (distance > farFromZero||distance < farFromZero) {
LobjToMove.transform.position += direction.normalized * (distance - farFromZero);
}
}
I've put camera in the spacesheep.
It looks good and spacesheep flies around the earth but it looks like it flying around the pivot, just making radius smaller when it reaches poluses and it can't go around if I press up or down - it stops at the bottom and upstairs
I've tried to make my it fly well for 3 months, I used Raycast or Quaternions and it doesn't work as I want it.
Looks like it going to take few months more and I ask you to help me with flying player around the object without stopping on puluses (or camera flipping)

so here we go
Vector3 rotDir=new Vector3(AxsVert,AxsHoriz,0);
float gypotenuse=Mathf.Sqrt(AxsHoriz*AxsHoriz+AxsVert*AxsVert);
gypotenuse=(gypotenuse>1)?1:gypotenuse;
objToMove.transform.RotateAround (zeroPoint.transform.position,
objToMove.transform.TransformDirection(rotDir),
gypotenuse
);

Related

Unity2D: How do I move the player to the touch position

I can't figure it out, how do I move the Player slowly, scaleable with a speed value to the point, where the touch happens?
My Camera is attached to the player.
You should use Vector2.Lerp, Input.GetTouch, and Touch.Position. Here is a code example that prints the current touch position:
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
Debug.Log(Input.GetTouch(0).position);
}
Now, we should add this to a certain position in the world. We can use Camera.ScreenPointToRay for this part:
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit2D hit;
Physics2D.Raycast(ray, out hit);
Debug.Log(hit.point);
}
Here, we actually get the position in the world that you have pressed. We can then use this along with Lerp to move an object to the position:
public float scaleFactor = 0.2f; //how fast to move object.
public GameObject moving // drag the object you want to move in this slot.
public Vector2 Lerped;//Vector3 if you’re working in 3D.
public float time = 0f;
public bool lerping = false;
private Vector2 newPos = new Vector2();
//Vector3 if you’re working in 3D.
RaycastHit2D hit;//remove the 2D after RaycastHit if you are in 3D. If you are in 2D, leave it.
float dist;
...
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
Physics2D.Raycast(ray, out hit);//same with this one. Remove the 2D if you are in 3D.
Debug.Log(hit.point);
dist = Vector2.Distance(moving.transform.position, hit.point);
//Vector3 if you’re in 3D.
lerping = true;
}
if (lerping)
{
time += Time.deltaTime * dist * scaleFactor;
lerp(hit.point.x, hit.point.y, hit.point.z);
//remove hit.point.z if you are in 2D.
}
else
{
time = 0f;
}
if (moving.transform.position == new Vector3(hit.point.x, hit.point.y, hit.point.z);
//remove hit.point.z if in 2D, and switch Vector3 to Vector2 if in 2D.
{
lerping = false;
}
moving.transform.position = newPos;
And then in another part of the script:
public void lerp(float x, float y, float z)
//remove the float z and the comma before it if you are in unity 2d.
{
Vector3 pos = new Vector3(x, y, z);
//Vector2 and no z if you’re in 2D.
newPos = Vector2.Lerp(moveObj, pos, time);
//Vector3 if in 3D.
}
This is a pretty large script, and it’s pretty hard to explain, so I will say this: do what the comments tell you, and tell me if you get any errors. I haven’t actually tested the script, so if anyone notices them, please tell me.
Now, what the script does: basically, it gets the point of where you touched, and makes the player slowly go towards it (the speed changes by the distance and scaleFactor variable of which you can change.)

bounce after collision doesnt work and object clips through terrain

EDIT I changed my code to the one below but it still clips through the terrain sometimes. It also likes to bounce on corners where it should slide off.
I tried implementing my own collision for a little game I am making. This is the code for my ball class.
class Ball {
//config
float gforce = 1;
float friction = 0.8;
float elasticity = 0.5;
//vars
PVector position;
PVector velocity = new PVector(0, 0);
Ball(PVector p) {position = p;}
void render(){fill(255, 255, 0); noStroke(); circle(position.x, height-position.y, 16);}
PVector bounce(PVector v, PVector n){ //calculate velocity after bounce
PVector u = PVector.mult(n,v.dot(n)/n.dot(n));
PVector w = PVector.sub(v, u);
return PVector.sub(PVector.mult(w, friction), PVector.mult(u, elasticity));
}
Boolean intersect(PVector c, PVector p1, PVector p2, float r){
if (c.dist(p1)<=r || c.dist(p2)<=r){return true;}
float len = p1.dist(p2);
float dot = ( ((c.x-p1.x)*(p2.x-p1.x)) + ((c.y-p1.y)*(p2.y-p1.y)) )/(len*len);
PVector closest = PVector.add(p1, PVector.mult(PVector.sub(p2, p1), dot));
float d1 = closest.dist(p1);
float d2 = closest.dist(p2);
if (d1+d2>=len-0.1 && d1+d2<=len+0.1){if(closest.dist(c)<=r){return true;}}
return false;
}
Boolean intersect(PVector p1, PVector p2, PVector p3, PVector p4){ //if the line p1p2 intersects line p3p4
float uA = ((p4.x-p3.x)*(p1.y-p3.y) - (p4.y-p3.y)*(p1.x-p3.x))/((p4.y-p3.y)*(p2.x-p1.x) - (p4.x-p3.x)*(p2.y-p1.y));
float uB = ((p2.x-p1.x)*(p1.y-p3.y) - (p2.y-p1.y)*(p1.x-p3.x))/((p4.y-p3.y)*(p2.x-p1.x) - (p4.x-p3.x)*(p2.y-p1.y));
if (uA>=0 && uA<=1 && uB>=0 && uB<=1){return true;}else{return false;}
}
void moveBall(){
//if the ball is stationary
if(velocity.mag()==0 || velocity.mag()==1 || velocity.mag()==2){
PVector v = new PVector(min(280, max(-280, mX-mouseX)), min(280, max(-280, mouseY-mY))).div(28);
int n = int(max(abs(v.x), abs(v.y)));
v.normalize().mult(25);
//render arrow
if(mX!=0 && mousePressed){
strokeWeight(5); stroke(#75D5FD); line(mX, mY, mouseX, mouseY);
for (int i=0; i<n+1; i++){
noStroke(); fill(510*(float)i/12, 510*(1-(float)i/12), 0, 55+200*(1-(float)i/12)); circle(mX+(v.x*i), mY-(v.y*i), 15);
}
}
if(mX==0 && mouseP){mX=mouseX; mY=mouseY; mouseP=false;}
if(mX!=0 && mouseR){b.velocity = new PVector(min(280, max(-280, mX-mouseX)), min(280, max(-280, mouseY-mY))).div(8); mX=0; mouseR=false;} //apply velocity
}else {
//if the ball is still, do not allow additional movement
if(mX!=0 && mousePressed){stroke(200); line(mX, mY, mouseX, mouseY);}
if(mX==0 && mouseP){mX=mouseX; mY=mouseY; mouseP=false;}
if(mX!=0 && mouseR){mX=0; mouseR=false;}
}
}
void collision(){
//test collision with terrain
for (int i=1; i<l.points.length; i++){
PVector centerout = PVector.div(velocity, velocity.mag()).mult(8);
strokeWeight(5); stroke(255,0,0); line(position.x, height-position.y, position.x+centerout.x+velocity.x, height-position.y-centerout.y-velocity.y);
if(intersect(position, l.points[i-1], l.points[i], 7) || intersect(position, PVector.add(position,velocity,centerout), l.points[i-1], l.points[i])){
velocity = bounce(velocity, l.normals[i-1]);
}
}
}
void move() {
moveBall();
collision();
position.add(velocity);
if(velocity.y>-10){velocity.y-=gforce;}
if(velocity.mag()<0.5){velocity.x=0; velocity.y=0;}
}
}
There is another object, l (which is the terrain), and it stores the array points[] which contains all the coordinates for the terrain. There is a line connecting each point and the ball detects if its velocity vector intersects that line, or if the ball itself intersects that line.
When i drag and release my mouse, it changes the velocity to whatever vector the mouse was dragged in. It then detects for a collision and it changes the velocity based on its current velocity and the normal of the terrain. However, when it does that, it flies in the opposite direction and flings itself through the floor.
How can I fix my code so that the collision and bouncing works as expected? Additionally it will be nice if the velocity eventually became 0 after bouncing a while.
Oh and usually the y position goes up the further you go down but I changed it so that the y position goes down the further you go down. SO at the very bottom, the y position is 0 instead of the height of the canvas.
Regarding your current problem, I actually have 2 different suggestions I decided to offer only the first one after consideration, as the second one can throw you into recursive madness, but I'm willing to expand on it if needed. I cannot really test them or offer you real code without a working example, though, so I'll stay on pseudocode level.
Solution 1: easy and robust
This one is pretty straightforward: save the pre-bounce vector. When the ball actually bounce, if the rebound has clipping, forget about it and inverse the initial vector. Since the ball came from this exact direction, you can be pretty damn sure that it can go back that way.
The algo would go like this:
if ValidateMove(CurrentVector):
Move(CurrentVector)
else:
NewVector = CalculateNewVector
if ValidateMove(NewVector):
Move(NewVector)
else:
Move(CurrentVector.Invert)
There's a possible problematic edge case, though. If another object is in movement, it may block the ball's inverted path. Honestly, I wouldn't care about the chance that this happens unless your game is specifically prone to this.
Hope this'll help!

Unity3D - How to control Light.Intensity without animation

I have the following animation:
See on video: Animation Light
Note: Below image of animation control
How can I get the same result using a script? My intention is that the script has no "if".
Don't like this:
public float minIntensity;
public float maxIntensity;
public float intensityAmount;
private Light light;
void Awake(){
light = GetComponent<Light>();
}
void Update(){
if (light.intensity > maxIntensity) {
light.intensity -= intensityAmount * Time.deltaTime;
}
else if (light.intensity < minIntensity) {
light.intensity += intensityAmount * Time.deltaTime;
}
}
I wonder if there is any possibility to do this using some native function ... like: (Math.Clamp, Math.Lerp, Quaternion.Slerp) without any condition as "if" in the code.
Thank you in advance.
Well like you mentioned, you can just use a clamp:
light.intensity = Mathf.Clamp(value, minIntensity, maxIntensity)
However, despite the lack of detail on what type of animation you want, I am assuming you want to "ping pong" between the min and the max. If that is the case, we can use our friendly neighborhood sine wave for that.
public float Frequency = 10f;
public float Magnitude = 2f;
void Update() {
light.Intensity = Magnitude + Mathf.Sin(Time.timeSinceLevelLoad * Frequency) * Magnitude;
}
The sine wave will go from -1 to 1, the magnitude value will make it go from (-magnitude to +magnitude) Since we don't want a negative light intensity, we add magnitude to the start, so the end result is (0 to 2 * magnitude) You can change this to work however you desire, but the point should be clear.
The Frequency variable will change how fast we animate back and forth.

Adding physics to a obj in processing

So I’m trying to give an obj some physics, and have work with the example of the bouncing ball that processing comes with, and I have make it instead of a ball to be a cylinder, but I can’t find the way to make the obj to have the physics.
I’m importing the obj with the library saito.objloader. I have this code, which is the invocation of the obj
import processing.opengl.*;
import saito.objloader.*;
OBJModel modelCan;
PVector location; // Location of shape
PVector velocity; // Velocity of shape
PVector gravity; // Gravity acts at the shape's acceleration
float rotX, rotY;
void setup() {
size(1028, 768, OPENGL);
frameRate(30);
modelCan = new OBJModel(this, "can.obj", "absolute", TRIANGLES);
modelCan.scale(50);
modelCan.translateToCenter();
modelCan.enableTexture();
location = new PVector(100,100,100);
velocity = new PVector(1.5,2.1,3);
gravity = new PVector(0,0.2,0);
}
void draw() {
background(129);
lights();
// Add velocity to the location.
location.add(velocity);
// Add gravity to velocity
velocity.add(gravity);
// Bounce off edges
if ((location.x > width) || (location.x < 0)) {
velocity.x = velocity.x * -1;
}
if (location.y > height) {
// We're reducing velocity ever so slightly
// when it hits the bottom of the window
velocity.y = velocity.y * -0.95;
location.y = height;
}
if (location.z < -height || location.z > 0) { //note that Zaxis goes 'into' the screen
velocity.z = velocity.z * -0.95;
//location.z = height;
}
println("location x: "+location.x);
println("location y: "+location.y);
println("location z: "+location.z);
println("velocidad x: "+velocity.x);
println("velocidad y: "+velocity.y);
println("velocidad z: "+velocity.z);
pushMatrix();
translate(width/2, height/2, 0);
modelCan.draw();
popMatrix();
}
I hope you guys can help me! Thanks!
The bouncing ball example is a very simple example, and I guess you can't apply it to a cylinder in an easy way.
You might want to have a look at these sites :
http://www.wildbunny.co.uk/blog/2011/04/20/collision-detection-for-dummies/
http://shiffman.net/ (his book is essential )
https://github.com/shiffman/Box2D-for-Processing (again : Daniel Shiffman)
Collision detection is made in computer time : it means that it's correlated with the frequency of your program execution. For instance, the velocity of an object can be so big that between two render cycles your first object is passing through your second object: collision detection won't happen. They didn't collide during the previous cycle, and don't collide during the current cycle.
That's why some physics "engine" like Box2D try to anticipate the collision, computing the bounding box of each object.
If you want to make accurate collision detection in 3D, you can look at the Bullet library, used in many games and movies : http://bulletphysics.org/wordpress/ but the learning curve might be large.
In your program, you can try to give a threshold to your collision detection (and add the dimensions of your object to its position, if you want the whole body to collide, not only its center !): if your object's position is between your limit and (your limit +/- a threshold) consider it's colliding, but you won't totally avoid tunnelling.

How to translate camera after rotating the scene?

I'm currently trying to visualize a building in OpenGL ES 2 in Android.
When I swipe to the left, the whole scene should move to the left (same with right, bottom and top). Now when I rotate the model by 90° the axis are swapped. When I swipe to the right, the model is moved to the top instead of the right (same with the other axis).
How can I translate the camera without having the problem with the current rotation of the scene? As I'm working with a phone, I always want to move the object to the direction I'm swiping to. When I call translate first, the translation works fine, but the rotation has some errors.
Update: I created most of the camera-class (suggested in answers):
private Vector3D c = new Vector3D(eye); // copy of eye
private Vector3D l = Vector3D.getNormalized
(Vector3D.subtract(center, eye)); // normalized center-eye
private Vector3D u = new Vector3D(up); // up (normalized)
public void rotateX(float angle) {
// Now you can rotate L vector left and
// right by rotating it around U by any angle. ??
}
public void rotateY(float angle) {
// If you also need to look up and down
// (rotate) you need to rotate both L and U around S ??
}
public void rotateZ(float angle) {
// to tilt left and right you need to rotate U around L. ??
}
// move left and right
// C = C+S * inputfactor
public void translateX(float inputFactor) {
Vector3D s = Vector3D.cross(l, u);
Vector3D sTimesInput = mul(s, inputFactor);
c = new Vector3D(c).add(sTimesInput);
}
// move forward and backward
// C = C+L*inputfactor
public void translateY(float inputFactor) {
Vector3D lTimesInput = mul(l, inputFactor);
c = new Vector3D(c).add(lTimesInput);
}
// move up and down
// C = C+U * inputfactor
public void translateZ(float inputFactor) {
Vector3D uTimesInput = mul(u, inputFactor);
c = new Vector3D(c).add(uTimesInput);
}
// reconstruct lookAt - eye,center,up
public Vector3D getEye() {
return new Vector3D(c);
}
public Vector3D getCenter() {
return new Vector3D(c).add(new Vector3D(l));
}
public Vector3D getUp() {
return new Vector3D(u);
}
public float[] createLookAt() {
Vector3D eye = getEye();
Vector3D center = getCenter();
Vector3D up = getUp();
return new float[] { eye.getX(), eye.getY(), eye.getZ(), center.getX(),
center.getY(), center.getZ(), up.getX(), up.getY(), up.getZ() };
}
// helper-function
public Vector3D mul(Vector3D vec, float factor) {
return new Vector3D(vec.getX() * factor, vec.getY() * factor,
vec.getZ() * factor);
}
How can I rotate a vector around another vector by an angle?
If you define camera base vectors C-center, L-look at, U-up such that C is the camera center, L is the normalized vector from center towards the point you are looking at and U is up normalized. Now you can rotate the L vector left and right by rotating it around U by any angle.
As for moving left and right you will have to reconstruct side vector S: a cross product between L and U (S=LxU). Now all you have to do is move the center: C = C+S*inputFactor.
To reconstruct the lookAt vectors:
eye = C
center = C+L
up = U
Just as a note: If you also need to look up and down (rotate) you need to rotate both L and U around S, to tilt left and right you need to rotate U around L. To move forward or backwards C = C+L*inputFactor and to move up and down C = C+U*inputFactor.
This should solve all logical camera movement and rotations.
I think you mix the concept of moving the camera and rotating your object. First you need to separate the action of rotating your object from any interaction with the camera. You should be able to do anything with the object without changing the camera at all. To do this just use the model matrix to describe the translation/rotation/scaling of your object. Then you handle your camera separatly by using Matrix.setLookAtM with an eyeX, eyeY, eyeZ that is independent of your model position. And a point to look at that in your case is dependent of the eyeX, eyeY, eyeZ, as you want to be looking at the same direction all the time.
For example, if you want to rotate your object 90 degrees around the Y axis, use:
Matrix.rotateM(mModelMatrix, 0, 90, 0.0f, 1.0f, 0.0f);
Also remember that all matrix operations after Matrix.setIdentityM will be done in a backwards order, ie if you rotate first and then translate as you do the first thing in your code, you will first translate your object away from its origin position, then rotate around the origin, making your object do a kind of rotation around of the first origin, instead of the center of your object.
Then lets say you have translated your camera 100 units to the left and looking at -Z direction, use:
Matrix.setLookAtM(mViewMatrix, offset, -100.0f+camX, camY, camZ, -100.0f+camX, camY, -1.0f+camZ, upX, upY, upZ);

Resources