3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 (c) 2010 Perttu Ahola <celeron55@gmail.com>
26 #include "connection.h"
27 #include "constants.h"
30 touching_ground(false),
31 inventory(PLAYER_INVENTORY_SIZE),
36 updateName("<not set>");
43 void Player::move(f32 dtime, Map &map)
45 v3f position = getPosition();
46 v3f oldpos = position;
47 v3s16 oldpos_i = floatToInt(oldpos);
49 /*std::cout<<"oldpos_i=("<<oldpos_i.X<<","<<oldpos_i.Y<<","
50 <<oldpos_i.Z<<")"<<std::endl;*/
52 position += m_speed * dtime;
54 // Skip collision detection if player is non-local
55 if(isLocal() == false)
57 setPosition(position);
65 v3s16 pos_i = floatToInt(position);
67 // The frame length is limited to the player going 0.1*BS per call
68 f32 d = (float)BS * 0.15;
70 #define PLAYER_RADIUS (BS*0.3)
71 #define PLAYER_HEIGHT (BS*1.7)
73 core::aabbox3d<f32> playerbox(
74 position.X - PLAYER_RADIUS,
76 position.Z - PLAYER_RADIUS,
77 position.X + PLAYER_RADIUS,
78 position.Y + PLAYER_HEIGHT,
79 position.Z + PLAYER_RADIUS
81 core::aabbox3d<f32> playerbox_old(
82 oldpos.X - PLAYER_RADIUS,
84 oldpos.Z - PLAYER_RADIUS,
85 oldpos.X + PLAYER_RADIUS,
86 oldpos.Y + PLAYER_HEIGHT,
87 oldpos.Z + PLAYER_RADIUS
90 //hilightboxes.push_back(playerbox);
92 touching_ground = false;
94 /*std::cout<<"Checking collisions for ("
95 <<oldpos_i.X<<","<<oldpos_i.Y<<","<<oldpos_i.Z
97 <<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z
100 for(s16 y = oldpos_i.Y - 1; y <= oldpos_i.Y + 2; y++){
101 for(s16 z = oldpos_i.Z - 1; z <= oldpos_i.Z + 1; z++){
102 for(s16 x = oldpos_i.X - 1; x <= oldpos_i.X + 1; x++){
103 //std::cout<<"with ("<<x<<","<<y<<","<<z<<"): ";
105 if(map.getNode(v3s16(x,y,z)).d == MATERIAL_AIR){
106 //std::cout<<"air."<<std::endl;
110 catch(InvalidPositionException &e)
112 // Doing nothing here will block the player from
113 // walking over map borders
116 core::aabbox3d<f32> nodebox = Map::getNodeBox(
119 // See if the player is touching ground
121 fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < d
122 && nodebox.MaxEdge.X-d > playerbox.MinEdge.X
123 && nodebox.MinEdge.X+d < playerbox.MaxEdge.X
124 && nodebox.MaxEdge.Z-d > playerbox.MinEdge.Z
125 && nodebox.MinEdge.Z+d < playerbox.MaxEdge.Z
127 touching_ground = true;
130 if(playerbox.intersectsWithBox(nodebox))
138 for(u16 i=0; i<3; i++)
140 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[i]);
141 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[i]);
142 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[i]);
143 f32 playermin = playerbox.MinEdge.dotProduct(dirs[i]);
144 f32 playermax_old = playerbox_old.MaxEdge.dotProduct(dirs[i]);
145 f32 playermin_old = playerbox_old.MinEdge.dotProduct(dirs[i]);
147 bool main_edge_collides =
148 ((nodemax > playermin && nodemax <= playermin_old + d
149 && m_speed.dotProduct(dirs[i]) < 0)
151 (nodemin < playermax && nodemin >= playermax_old - d
152 && m_speed.dotProduct(dirs[i]) > 0));
154 bool other_edges_collide = true;
155 for(u16 j=0; j<3; j++)
159 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[j]);
160 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[j]);
161 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[j]);
162 f32 playermin = playerbox.MinEdge.dotProduct(dirs[j]);
163 if(!(nodemax - d > playermin && nodemin + d < playermax))
165 other_edges_collide = false;
170 if(main_edge_collides && other_edges_collide)
172 m_speed -= m_speed.dotProduct(dirs[i]) * dirs[i];
173 position -= position.dotProduct(dirs[i]) * dirs[i];
174 position += oldpos.dotProduct(dirs[i]) * dirs[i];
178 } // if(playerbox.intersectsWithBox(nodebox))
183 setPosition(position);
186 // Y direction is ignored
187 void Player::accelerate(v3f target_speed, f32 max_increase)
189 if(m_speed.X < target_speed.X - max_increase)
190 m_speed.X += max_increase;
191 else if(m_speed.X > target_speed.X + max_increase)
192 m_speed.X -= max_increase;
193 else if(m_speed.X < target_speed.X)
194 m_speed.X = target_speed.X;
195 else if(m_speed.X > target_speed.X)
196 m_speed.X = target_speed.X;
198 if(m_speed.Z < target_speed.Z - max_increase)
199 m_speed.Z += max_increase;
200 else if(m_speed.Z > target_speed.Z + max_increase)
201 m_speed.Z -= max_increase;
202 else if(m_speed.Z < target_speed.Z)
203 m_speed.Z = target_speed.Z;
204 else if(m_speed.Z > target_speed.Z)
205 m_speed.Z = target_speed.Z;
212 RemotePlayer::RemotePlayer(
213 scene::ISceneNode* parent,
214 IrrlichtDevice *device,
216 scene::ISceneNode(parent, (device==NULL)?NULL:device->getSceneManager(), id),
219 m_box = core::aabbox3d<f32>(-BS/2,0,-BS/2,BS/2,BS*2,BS/2);
221 if(parent != NULL && device != NULL)
223 // ISceneNode stores a member called SceneManager
224 scene::ISceneManager* mgr = SceneManager;
225 video::IVideoDriver* driver = mgr->getVideoDriver();
226 gui::IGUIEnvironment* gui = device->getGUIEnvironment();
228 // Add a text node for showing the name
229 wchar_t wname[1] = {0};
230 m_text = mgr->addTextSceneNode(gui->getBuiltInFont(),
231 wname, video::SColor(255,255,255,255), this);
232 m_text->setPosition(v3f(0, (f32)BS*2.1, 0));
234 // Attach a simple mesh to the player for showing an image
235 scene::SMesh *mesh = new scene::SMesh();
237 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
238 video::SColor c(255,255,255,255);
239 video::S3DVertex vertices[4] =
241 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
242 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
243 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
244 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
246 u16 indices[] = {0,1,2,2,3,0};
247 buf->append(vertices, 4, indices, 6);
249 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
250 //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
251 buf->getMaterial().setTexture(0, driver->getTexture("../data/player.png"));
252 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
253 //buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
254 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
256 mesh->addMeshBuffer(buf);
260 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
261 video::SColor c(255,255,255,255);
262 video::S3DVertex vertices[4] =
264 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
265 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
266 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
267 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
269 u16 indices[] = {0,1,2,2,3,0};
270 buf->append(vertices, 4, indices, 6);
272 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
273 //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
274 buf->getMaterial().setTexture(0, driver->getTexture("../data/player_back.png"));
275 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
276 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
278 mesh->addMeshBuffer(buf);
281 scene::IMeshSceneNode *node = mgr->addMeshSceneNode(mesh, this);
283 node->setPosition(v3f(0,0,0));
287 RemotePlayer::~RemotePlayer()
289 if(SceneManager != NULL)
290 ISceneNode::remove();
293 void RemotePlayer::updateName(const char *name)
295 Player::updateName(name);
298 wchar_t wname[PLAYERNAME_SIZE];
299 mbstowcs(wname, m_name, strlen(m_name)+1);
300 m_text->setText(wname);
308 LocalPlayer::LocalPlayer()
312 LocalPlayer::~LocalPlayer()
316 void LocalPlayer::applyControl(float dtime)
319 #define WALK_ACCELERATION (4.0 * BS)
320 #define WALKSPEED_MAX (4.0 * BS)
321 f32 walk_acceleration = WALK_ACCELERATION;
322 f32 walkspeed_max = WALKSPEED_MAX;
324 setPitch(control.pitch);
327 v3f move_direction = v3f(0,0,1);
328 move_direction.rotateXZBy(getYaw());
330 v3f speed = v3f(0,0,0);
333 bool superspeed = false;
334 if(control.superspeed)
336 speed += move_direction;
342 speed += move_direction;
346 speed -= move_direction;
350 speed += move_direction.crossProduct(v3f(0,1,0));
354 speed += move_direction.crossProduct(v3f(0,-1,0));
359 v3f speed = getSpeed();
365 // The speed of the player (Y is ignored)
367 speed = speed.normalize() * walkspeed_max * 5;
369 speed = speed.normalize() * walkspeed_max;
371 f32 inc = walk_acceleration * BS * dtime;
373 // Accelerate to target speed with maximum increment
374 accelerate(speed, inc);