2 (c) 2010 Perttu Ahola <celeron55@gmail.com>
7 #include "connection.h"
11 touching_ground(false),
12 inventory(PLAYER_INVENTORY_SIZE),
17 updateName("<not set>");
24 void Player::move(f32 dtime, Map &map)
26 v3f position = getPosition();
27 v3f oldpos = position;
28 v3s16 oldpos_i = floatToInt(oldpos);
30 /*std::cout<<"oldpos_i=("<<oldpos_i.X<<","<<oldpos_i.Y<<","
31 <<oldpos_i.Z<<")"<<std::endl;*/
33 position += m_speed * dtime;
35 // Skip collision detection if player is non-local
36 if(isLocal() == false)
38 setPosition(position);
46 v3s16 pos_i = floatToInt(position);
48 // The frame length is limited to the player going 0.1*BS per call
49 f32 d = (float)BS * 0.15;
51 #define PLAYER_RADIUS (BS*0.3)
52 #define PLAYER_HEIGHT (BS*1.7)
54 core::aabbox3d<f32> playerbox(
55 position.X - PLAYER_RADIUS,
57 position.Z - PLAYER_RADIUS,
58 position.X + PLAYER_RADIUS,
59 position.Y + PLAYER_HEIGHT,
60 position.Z + PLAYER_RADIUS
62 core::aabbox3d<f32> playerbox_old(
63 oldpos.X - PLAYER_RADIUS,
65 oldpos.Z - PLAYER_RADIUS,
66 oldpos.X + PLAYER_RADIUS,
67 oldpos.Y + PLAYER_HEIGHT,
68 oldpos.Z + PLAYER_RADIUS
71 //hilightboxes.push_back(playerbox);
73 touching_ground = false;
75 /*std::cout<<"Checking collisions for ("
76 <<oldpos_i.X<<","<<oldpos_i.Y<<","<<oldpos_i.Z
78 <<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z
81 for(s16 y = oldpos_i.Y - 1; y <= oldpos_i.Y + 2; y++){
82 for(s16 z = oldpos_i.Z - 1; z <= oldpos_i.Z + 1; z++){
83 for(s16 x = oldpos_i.X - 1; x <= oldpos_i.X + 1; x++){
84 //std::cout<<"with ("<<x<<","<<y<<","<<z<<"): ";
86 if(map.getNode(v3s16(x,y,z)).d == MATERIAL_AIR){
87 //std::cout<<"air."<<std::endl;
91 catch(InvalidPositionException &e)
93 // Doing nothing here will block the player from
94 // walking over map borders
97 core::aabbox3d<f32> nodebox = Map::getNodeBox(
100 // See if the player is touching ground
102 fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < d
103 && nodebox.MaxEdge.X-d > playerbox.MinEdge.X
104 && nodebox.MinEdge.X+d < playerbox.MaxEdge.X
105 && nodebox.MaxEdge.Z-d > playerbox.MinEdge.Z
106 && nodebox.MinEdge.Z+d < playerbox.MaxEdge.Z
108 touching_ground = true;
111 if(playerbox.intersectsWithBox(nodebox))
119 for(u16 i=0; i<3; i++)
121 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[i]);
122 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[i]);
123 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[i]);
124 f32 playermin = playerbox.MinEdge.dotProduct(dirs[i]);
125 f32 playermax_old = playerbox_old.MaxEdge.dotProduct(dirs[i]);
126 f32 playermin_old = playerbox_old.MinEdge.dotProduct(dirs[i]);
128 bool main_edge_collides =
129 ((nodemax > playermin && nodemax <= playermin_old + d
130 && m_speed.dotProduct(dirs[i]) < 0)
132 (nodemin < playermax && nodemin >= playermax_old - d
133 && m_speed.dotProduct(dirs[i]) > 0));
135 bool other_edges_collide = true;
136 for(u16 j=0; j<3; j++)
140 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[j]);
141 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[j]);
142 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[j]);
143 f32 playermin = playerbox.MinEdge.dotProduct(dirs[j]);
144 if(!(nodemax - d > playermin && nodemin + d < playermax))
146 other_edges_collide = false;
151 if(main_edge_collides && other_edges_collide)
153 m_speed -= m_speed.dotProduct(dirs[i]) * dirs[i];
154 position -= position.dotProduct(dirs[i]) * dirs[i];
155 position += oldpos.dotProduct(dirs[i]) * dirs[i];
159 } // if(playerbox.intersectsWithBox(nodebox))
164 setPosition(position);
167 // Y direction is ignored
168 void Player::accelerate(v3f target_speed, f32 max_increase)
170 if(m_speed.X < target_speed.X - max_increase)
171 m_speed.X += max_increase;
172 else if(m_speed.X > target_speed.X + max_increase)
173 m_speed.X -= max_increase;
174 else if(m_speed.X < target_speed.X)
175 m_speed.X = target_speed.X;
176 else if(m_speed.X > target_speed.X)
177 m_speed.X = target_speed.X;
179 if(m_speed.Z < target_speed.Z - max_increase)
180 m_speed.Z += max_increase;
181 else if(m_speed.Z > target_speed.Z + max_increase)
182 m_speed.Z -= max_increase;
183 else if(m_speed.Z < target_speed.Z)
184 m_speed.Z = target_speed.Z;
185 else if(m_speed.Z > target_speed.Z)
186 m_speed.Z = target_speed.Z;
193 RemotePlayer::RemotePlayer(
194 scene::ISceneNode* parent,
195 IrrlichtDevice *device,
197 scene::ISceneNode(parent, (device==NULL)?NULL:device->getSceneManager(), id),
200 m_box = core::aabbox3d<f32>(-BS/2,0,-BS/2,BS/2,BS*2,BS/2);
202 if(parent != NULL && device != NULL)
204 // ISceneNode stores a member called SceneManager
205 scene::ISceneManager* mgr = SceneManager;
206 video::IVideoDriver* driver = mgr->getVideoDriver();
207 gui::IGUIEnvironment* gui = device->getGUIEnvironment();
209 // Add a text node for showing the name
210 wchar_t wname[1] = {0};
211 m_text = mgr->addTextSceneNode(gui->getBuiltInFont(),
212 wname, video::SColor(255,255,255,255), this);
213 m_text->setPosition(v3f(0, (f32)BS*2.1, 0));
215 // Attach a simple mesh to the player for showing an image
216 scene::SMesh *mesh = new scene::SMesh();
218 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
219 video::SColor c(255,255,255,255);
220 video::S3DVertex vertices[4] =
222 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
223 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
224 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
225 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
227 u16 indices[] = {0,1,2,2,3,0};
228 buf->append(vertices, 4, indices, 6);
230 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
231 //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
232 buf->getMaterial().setTexture(0, driver->getTexture("../data/player.png"));
233 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
234 //buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
235 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
237 mesh->addMeshBuffer(buf);
241 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
242 video::SColor c(255,255,255,255);
243 video::S3DVertex vertices[4] =
245 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
246 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
247 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
248 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
250 u16 indices[] = {0,1,2,2,3,0};
251 buf->append(vertices, 4, indices, 6);
253 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
254 //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
255 buf->getMaterial().setTexture(0, driver->getTexture("../data/player_back.png"));
256 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
257 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
259 mesh->addMeshBuffer(buf);
262 scene::IMeshSceneNode *node = mgr->addMeshSceneNode(mesh, this);
264 node->setPosition(v3f(0,0,0));
268 RemotePlayer::~RemotePlayer()
270 if(SceneManager != NULL)
271 ISceneNode::remove();
274 void RemotePlayer::updateName(const char *name)
276 Player::updateName(name);
279 wchar_t wname[PLAYERNAME_SIZE];
280 mbstowcs(wname, m_name, strlen(m_name)+1);
281 m_text->setText(wname);
289 LocalPlayer::LocalPlayer()
293 LocalPlayer::~LocalPlayer()
297 void LocalPlayer::applyControl(float dtime)
300 #define WALK_ACCELERATION (4.0 * BS)
301 #define WALKSPEED_MAX (4.0 * BS)
302 f32 walk_acceleration = WALK_ACCELERATION;
303 f32 walkspeed_max = WALKSPEED_MAX;
305 setPitch(control.pitch);
308 v3f move_direction = v3f(0,0,1);
309 move_direction.rotateXZBy(getYaw());
311 v3f speed = v3f(0,0,0);
314 bool superspeed = false;
315 if(control.superspeed)
317 speed += move_direction;
323 speed += move_direction;
327 speed -= move_direction;
331 speed += move_direction.crossProduct(v3f(0,1,0));
335 speed += move_direction.crossProduct(v3f(0,-1,0));
340 v3f speed = getSpeed();
346 // The speed of the player (Y is ignored)
348 speed = speed.normalize() * walkspeed_max * 5;
350 speed = speed.normalize() * walkspeed_max;
352 f32 inc = walk_acceleration * BS * dtime;
354 // Accelerate to target speed with maximum increment
355 accelerate(speed, inc);