Skip to content

Commit

Permalink
Fixes for projectile physics, should also fix compiling on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
fador committed Jun 29, 2013
1 parent 2e63b77 commit 8a9c5d2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/packets.cpp
Expand Up @@ -273,7 +273,7 @@ int PacketHandler::creative_inventory(User *user)

user->buffer>>slot>>itemID;

if(itemID == 0xffff) return PACKET_OK;
if((uint16_t)itemID == 0xffff) return PACKET_OK;

if (!user->buffer.haveData(5))
return PACKET_NEED_MORE_DATA;
Expand Down
39 changes: 28 additions & 11 deletions src/physics.cpp
Expand Up @@ -25,6 +25,7 @@
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <math.h>

#include "physics.h"
#include "logger.h"
Expand Down Expand Up @@ -221,7 +222,7 @@ bool Physics::updateEntity()
{

uint32_t listSize = entitySimList.size();

std::vector<uint32_t> toRem;
for (int32_t simIt = listSize-1; simIt >= 0; simIt--)
{
simulationEntity& f = entitySimList[simIt];
Expand All @@ -231,11 +232,12 @@ bool Physics::updateEntity()
f.lastTime = microTime();
f.ticks++;

const double gravity = -9.81;
const double gravity = -13.5;//9.81;

//Calculate offset when x and z velocity stays the same and y velocity has gravity to deal with
double offset_x = f.startPos.vel_x*timeInSec;
double offset_z = f.startPos.vel_z*timeInSec;

double offset_y = ( ((gravity*timeInSec+gravity*lasttimeInSec)/2.0)*timeInSec+f.startPos.vel_y*timeInSec);
double offset_y = 0.5*gravity*timeInSec*timeInSec+f.startPos.vel_y*timeInSec;
entity_position newPos;
newPos.x = offset_x+f.startPos.x;
newPos.z = offset_z+f.startPos.z;
Expand All @@ -245,16 +247,20 @@ bool Physics::updateEntity()
diffPos.z = newPos.z-f.pos.z;
diffPos.y = newPos.y-f.pos.y;

//Loop all blocks between current point and last point. Just simple linear interpolation
for(double i = 0; i < 1.0; i+= 1.0/sqrt(diffPos.x*diffPos.x+diffPos.y*diffPos.y+diffPos.z*diffPos.z))
{
//
//Check for the block
uint8_t block,meta;
ServerInstance->map(map)->getBlock(f.pos.x+diffPos.x*i-0.5,f.pos.y+diffPos.y*i,f.pos.z+diffPos.z*i-0.5,&block, &meta);
ServerInstance->map(map)->getBlock((int)(f.pos.x+diffPos.x*i-0.5),(int)(f.pos.y+diffPos.y*i),(int)(f.pos.z+diffPos.z*i-0.5),&block, &meta);

//When hitting _something_, turn it to glass and destroy the entity
if(block != BLOCK_AIR)
{
ServerInstance->map(map)->sendBlockChange(f.pos.x+diffPos.x*i-0.5, f.pos.y+diffPos.y*i, f.pos.z+diffPos.z*i-0.5, BLOCK_GLASS, 0);
entitySimList.erase(entitySimList.begin()+simIt);
return true;
ServerInstance->map(map)->sendBlockChange((int)(f.pos.x+diffPos.x*i-0.5), (int)(f.pos.y+diffPos.y*i), (int)(f.pos.z+diffPos.z*i-0.5), BLOCK_GLASS, 0);
//Add to "to-remove" list
toRem.push_back(f.EID);
break;
}
}

Expand All @@ -271,8 +277,19 @@ bool Physics::updateEntity()
User::sendAll(pkt);
*/


}
for(int32_t i = 0; i < toRem.size(); i++)
{
for (int32_t simIt = entitySimList.size()-1; simIt >= 0; simIt--)
{
if(entitySimList[simIt].EID == toRem[i])
{
Packet pkt = Protocol::destroyEntity(toRem[i]);
User::sendAll(pkt);
entitySimList.erase(entitySimList.begin()+simIt);
break;
}
}
}
return true;
}
Expand Down

0 comments on commit 8a9c5d2

Please sign in to comment.