主题:  各位,对碰撞检测有新的收获吗?

southwest

职务:普通成员
等级:1
金币:0.0
发贴:101
#12001/12/20 9:03:09
我的虚拟漫游房间已经做好了,现正在摸索java的碰撞检测。有谁知道一些相关的东东的。如java源程序、经验等等的。共同交流嘛!



平凡人

职务:普通成员
等级:4
金币:17.0
发贴:1442
#22001/12/20 12:09:26
源程序如下:
import com.cult3d.*;
import com.cult3d.world.*;
import com.cult3d.math.*;
import java.util.*;
import java.io.*;
import java.*;
import java.awt.*;


public class kollision implements Cult3DScript
{
public boolean objekt_loaded = false;
private Camera cam;
private String namn = new String();
//an array with the former camera-position
//and the new one..
private float cam_pos[][] = new float[2][3];
//array with all the wall-objects..in this
//case 264 ones, and 4 values a wall
//(minX,minY,maxX,maxY)
private float vaggar[][] = new float[264][4];
public CultObject box;// = new CultObject();
Vector3 Vcam_pos,ny_pos;
Vector3 box_max,box_min,box_pos;

public kollision()
{
cam = new Camera("Camera01");
ny_pos = new Vector3();
//loops through all the objects in the scene
//and puts the value in the array.
//the object have to be named from "0" up
//to the current amount of walls...
for(int antal = 0; antal <= 264; antal++)
{
//making the integer a string..
namn = namn.valueOf(antal);
//loading the objekt
box = new CultObject(namn);
box_pos = box.getPosition(CultObject.WORLD);
box_max = box.getMaxVertexPosition();
box_max.add(box_pos);
box_min = box.getMinVertexPosition();
box_min.add(box_pos);
//the 10 value is the closest you are
//supposed to get to the wall.
vaggar[antal][0] = box_min.getX() - 10;
vaggar[antal][1] = box_min.getY()-10 ;
vaggar[antal][2] = box_max.getX() + 10;
vaggar[antal][3] = box_max.getY() + 10;
box = null;
}
//indicating all objects are loaded..
objekt_loaded = true;

}
//this funktion keeps track of the camera
//placement..
public void kamera_plac_reg()
{
//if it´s the first movement of the camera,
//it loads the value in the first position..
Vcam_pos = cam.getPosition(Camera.WORLD);
if (cam_pos[0][1] == 0 && cam_pos[0][2] == 0)
{
cam_pos[0][0] = Vcam_pos.getX();
cam_pos[0][1] = Vcam_pos.getY();
cam_pos[0][2] = Vcam_pos.getZ();
}
//if it has a value in the firs position,
//load in the second one.
else if (cam_pos[1][1] == 0 && cam_pos[1][2] == 0 && cam_pos[0][1] != 0 && cam_pos[0][2] != 0)
{
cam_pos[1][0] = Vcam_pos.getX();
cam_pos[1][1] = Vcam_pos.getY();
cam_pos[1][2] = Vcam_pos.getZ();
}

//if both post are writen into, load the
//former actual position to the former-
//position..and write the new position to
//the actual...get it..??
//
else if (cam_pos[0][1] != 0 && cam_pos[1][1] != 0 )
{
cam_pos[0][0] = cam_pos[1][0];
cam_pos[0][1] = cam_pos[1][1];
cam_pos[0][2] = cam_pos[1][2];
cam_pos[1][0] = Vcam_pos.getX();
cam_pos[1][1] = Vcam_pos.getY();
cam_pos[1][2] = Vcam_pos.getZ();
}


}
//This function is placed in the cult3d
//designer and called when the camera is
//sent forward..
public void framat(String s)
{
kamera_plac_reg();
//Här kommer grid-testet sen som spänner över hela spelet
//if(1 == 1)
//{
if (objekt_loaded == true)
{
//calling the function that handels the
//collision detection..
kamera_kollisions_koll(Vcam_pos.getX(), Vcam_pos.getY());

}
else
{
new CultEvent("masterReset").trigger();
}
//}
}
//This function is placed in the cult3d
//designer and called when the camera is
//sent backward, the same thing as the
//forward function but I couldn´t connect the
//same function to two events in the designer
//so I made one for each of em...
public void bakat(String s)
{
//getting the new camera position
kamera_plac_reg();
//Här kommer grid-testet sen som spänner över hela spelet
//if(1 != 1)
//{
if (objekt_loaded == true)
{
//calling the collision detection with the
//cameras X,Y position as arguments..
kamera_kollisions_koll(Vcam_pos.getX(), Vcam_pos.getY());
}
else
{
new CultEvent("masterReset").trigger();
}
//}

}

public void kamera_kollisions_koll(float X, float Y)
{
//looping through all the walls..
for(int i = 0 ; i <= 264;i++ )
/*if camera_x >= wall_minX and camera_x <=
wall_maxX and camera_y >= wall_minY and camara_y <= wall_maxY = the camera is inside the object...
*/
{
if(X >= vaggar[i][0] && X <= vaggar[i][2] && Y >= vaggar[i][1] && Y <= vaggar[i][3])
{
//call to the function that moves the camera
//back to the position that it was, before
//it intersected the wall..
flytta();
}

}
}

public void cult3dDestroy()
{
}

public void flytta()
{
ny_pos.setY(cam_pos[0][1]); //cam_pos[0][0]
ny_pos.setX(cam_pos[0][0]);
ny_pos.setZ(cam_pos[0][2]);
cam.setPosition(Camera.WORLD,ny_pos);
kamera_plac_reg();
}
}(转3D多媒体)
你慢慢研究吧!希望你早日成功。



southwest

职务:普通成员
等级:1
金币:0.0
发贴:101
#32001/12/20 20:29:30
This is "Kinda working, Collision detection" Java code on "cult3d.com".I have some questions:
1. How to define the position and name of object in code?
for example: "box=new CultObject(namn);" ----> "box=new CultObject("wall01")" isn't?
2. I don't know if it need to define each object in code?
Thanks



balabalala

职务:普通成员
等级:1
金币:0.0
发贴:12
#42001/12/23 17:01:04
我想问问在cult3d里用的java哪种形式,我用java builder编写可以吗?怎样加?直接在菜单里把下面的程序加入就可以吗?还需不需要编辑她了?
谢谢!



平凡人

职务:普通成员
等级:4
金币:17.0
发贴:1442
#52001/12/23 17:23:47
这个程序应该还有点问题,我测试过不通过。你的那个软件可以的,不过你要把它转为class文件然后才能在cult3d中导入。



balabalala

职务:普通成员
等级:1
金币:0.0
发贴:12
#62001/12/29 13:57:37
谢谢!你的测试通过了吗?通过的话告诉我一声!