forked from coronalabs/corona
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRtt_PhysicsContact.cpp
175 lines (140 loc) · 3.79 KB
/
Rtt_PhysicsContact.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//////////////////////////////////////////////////////////////////////////////
//
// This file is part of the Corona game engine.
// For overview and more information on licensing please refer to README.md
// Home page: https://github.com/coronalabs/corona
// Contact: [email protected]
//
//////////////////////////////////////////////////////////////////////////////
#include "Core/Rtt_Build.h"
#ifdef Rtt_PHYSICS
#include "Rtt_PhysicsContact.h"
// TODO: see which of these are actually needed here
#include "Rtt_Lua.h"
#include "Rtt_LuaLibPhysics.h"
#include "Rtt_LuaProxy.h"
#include "Rtt_Runtime.h"
#include "Rtt_LuaProxyVTable.h"
#include "Rtt_LuaContext.h"
#include "Box2D/Box2D.h"
// ----------------------------------------------------------------------------
namespace Rtt
{
// ----------------------------------------------------------------------------
const char PhysicsContact::kMetatableName[] = "physics.contact"; // unique identifier for this userdata type
UserdataWrapper *
PhysicsContact::CreateWrapper( const ResourceHandle< lua_State >& luaStateHandle, b2Contact *contact )
{
// Lua owns wrapper (which has a weak pointer to joint)
UserdataWrapper *result = Rtt_NEW(
runtime.Allocator(),
UserdataWrapper( luaStateHandle, contact, PhysicsContact::kMetatableName ) );
return result;
}
b2Contact*
PhysicsContact::GetContact( lua_State *L, int index )
{
b2Contact *result = NULL;
UserdataWrapper **ud = (UserdataWrapper **)luaL_checkudata( L, index, Self::kMetatableName );
if ( ud )
{
UserdataWrapper *wrapper = *ud;
result = (b2Contact*)wrapper->Dereference();
}
return result;
}
int
PhysicsContact::ValueForKey( lua_State *L )
{
int result = 0; // number of args pushed on the stack
b2Contact *contact = GetContact( L, 1 );
if ( contact )
{
const char *key = luaL_checkstring( L, 2 );
result = 1;
if ( 0 == strcmp( "isTouching", key ) )
{
lua_pushboolean( L, contact->IsTouching() );
}
else if ( 0 == strcmp( "isEnabled", key ) )
{
lua_pushboolean( L, contact->IsEnabled() );
}
else if ( 0 == strcmp( "friction", key ) )
{
lua_pushnumber( L, contact->GetFriction() );
}
else if ( 0 == strcmp( "bounce", key ) )
{
lua_pushnumber( L, contact->GetRestitution() );
}
// STEVE CHANGE
else if ( 0 == strcmp( "tangentSpeed", key ) )
{
lua_pushnumber( L, contact->GetTangentSpeed() );
}
// /STEVE CHANGE
else
{
result = 0;
}
}
return result;
}
int
PhysicsContact::SetValueForKey( lua_State *L )
{
b2Contact *contact = GetContact( L, 1 );
if ( contact )
{
const char *key = luaL_checkstring( L, 2 );
if ( 0 == strcmp( "isEnabled", key ) )
{
contact->SetEnabled( lua_toboolean( L, 3 ) );
}
else if ( 0 == strcmp( "friction", key ) )
{
contact->SetFriction( lua_tonumber( L, 3 ) );
}
else if ( 0 == strcmp( "bounce", key ) )
{
contact->SetRestitution( lua_tonumber( L, 3 ) );
}
// STEVE CHANGE
else if ( 0 == strcmp( "tangentSpeed", key ) )
{
contact->SetTangentSpeed( lua_tonumber( L, 3 ) );
}
// /STEVE CHANGE
}
return 0;
}
int
PhysicsContact::Finalizer( lua_State *L )
{
UserdataWrapper **ud = (UserdataWrapper **)luaL_checkudata( L, 1, Self::kMetatableName );
if ( ud )
{
UserdataWrapper *wrapper = *ud;
Rtt_DELETE( wrapper );
}
return 0;
}
// Call this to init metatable
void
PhysicsContact::Initialize( lua_State *L )
{
Rtt_LUA_STACK_GUARD( L );
const luaL_Reg kVTable[] =
{
{ "__index", Self::ValueForKey },
{ "__newindex", Self::SetValueForKey },
{ "__gc", Self::Finalizer },
{ NULL, NULL }
};
Lua::InitializeMetatable( L, Self::kMetatableName, kVTable );
}
// ----------------------------------------------------------------------------
} // namespace Rtt
// ----------------------------------------------------------------------------
#endif // Rtt_PHYSICS