- Partly implemented client side prediction (still unstable).
[peakengine.git] / engine / include / physics / RayCallback.h
blob7ab9f2a8e6605d87a2c95060934ba8380a86018d
1 /*
2 Copyright (C) 2008 Christian Reiser
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #ifndef _RAY_CALLBACK_H_
23 #define _RAY_CALLBACK_H_
25 #include "physics/Callback.h"
26 #include "core/Vector3D.h"
28 #include <string>
30 //tolua_begin
31 namespace peak
33 class Body;
34 class Script;
36 /**
37 * \brief Structure which contains infos about collisions
39 * A callback gets this structure as parameter, so the collision
40 * info is avaible in the script, too.
43 struct CollisionInfo
45 CollisionInfo()
47 lambda = 0;
48 object = 0;
50 /**
51 * \brief (Only for rays) that's the way from the ray origin to the collision point.
54 float lambda;
55 /**
56 * \brief The position where the collision has occured.
59 Vector3D point;
60 /**
61 * \brief The object with which the ray/convex intersects.
63 * That's very useful to findout with which entity the ray/convex has intersect.
64 * Use Body::getEntity to finally find out.
67 Body *object;
70 /**
71 * \brief Class for custom ray callbacks.
73 * Example:
74 * \code
75 * (lua_start)
76 * -- We need a a script, in which the callback will be called.
77 * samplescript = entity:getScript();
79 * raycallback = peak.RayCallback:new();
80 * raycallback:create(peak.Vector3D(0, 1, 0), peak.Vector3D(0, -1, 0), samplescript, MyRayCallback);
82 * -- This is our callback function, it ll be called when the ray had intersected.
83 * function MyRayCallback()
84 * print("The Ray had intersected!!");
85 * -- That'll print "The Ray had intersected!!" every time in the console when the ray had intersected(logical!).
86 * (lua_end)
87 * \endcode
89 * You see: It's very easy to create a custom ray callback and you need that for very many things.
92 class RayCallback : public Callback
94 public:
95 RayCallback();
96 virtual ~RayCallback();
98 /**
99 * \brief Creates a custom ray callback and defines it.
101 * \param rayto The Point where the ray ends.
102 * \param rayfrom The Point where the ray starts.
103 * \param script The Script where the callback function will be called.
104 * \param callbackname The name of the callback in the script. This function will be called when the ray had intersected.
107 bool create(Vector3D rayto, Vector3D rayfrom, Script *script, std::string callbackname);
109 * \brief Creates a custom ray callback and defines it.
111 * \param script The Script where the callback function will be called.
112 * \param callbackname The name of the callback in the script. This function will be called when the ray had intersected.
115 bool create(Script *script, std::string callbackname);
117 * \brief Destroys the callback.
119 * When this function is called the callback won't be called again.
121 bool destroy();
124 * \brief Updates the callback, for internal use only.
126 void update();
129 * \brief Returns true when the ray had intersected and false if not.
131 bool isCollision();
134 * \brief Sets the ray start position AND end position new.
136 * \param rayto The point where the ray ends.
137 * \param rayfrom The point where the ray starts.
139 * If you only want to change one of the both components you can do something like that:
140 * \code
141 * (lua_start)
142 * raycallback:setRay(peak.Vector3D(1, 0, 0), raycallback:getRay(1));
143 * (lua_end)
144 * \endcode
146 void setRay(Vector3D rayto, Vector3D rayfrom);
148 * \brief Gets the start position OR the end position of the ray.
150 * \param index If index is true the end position is returned and if index is false the start positon is returned.
152 * \return The return value depends on the "index" parameter. For more info see parameter documentation.
154 Vector3D getRay(bool index);
157 * \brief Sets the collision info.
159 void setInfo(CollisionInfo info);
161 * \brief Gets the collision info.
163 CollisionInfo getInfo(void);
164 private:
165 //tolua_end
166 std::string callbackname;
167 Script *script;
169 Vector3D rayto;
170 Vector3D rayfrom;
172 CollisionInfo info;
173 bool collision;
174 //tolua_begin
177 //tolua_end
179 #endif