Skip to content

Commit f148468

Browse files
star-eorchid-worldbofeng-songshrinktofittangkaikk
authored
backport from 4.0.0 alpha.8 (#19205)
* feat(node): make setWorldPosition parameter z optional * Fix crash caused by float canvas size on minigame platform for some mobile phones. * Fix the issue of macro usage errors. (#58) * Fix the issue on the native platform where miniMaps are not updated after calling initDefault on a texture, resulting in the inability to retrieve texture.image. (#59) * Revert "Fix crash caused by float canvas size on minigame platform for some mobile phones." (#62) This reverts commit 7fdf0fd. * Fix memory leaks in Spine resource parsing on the web-side. (#65) * Fix memory leaks in Spine resource parsing on the web-side. * update external version # Conflicts: # native/external-config.json * Fix the issue where the character controller’s group/mask was not set correctly (#64) * Fix the issue that the implementation of Vec3 equals is inconsistent with the implementations of Vec2 and Vec4. (#57) * Fix the issue that the implementation of Vec3 equals is inconsistent with the implementations of Vec2 and Vec4. * Change approxEquals method to return boolean * Fix formatting of approxEquals method --------- Co-authored-by: hyde zhou <zlzhou.sh@outlook.com> * Remove unneed function (#70) * Remove unneed function * Reorder import statement for approx in model.ts --------- Co-authored-by: hyde zhou <zlzhou.sh@outlook.com> * TextureRegion in Spine 4.2 is used during runtime, and the atlas must be released last. (#75) * Fix out of memory for spine4.2 * update external version * feat(minigame): 支付宝小程序添加安全区域获取功能 (#44) * Fix crash on iOS when resuming download task. (#83) * change increaseThreadCount position before sendImmediate (#87) * fixing bytedance minigame file downloading blocking problem, using solution in 2.4.16 * 参考2.4.16,解决cocos在字节小游戏上下载文件,每隔2秒掉帧到30-40帧的问题。原因是_write方法里使用writeFileSync方法在字节小游戏上可能时长700ms,卡主游戏。修改方法是改成2.x的版本使用异步方法writeFile,cache-manager基本改成和2.4.16一样了 * 解决cocos在字节小游戏上下载文件,每隔2秒掉帧到30-40帧的问题。 * Fix the flickering issue in Spine that may occur when setting false first and then true within the same frame. (#84) * 1.Fix Box2D JSB collision end callback 2.fix BeginContact/EndContact mismatch (#89) * 1.基于 Box2D JSB 的2D物理系统 结束碰撞但不回调 2.回调个数BeginContact和EndContact不一致。 * recover logic of physics-2d/box2d-jsb/platform/physics-contact-listener.ts * add physics-2d/box2d-jsb/physics-contact-manager.ts * the logic is consistent with box2d-jsb & box2d (#93) * the logic is consistent with box2d-jsb & box2d * Add return type to register/unregisterContactFixture, fix lint * update 3.8.9 version and external dep * bump engine version to 3.8.9 --------- Co-authored-by: orchid-world <orchidworld@163.com> Co-authored-by: bofeng-song <song2008_2001@126.com> Co-authored-by: Leslie Leigh (李的序) <shrinktofit@outlook.com> Co-authored-by: tangkai <1944876319@qq.com> Co-authored-by: pengchengo <304829842@qq.com>
2 parents 68818ea + 00b9410 commit f148468

25 files changed

Lines changed: 349 additions & 235 deletions

File tree

cocos/asset/assets/texture-2d.jsb.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ texture2DProto._ctor = function () {
5656
this._mipmaps = [];
5757
};
5858

59+
const oldInitDefault = texture2DProto.initDefault;
60+
texture2DProto.initDefault = function (uuid: string) {
61+
oldInitDefault.call(this, uuid);
62+
this._mipmaps = this.getMipmaps();
63+
}
64+
5965
texture2DProto._serialize = function (ctxForExporting: any) {
6066
if (EDITOR || TEST) {
6167
return {

cocos/core/global-exports.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ if (DEV) {
5555
cclegacy._Test = {};
5656
}
5757

58-
const engineVersion = '3.8.8';
58+
const engineVersion = '3.8.9';
5959

6060
/**
6161
* @en

cocos/core/math/vec3.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -959,11 +959,7 @@ export class Vec3 extends ValueType {
959959
* @returns Returns `true` when the components of both vectors are equal within the specified range of error; otherwise it returns `false`.
960960
*/
961961
public equals (other: Vec3, epsilon = EPSILON): boolean {
962-
return (
963-
abs(this.x - other.x) <= epsilon
964-
&& abs(this.y - other.y) <= epsilon
965-
&& abs(this.z - other.z) <= epsilon
966-
);
962+
return Vec3.equals(this, other, epsilon);
967963
}
968964

969965
/**
@@ -976,11 +972,10 @@ export class Vec3 extends ValueType {
976972
* @returns Returns `true` when the components of both vectors are equal within the specified range of error; otherwise it returns `false`.
977973
*/
978974
public equals3f (x: number, y: number, z: number, epsilon = EPSILON): boolean {
979-
return (
980-
abs(this.x - x) <= epsilon
981-
&& abs(this.y - y) <= epsilon
982-
&& abs(this.z - z) <= epsilon
983-
);
975+
const self = this;
976+
return (abs(self.x - x) <= epsilon * max(1.0, abs(self.x), abs(x))
977+
&& abs(self.y - y) <= epsilon * max(1.0, abs(self.y), abs(y))
978+
&& abs(self.z - z) <= epsilon * max(1.0, abs(self.z), abs(z)));
984979
}
985980

986981
/**
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright (c) 2024 Xiamen Yaji Software Co., Ltd.
3+
4+
https://www.cocos.com/
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights to
9+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10+
of the Software, and to permit persons to whom the Software is furnished to do so,
11+
subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
*/
24+
import { PhysicsContact, b2ContactExtends } from './physics-contact';
25+
26+
const contactMap = new Map<b2ContactExtends, PhysicsContact>();
27+
const pools: PhysicsContact[] = [];
28+
29+
export class PhysicsContactManager {
30+
static get (b2contact: b2ContactExtends): PhysicsContact {
31+
let c = pools.pop();
32+
if (!c) {
33+
c = new PhysicsContact();
34+
}
35+
c.init(b2contact);
36+
contactMap.set(b2contact, c);
37+
return c;
38+
}
39+
40+
static find (b2contact: b2ContactExtends): PhysicsContact | null {
41+
return contactMap.get(b2contact) ?? null;
42+
}
43+
44+
static put (b2contact: b2ContactExtends): void {
45+
const c = contactMap.get(b2contact);
46+
if (!c) return;
47+
pools.push(c);
48+
c.reset();
49+
contactMap.delete(b2contact);
50+
}
51+
52+
static clear (): void {
53+
contactMap.clear();
54+
pools.length = 0;
55+
}
56+
}

cocos/physics-2d/box2d-jsb/physics-contact.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ export type b2ContactExtends = b2jsb.Contact & {
3131
m_userData: any
3232
}
3333

34-
const pools: PhysicsContact[] = [];
35-
3634
// temp world manifold
3735
const pointCache = [new Vec2(), new Vec2()];
3836

@@ -74,25 +72,6 @@ const impulse: IPhysics2DImpulse = {
7472
};
7573

7674
export class PhysicsContact implements IPhysics2DContact {
77-
static get (b2contact: b2ContactExtends): PhysicsContact {
78-
let c = pools.pop();
79-
80-
if (!c) {
81-
c = new PhysicsContact();
82-
}
83-
84-
c.init(b2contact);
85-
return c;
86-
}
87-
88-
static put (b2contact: b2ContactExtends): void {
89-
const c: PhysicsContact = b2contact.m_userData as PhysicsContact;
90-
if (!c) return;
91-
92-
pools.push(c);
93-
c.reset();
94-
}
95-
9675
colliderA: Collider2D | null = null;
9776
colliderB: Collider2D | null = null;
9877

@@ -108,16 +87,13 @@ export class PhysicsContact implements IPhysics2DContact {
10887
}
10988

11089
init (b2contact: b2ContactExtends): void {
90+
this._b2contact = b2contact;
11191
this.colliderA = (b2contact.GetFixtureA().m_userData as b2Shape2D).collider;
11292
this.colliderB = (b2contact.GetFixtureB().m_userData as b2Shape2D).collider;
11393
this.disabled = false;
11494
this.disabledOnce = false;
11595
this._impulse = null!;
116-
11796
this._inverted = false;
118-
119-
this._b2contact = b2contact;
120-
b2contact.m_userData = this;
12197
}
12298

12399
reset (): void {
@@ -130,7 +106,6 @@ export class PhysicsContact implements IPhysics2DContact {
130106
this.disabled = false;
131107
this._impulse = null!;
132108

133-
this._b2contact.m_userData = null;
134109
this._b2contact = null!;
135110
}
136111

cocos/physics-2d/box2d-jsb/physics-world.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { PhysicsContactListener } from './platform/physics-contact-listener';
3636
import { PhysicsAABBQueryCallback } from './platform/physics-aabb-query-callback';
3737
import { PhysicsRayCastCallback } from './platform/physics-ray-cast-callback';
3838
import { PhysicsContact, b2ContactExtends } from './physics-contact';
39+
import { PhysicsContactManager } from './physics-contact-manager';
3940
import { Contact2DType, Collider2D, RaycastResult2D } from '../framework';
4041
import { b2Shape2D } from './shapes/shape-2d';
4142
import { PhysicsDebugDraw } from './platform/physics-debug-draw';
@@ -444,22 +445,22 @@ export class b2PhysicsWorld implements IPhysicsWorld {
444445
}
445446

446447
_onBeginContact (b2contact: b2ContactExtends): void {
447-
const c = PhysicsContact.get(b2contact);
448+
const c = PhysicsContactManager.get(b2contact);
448449
c.emit(Contact2DType.BEGIN_CONTACT);
449450
}
450451

451452
_onEndContact (b2contact: b2ContactExtends): void {
452-
const c = b2contact.m_userData as PhysicsContact;
453+
const c = PhysicsContactManager.find(b2contact);
453454
if (!c) {
454455
return;
455456
}
456457
c.emit(Contact2DType.END_CONTACT);
457458

458-
PhysicsContact.put(b2contact);
459+
PhysicsContactManager.put(b2contact);
459460
}
460461

461462
_onPreSolve (b2contact: b2ContactExtends): void {
462-
const c = b2contact.m_userData as PhysicsContact;
463+
const c = PhysicsContactManager.find(b2contact);
463464
if (!c) {
464465
return;
465466
}
@@ -468,7 +469,7 @@ export class b2PhysicsWorld implements IPhysicsWorld {
468469
}
469470

470471
_onPostSolve (b2contact: b2ContactExtends, impulse: b2jsb.ContactImpulse): void {
471-
const c: PhysicsContact = b2contact.m_userData as PhysicsContact;
472+
const c = PhysicsContactManager.find(b2contact);
472473
if (!c) {
473474
return;
474475
}

cocos/physics-2d/box2d-wasm/physics-world.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ export class B2PhysicsWorld implements IPhysicsWorld {
8282
PhysicsContactListener._PreSolve = this._onPreSolve;
8383
PhysicsContactListener._PostSolve = this._onPostSolve;
8484
this._contactListener = B2.ContactListener.implement(PhysicsContactListener.callback);
85+
const orgRegisterContactFixture = this._contactListener.registerContactFixture.bind(this._contactListener);
86+
this._contactListener.registerContactFixture = (contactFixture: number): void => {
87+
PhysicsContactListener.registerContactFixture(contactFixture);
88+
orgRegisterContactFixture(contactFixture);
89+
};
90+
const orgUnregisterContactFixture = this._contactListener.unregisterContactFixture.bind(this._contactListener);
91+
this._contactListener.unregisterContactFixture = (contactFixture: number): void => {
92+
PhysicsContactListener.unregisterContactFixture(contactFixture);
93+
orgUnregisterContactFixture(contactFixture);
94+
};
8595
this._world.SetContactListener(this._contactListener);
8696

8797
this._aabbQueryCallback = B2.QueryCallback.implement(PhysicsAABBQueryCallback.callback);

cocos/physics-2d/box2d-wasm/platform/physics-contact-listener.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,56 @@
2222
THE SOFTWARE.
2323
*/
2424

25+
import { B2 } from '../instantiated';
26+
2527
type BeginContactCallback = (contact: number) => void;
2628
type EndContactCallback = (contact: number) => void;
2729
type PreSolveCallback = (contact: number, oldManifold: number) => void;
2830
type PostSolveCallback = (contact: number, impulse: number) => void;
2931

3032
export class PhysicsContactListener {
33+
static _contactFixtures: Set<number> = new Set<number>();
3134
static _BeginContact: BeginContactCallback | null = null;
3235
static _EndContact: EndContactCallback | null = null;
3336
static _PreSolve: PreSolveCallback | null = null;
3437
static _PostSolve: PostSolveCallback | null = null;
38+
static _shouldReportContacts: Set<number> = new Set<number>();
3539

3640
static BeginContact (contact: number): void {
37-
if (this._BeginContact) {
41+
if (!this._BeginContact) return;
42+
const fixtureA = B2.ContactGetFixtureA(contact) as number;
43+
const fixtureB = B2.ContactGetFixtureB(contact) as number;
44+
const fixtures = this._contactFixtures;
45+
this._shouldReportContacts.delete(contact);
46+
47+
if (fixtures.has(fixtureA) || fixtures.has(fixtureB)) {
48+
this._shouldReportContacts.add(contact);
3849
this._BeginContact(contact);
3950
}
4051
}
4152

4253
static EndContact (contact: number): void {
43-
if (this._EndContact) {
54+
if (this._EndContact && this._shouldReportContacts.has(contact)) {
55+
this._shouldReportContacts.delete(contact);
4456
this._EndContact(contact);
4557
}
4658
}
4759

4860
static PreSolve (contact: number, oldManifold: number): void {
49-
if (this._PreSolve) {
61+
if (this._PreSolve && this._shouldReportContacts.has(contact)) {
5062
this._PreSolve(contact, oldManifold);
5163
}
5264
}
65+
static registerContactFixture (fixture: number): void {
66+
this._contactFixtures.add(fixture);
67+
}
68+
69+
static unregisterContactFixture (fixture: number): void {
70+
this._contactFixtures.delete(fixture);
71+
}
5372

5473
static PostSolve (contact: number, impulse: number): void {
55-
if (this._PostSolve) {
74+
if (this._PostSolve && this._shouldReportContacts.has(contact)) {
5675
this._PostSolve(contact, impulse);
5776
}
5877
}

cocos/physics/physx/character-controllers/physx-box-character-controller.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,12 @@ export class PhysXBoxCharacterController extends PhysXCharacterController implem
6767
controllerDesc.position = { x: v3_0.x, y: v3_0.y, z: v3_0.z };//PxExtendedVec3
6868
controllerDesc.setMaterial(pxMtl);
6969
controllerDesc.setReportCallback(PX.PxUserControllerHitReport.implement(physxWorld.callback.controllerHitReportCB));
70-
this._impl = PX.createBoxCharacterController(physxWorld.controllerManager, controllerDesc);
70+
const impl = PX.createBoxCharacterController(physxWorld.controllerManager, controllerDesc);
71+
this.setImpl(impl);
7172

72-
if (this._impl.$$) {
73-
PX.IMPL_PTR[this._impl.$$.ptr] = this;
74-
const shapePtr = this._impl.getShape().$$.ptr;
73+
if (impl.$$) {
74+
PX.IMPL_PTR[impl.$$.ptr] = this;
75+
const shapePtr = impl.getShape().$$.ptr;
7576
PX.IMPL_PTR[shapePtr] = this;
7677
}
7778

cocos/physics/physx/character-controllers/physx-capsule-character-controller.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,12 @@ export class PhysXCapsuleCharacterController extends PhysXCharacterController im
6868
controllerDesc.position = { x: v3_0.x, y: v3_0.y, z: v3_0.z };//PxExtendedVec3
6969
controllerDesc.setMaterial(pxMtl);
7070
controllerDesc.setReportCallback(PX.PxUserControllerHitReport.implement(physxWorld.callback.controllerHitReportCB));
71-
this._impl = PX.createCapsuleCharacterController(physxWorld.controllerManager, controllerDesc);
71+
const impl = PX.createCapsuleCharacterController(physxWorld.controllerManager, controllerDesc);
72+
this.setImpl(impl);
7273

73-
if (this._impl.$$) {
74-
PX.IMPL_PTR[this._impl.$$.ptr] = this;
75-
const shapePtr = this._impl.getShape().$$.ptr;
74+
if (impl.$$) {
75+
PX.IMPL_PTR[impl.$$.ptr] = this;
76+
const shapePtr = impl.getShape().$$.ptr;
7677
PX.IMPL_PTR[shapePtr] = this;
7778
}
7879

0 commit comments

Comments
 (0)