daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Jan 29th, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.dkstrong.transport.g3d.shape;
  2.  
  3. import com.badlogic.gdx.math.Intersector;
  4. import com.badlogic.gdx.math.MathUtils;
  5. import com.badlogic.gdx.math.Matrix4;
  6. import com.badlogic.gdx.math.Quaternion;
  7. import com.badlogic.gdx.math.Vector3;
  8. import com.badlogic.gdx.math.collision.BoundingBox;
  9. import com.badlogic.gdx.math.collision.Ray;
  10.  
  11. public class BoxTest {
  12.  
  13.     public static void main(String[] args) {
  14.         // transform for when the box isnt rotated
  15.         Matrix4 identitydTransform = new Matrix4(
  16.             new Vector3(),
  17.             new Quaternion(),
  18.             new Vector3(1,1,1)
  19.         );
  20.  
  21.         // tranform for when box is rotated 90 degress (this is the problem!)
  22.         Matrix4 rotatedTransform = new Matrix4(
  23.             new Vector3(),
  24.             new Quaternion().setFromAxisRad(new Vector3(0,1,0), MathUtils.PI/2f),
  25.             new Vector3(1,1,1)
  26.         );
  27.  
  28.         Matrix4 identityTransformInverse = identitydTransform.cpy().inv();
  29.         Matrix4 rotatedTransformInverse = rotatedTransform.cpy().inv();
  30.  
  31.         // bounding box is a rectangle shape, so rotating creates problems
  32.         BoundingBox bb = new BoundingBox(
  33.             new Vector3(-5,-10,-10),
  34.             new Vector3(5, 10, 10)
  35.         );
  36.  
  37.         // ray goes from top of world and goes down
  38.         Ray ray = new Ray(
  39.             new Vector3(0,20,9), // the ray position means its going to hit the long end of the rectangle
  40.             new Vector3(0,-1,0));
  41.  
  42.         Ray ray2 = new Ray(
  43.             new Vector3(9,20,0), // since box is rotated the person would click somewhere else..
  44.             new Vector3(0,-1,0));
  45.  
  46.         Ray rayForIdentiy = ray.cpy().mul(identityTransformInverse);
  47.         Ray rayForRotated = ray2.cpy().mul(rotatedTransformInverse);
  48.  
  49.         Vector3 bbCenter = bb.getCenter(new Vector3());
  50.         Vector3 bbDimensions = bb.getDimensions(new Vector3());
  51.         boolean isHit;
  52.         isHit = Intersector.intersectRayBoundsFast(rayForIdentiy, bbCenter, bbDimensions);
  53.         System.out.println("works if transform is identity: "+isHit); // true
  54.         isHit = Intersector.intersectRayBoundsFast(rayForRotated, bbCenter, bbDimensions);
  55.         System.out.println("works if transform is rotated: "+isHit); // true
  56.  
  57.     }
  58. }
RAW Paste Data
Pastebin PRO WINTER Special!
Get 40% OFF Pastebin PRO accounts!
Top