OpenClonk
C4Movement.cpp File Reference
#include "C4Include.h"
#include "game/C4Physics.h"
#include "landscape/C4Landscape.h"
#include "landscape/C4SolidMask.h"
#include "object/C4Def.h"
#include "object/C4Object.h"
#include "script/C4Effect.h"
Include dependency graph for C4Movement.cpp:

Go to the source code of this file.

Functions

void RedirectForce (C4Real &from, C4Real &to, int32_t tdir)
 
void ApplyFriction (C4Real &tval, int32_t percent)
 
bool ContactVtxCNAT (C4Object *object, BYTE cnat_dir)
 
int32_t ContactVtxWeight (C4Object *object)
 
int32_t ContactVtxFriction (C4Object *object)
 
const char * CNATName (int32_t cnat)
 
bool SimFlight (C4Real &x, C4Real &y, C4Real &xdir, C4Real &ydir, int32_t min_density, int32_t max_density, int32_t &iterations)
 
bool SimFlightHitsLiquid (C4Real start_x, C4Real start_y, C4Real xdir, C4Real ydir)
 

Variables

const C4Real FRedirect = C4REAL100(50)
 
const C4Real FFriction = C4REAL100(30)
 
const C4Real FixFullCircle = itofix(360)
 
const C4Real FixHalfCircle = FixFullCircle / 2
 
const C4Real FloatFriction = C4REAL100(2)
 
const C4Real RotateAccel = C4REAL100(20)
 
const C4Real HitSpeed1 = C4REAL100(150)
 
const C4Real HitSpeed2 = itofix(2)
 
const C4Real HitSpeed3 = itofix(6)
 
const C4Real HitSpeed4 = itofix(8)
 
const C4Real DefaultGravAccel = C4REAL100(20)
 

Function Documentation

◆ ApplyFriction()

void ApplyFriction ( C4Real tval,
int32_t  percent 
)

Definition at line 52 of file C4Movement.cpp.

53 {
54  C4Real ffric = FFriction * percent / 100;
55  if (tval > +ffric)
56  {
57  tval -= ffric;
58  }
59  else if (tval < -ffric)
60  {
61  tval += ffric;
62  }
63  else
64  {
65  tval = 0;
66  }
67 }
const C4Real FFriction
Definition: C4Movement.cpp:32
Definition: C4Real.h:59

References FFriction.

Referenced by C4Object::DoMovement().

Here is the caller graph for this function:

◆ CNATName()

const char* CNATName ( int32_t  cnat)

Definition at line 121 of file C4Movement.cpp.

122 {
123  switch (cnat)
124  {
125  case CNAT_None: return "None";
126  case CNAT_Left: return "Left";
127  case CNAT_Right: return "Right";
128  case CNAT_Top: return "Top";
129  case CNAT_Bottom: return "Bottom";
130  case CNAT_Center: return "Center";
131  }
132  return "Undefined";
133 }
const BYTE CNAT_Bottom
Definition: C4Constants.h:112
const BYTE CNAT_Center
Definition: C4Constants.h:113
const BYTE CNAT_Right
Definition: C4Constants.h:110
const BYTE CNAT_Top
Definition: C4Constants.h:111
const BYTE CNAT_None
Definition: C4Constants.h:108
const BYTE CNAT_Left
Definition: C4Constants.h:109

References CNAT_Bottom, CNAT_Center, CNAT_Left, CNAT_None, CNAT_Right, and CNAT_Top.

Referenced by C4Object::Contact().

Here is the caller graph for this function:

◆ ContactVtxCNAT()

bool ContactVtxCNAT ( C4Object object,
BYTE  cnat_dir 
)

Definition at line 72 of file C4Movement.cpp.

73 {
74  for (int32_t index = 0; index < object->Shape.VtxNum; index++)
75  {
76  if (object->Shape.VtxContactCNAT[index] & cnat_dir)
77  {
78  return true;
79  }
80  }
81  return false;
82 }
C4Shape Shape
Definition: C4Object.h:146
int32_t VtxContactCNAT[C4D_MaxVertex]
Definition: C4Shape.h:51

References C4Object::Shape, and C4Shape::VtxContactCNAT.

Referenced by C4Object::DoMovement().

Here is the caller graph for this function:

◆ ContactVtxFriction()

int32_t ContactVtxFriction ( C4Object object)

Definition at line 109 of file C4Movement.cpp.

110 {
111  for (int32_t index = 0; index < object->Shape.VtxNum; index++)
112  {
113  if (object->Shape.VtxContactCNAT[index])
114  {
115  return object->Shape.VtxFriction[index];
116  }
117  }
118  return 0;
119 }

References C4Object::Shape, and C4Shape::VtxContactCNAT.

Referenced by C4Object::DoMovement().

Here is the caller graph for this function:

◆ ContactVtxWeight()

int32_t ContactVtxWeight ( C4Object object)

Definition at line 87 of file C4Movement.cpp.

88 {
89  for (int32_t index = 0; index < object->Shape.VtxNum; index++)
90  {
91  if (object->Shape.VtxContactCNAT[index])
92  {
93  if (object->Shape.VtxX[index] < 0)
94  {
95  return -1;
96  }
97  if (object->Shape.VtxX[index] > 0)
98  {
99  return +1;
100  }
101  }
102  }
103  return 0;
104 }
int32_t VtxX[C4D_MaxVertex]
Definition: C4Shape.h:43

References C4Object::Shape, C4Shape::VtxContactCNAT, and C4Shape::VtxX.

Referenced by C4Object::DoMovement().

Here is the caller graph for this function:

◆ RedirectForce()

void RedirectForce ( C4Real from,
C4Real to,
int32_t  tdir 
)

Definition at line 45 of file C4Movement.cpp.

46 {
47  C4Real force_redirected = std::min(Abs(from), FRedirect);
48  from -= force_redirected * Sign(from);
49  to += force_redirected * tdir;
50 }
const C4Real FRedirect
Definition: C4Movement.cpp:31
int Sign(T val)
Definition: Standard.h:45
T Abs(T val)
Definition: Standard.h:42

References Abs(), FRedirect, and Sign().

Referenced by C4Object::DoMovement().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SimFlight()

bool SimFlight ( C4Real x,
C4Real y,
C4Real xdir,
C4Real ydir,
int32_t  min_density,
int32_t  max_density,
int32_t &  iterations 
)

Definition at line 810 of file C4Movement.cpp.

811 {
812  bool hit_on_time = true;
813  bool break_main_loop = false;
814  int32_t target_x, target_y;
815  int32_t current_x = fixtoi(x);
816  int32_t current_y = fixtoi(y);
817  int32_t index = iterations;
818  do
819  {
820  if (!--index)
821  {
822  hit_on_time = false;
823  break;
824  }
825  // If the object isn't moving and there is no gravity either, abort
826  if (xdir == 0 && ydir == 0 && GravAccel == 0)
827  {
828  return false;
829  }
830  // If the object is above the landscape flying upwards in no/negative gravity, abort
831  if (ydir <= 0 && GravAccel <= 0 && current_y < 0)
832  {
833  return false;
834  }
835  // Set target position by momentum
836  x += xdir;
837  y += ydir;
838  // Set target position, then iterate towards this position, checking for contact
839  target_x = fixtoi(x);
840  target_y = fixtoi(y);
841  // Is the target position outside of the landscape?
842  if (!Inside<int32_t>(target_x, 0, ::Landscape.GetWidth()) || (target_y >= ::Landscape.GetHeight()))
843  {
844  return false;
845  }
846  // Check the way towards the target position, by doing minimal movement steps
847  do
848  {
849  // Set next step target
850  current_x += Sign(target_x - current_x);
851  current_y += Sign(target_y - current_y);
852  // Contact check
853  if (Inside(GBackDensity(current_x, current_y), min_density, max_density))
854  {
855  break_main_loop = true;
856  break;
857  }
858  }
859  while ((current_x != target_x) || (current_y != target_y));
860  // Adjust GravAccel once per frame
861  ydir += GravAccel;
862  }
863  while (!break_main_loop);
864  // Write position back
865  x = itofix(current_x);
866  y = itofix(current_y);
867 
868  // How many steps did it take to get here?
869  iterations -= index;
870 
871  return hit_on_time;
872 }
C4Landscape Landscape
int32_t GBackDensity(int32_t x, int32_t y)
Definition: C4Landscape.h:224
#define GravAccel
Definition: C4Physics.h:27
C4Fixed itofix(int32_t x)
Definition: C4Real.h:261
int fixtoi(const C4Fixed &x)
Definition: C4Real.h:259
bool Inside(T ival, U lbound, V rbound)
Definition: Standard.h:43
int32_t GetWidth() const
int32_t GetHeight() const

References fixtoi(), GBackDensity(), C4Landscape::GetHeight(), C4Landscape::GetWidth(), GravAccel, Inside(), itofix(), Landscape, and Sign().

Referenced by SimFlightHitsLiquid().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SimFlightHitsLiquid()

bool SimFlightHitsLiquid ( C4Real  start_x,
C4Real  start_y,
C4Real  xdir,
C4Real  ydir 
)

Definition at line 874 of file C4Movement.cpp.

875 {
876  // Start in water?
877  if (DensityLiquid(GBackDensity(fixtoi(start_x), fixtoi(start_y))))
878  {
879  int temp = 10;
880  if (!SimFlight(start_x, start_y, xdir, ydir, 0, C4M_Liquid - 1, temp))
881  {
882  return false;
883  }
884  }
885  // Hits liquid?
886  int temp = -1;
887  if (!SimFlight(start_x, start_y, xdir, ydir, C4M_Liquid, C4M_Vehicle, temp))
888  {
889  return false;
890  }
891  // Liquid & deep enough?
892  return GBackLiquid(fixtoi(start_x), fixtoi(start_y)) && GBackLiquid(fixtoi(start_x), fixtoi(start_y) + 9);
893 }
const int32_t C4M_Vehicle
Definition: C4Constants.h:171
const int32_t C4M_Liquid
Definition: C4Constants.h:174
bool GBackLiquid(int32_t x, int32_t y)
Definition: C4Landscape.h:239
bool DensityLiquid(int32_t dens)
Definition: C4Landscape.h:206
bool SimFlight(C4Real &x, C4Real &y, C4Real &xdir, C4Real &ydir, int32_t min_density, int32_t max_density, int32_t &iterations)
Definition: C4Movement.cpp:810

References C4M_Liquid, C4M_Vehicle, DensityLiquid(), fixtoi(), GBackDensity(), GBackLiquid(), and SimFlight().

Referenced by ObjectComJump().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ DefaultGravAccel

const C4Real DefaultGravAccel = C4REAL100(20)

Definition at line 41 of file C4Movement.cpp.

Referenced by C4Landscape::CompileFunc(), and C4Landscape::ScenarioInit().

◆ FFriction

const C4Real FFriction = C4REAL100(30)

Definition at line 32 of file C4Movement.cpp.

Referenced by ApplyFriction().

◆ FixFullCircle

const C4Real FixFullCircle = itofix(360)

Definition at line 33 of file C4Movement.cpp.

Referenced by C4Object::DoMovement().

◆ FixHalfCircle

const C4Real FixHalfCircle = FixFullCircle / 2

Definition at line 34 of file C4Movement.cpp.

Referenced by C4Object::DoMovement().

◆ FloatFriction

const C4Real FloatFriction = C4REAL100(2)

Definition at line 35 of file C4Movement.cpp.

◆ FRedirect

const C4Real FRedirect = C4REAL100(50)

Definition at line 31 of file C4Movement.cpp.

Referenced by RedirectForce().

◆ HitSpeed1

const C4Real HitSpeed1 = C4REAL100(150)

Definition at line 37 of file C4Movement.cpp.

Referenced by C4Object::SetOCF(), and C4Object::UpdateOCF().

◆ HitSpeed2

const C4Real HitSpeed2 = itofix(2)

Definition at line 38 of file C4Movement.cpp.

◆ HitSpeed3

const C4Real HitSpeed3 = itofix(6)

Definition at line 39 of file C4Movement.cpp.

◆ HitSpeed4

const C4Real HitSpeed4 = itofix(8)

Definition at line 40 of file C4Movement.cpp.

◆ RotateAccel

const C4Real RotateAccel = C4REAL100(20)

Definition at line 36 of file C4Movement.cpp.

Referenced by C4Object::Push().